Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Test if a string is a sub name

by choroba (Cardinal)
on Oct 05, 2022 at 21:56 UTC ( #11147265=note: print w/replies, xml ) Need Help??


in reply to Test if a string is a sub name

You can use exists and defined to find out:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; sub My::Filters::known {} sub My::Filters::forward; for my $string (qw(My::Filters::known My::Filters::unknown My::Filters::forward) ) { say join "\t", $string, defined &$string, exists &$string; }

Output:

My::Filters::known 1 1 My::Filters::unknown My::Filters::forward 1
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Test if a string is a sub name
by BillKSmith (Monsignor) on Oct 06, 2022 at 15:15 UTC
    I do not understand how your expression &$string works. The documentation for exists tells us that the expression must return the name of a subroutine. It appears to me that this would only be true for your expression if $string is a symbolic reference (which is prohibited by use strict). I would expect you need string interpolation (perhaps "&$string") but this syntax throws an error that the argument of exists is not a subroutine.
    Bill
      It seems the documentation uses the term "subroutine name" in quite a confusing way. Check the examples:
      Use of a subroutine call, rather than a subroutine name, as an argument to exists is an error.
      exists ⊂ # OK exists &sub(); # Error

      So, in this context, "subroutine name" includes the ampersand.

      Considering strict, note the exceptions to strict 'refs':

      $bar = \&{'foo'}; &$bar;
      We are not calling the subroutine, we're just checking it exists/is defined.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^2: Test if a string is a sub name
by clscott (Friar) on Oct 05, 2022 at 22:42 UTC
    That's great. Thanks very much
    --
    Clayton

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11147265]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2023-02-03 14:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (26 votes). Check out past polls.

    Notices?