Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

how to find a subroutine exists or not

by kingofking (Acolyte)
on Apr 20, 2006 at 14:26 UTC ( #544608=perlquestion: print w/replies, xml ) Need Help??
kingofking has asked for the wisdom of the Perl Monks concerning the following question:

Dear all,

I am trying to find whether a particular subroutine exists or not.

so far I have

if (&test;) { test(); } sub test { #do something; }

If the subroutine "test" does not exists, it is popuping an error as :

Undefined subroutine &main::test called at

Kindly suggest me.


Replies are listed 'Best First'.
Re: how to find a subroutine exists or not
by revdiablo (Prior) on Apr 20, 2006 at 14:31 UTC

    Update: Ah. This works, but only sort of. See diotalevi's response below, as well as the other posts in the thread.

    This should work:

    if (my $coderef = __PACKAGE__->can("test")) { $coderef->(); }

    Update: it might be worth pointing out that this is equivalent:

    if (__PACKAGE__->can("test")) { test(); }

    And if you're in the main package, this is as well:

    if (main->can("test")) { test(); }

      This works, sort of. It also brings in @ISA and any overloaded ->can(...) methods so you might find that __PACKAGE__->can( 'test' ) is true even if the current package doesn't have a &test function. You should have said defined &test which is the documented and backwards compatible way to test for this.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      I think __PACKAGE__ should be mentioned in perlvar. I understand that it is not a "variable", strictly speaking, but it is a symbol with special meaning, and perlvar seems the obvious place to start looking for documentation.

      Perhaps just a mention of the name and pointer to perldata, so a quick grep can catch it.

        It's not a symbol, it's a special literal that's documented in perldata. That said, a mention of it in perlvar probably makes sense, too.


        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: how to find a subroutine exists or not
by Zaxo (Archbishop) on Apr 20, 2006 at 14:57 UTC

    This should do it,

    test() if defined *test{CODE};
    That directly checks the symbol table to see if *test contains a code definition.

    After Compline,

      This does not work when the symbol is a constant sub and you're using perl 5.9.3+. defined &test s the documented and backwards compatible way to test for this.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: how to find a subroutine exists or not
by sen (Hermit) on Apr 20, 2006 at 17:19 UTC


    Try this one.

    print "Exists\n" if exists &subroutine; print "Defined\n" if defined &subroutine;


Re: how to find a subroutine exists or not
by pileofrogs (Priest) on Apr 20, 2006 at 21:06 UTC

    What about plain old...

    eval { test() }; if ( $@ =~ /Undefined subroutine/ ) { # we know it doesn't exits }

    Oh, wait, I think I get it. We want to know if it exists before we run it...

      eval { rm_min_rf_root() } was_lucky() if $@ =~ /Undefined subroutine/;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://544608]
Approved by Tanalis
Front-paged by Courage
[erix]: " mathematics has brought rigor to economics. Unfortunately, it also brought mortis "

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2017-01-22 11:28 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (187 votes). Check out past polls.