Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^9: Function name in a variable, can't recall the concept (introspection with ->can )

by LanX (Saint)
on Apr 19, 2019 at 15:23 UTC ( [id://1232807]=note: print w/replies, xml ) Need Help??


in reply to Re^8: Function name in a variable, can't recall the concept (introspection with ->can )
in thread Function name in a variable, can't recall the concept

I'm afraid your hypothetical code for his hypothetical function is ignoring how typeglobs work.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

  • Comment on Re^9: Function name in a variable, can't recall the concept (introspection with ->can )

Replies are listed 'Best First'.
Re^10: Function name in a variable, can't recall the concept (introspection with ->can )
by Veltro (Hermit) on Apr 19, 2019 at 17:30 UTC

    Oh yeah, haha, what have I been smoking!

    I'll probably stick with this then:

    sub get_sub_by_name { my ($pkg, $name) = @_ ; no strict 'refs' ; if ( defined &{$pkg . '::'. $name} ) { return \&{$pkg . '::'. $name} ; } }

      \&$name works even under strict refs. No need to turn it off.

      You need to return something (undef?) in the else case.

        \&$name works even under strict refs. No need to turn it off.

        Perfect, makes the code even simpler

        You need to return something (undef?) in the else case.

        Good question actually. My tests didn't seem to need it...

        sub test { ... } ; ... if ( my $s = get_sub_by_name( "", "test" ) ) { $s->() ; } else { print "No\n" ; }
      > I been smoking!

      something very strong it's still broken.

      update

      I was wrong, it works almost as well as ->can for non-class modules.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2024-04-19 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found