Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

You can check if it's in a specific package, but

  • I don't know anything that checks if a function exists in any package (so I coded up a solution below).
  • I don't know anything that checks if a function is in a module (and that's impossible to do correctly given Perl's dynamic nature).
  • I don't know anything that checks if a function is in any module on the machine (and that's impossible to do correctly given Perl's dynamic nature).

In the current package:

UNIVERSAL::can(__PACKAGE__, 'myfunc')

In a specific package:

UNIVERSAL::can($pkg, 'myfunc')

In any package:

sub check_for_func { my ($func_name) = @_; my @pkgs_with_func; my $helper; # Don't combine this line with the assignement. $helper = sub { my ($pkg_name) = @_; my $pkg = do { no strict 'refs'; \%{$pkg_name.'::'} }; push(@pkgs_with_func, $pkg_name) if $pkg->{$func_name} && *{$pkg->{$func_name}}{CODE}; my $pkg_name_ = ($pkg_name eq 'main' ? '' : $pkg_name.'::' ); /^(.*)::$/ && $1 ne 'main' && &$helper($pkg_name_.$1) foreach (keys(%$pkg)); }; &$helper('main'); return @pkgs_with_func; } print(join(', ', check_for_func('test')), $/);

In reply to Re: How do you determine if a user-defined function exists? by ikegami
in thread How do you determine if a user-defined function exists? by beable

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-25 06:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found