in reply to Re^9: 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

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} ; } }
  • Comment on Re^10: Function name in a variable, can't recall the concept (introspection with ->can )
  • Download Code

Replies are listed 'Best First'.
Re^11: Function name in a variable, can't recall the concept (introspection with ->can )
by ikegami (Patriarch) on Apr 19, 2019 at 18:06 UTC

    \&$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" ; }

        It wasn't a question. Your code relies on the result of an if statement with a false conditional being that false value, and it shouldn't.

Re^11: Function name in a variable, can't recall the concept (introspection with ->can )
by LanX (Saint) on Apr 19, 2019 at 18:52 UTC
    > 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