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

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

by LanX (Saint)
on Apr 16, 2019 at 21:30 UTC ( [id://1232691]=note: print w/replies, xml ) Need Help??


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

No I'm saying if one is really worried that the inspected module is a class, one can easily check @ISA.

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

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

Replies are listed 'Best First'.
Re^6: Function name in a variable, can't recall the concept (introspection with ->can )
by AnomalousMonk (Archbishop) on Apr 16, 2019 at 22:22 UTC

    What about a base class? Do such classes always have a declared  @ISA that is empty?


    Give a man a fish:  <%-{-{-{-<

      Actually I don't understand the discussion anymore, if the OP was willing to inspect a class the natural solution would be ->can.

      If he wasn't inspecting a class ->can would do the job, too.

      Are there pathological cases where an approach might break?

      Of course, this is Perl, with high flexibility to bend internals.

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

        No, that's not true.

        If they're trying to resolve a method, then ->can is correct.

        If they're trying to resolve a sub, then ->can can fail

        The OP is looking for a sub.

Re^6: Function name in a variable, can't recall the concept (introspection with ->can )
by ikegami (Patriarch) on Apr 19, 2019 at 08:53 UTC

    So it is as I said. You're suggesting that we use

    my $sub = @{ get_array_by_name($pkg, 'ISA') } ? get_sub_by_name($pkg, $name) : $pkg->can($name);
    instead of
    my $sub = get_sub_by_name($pkg, $name);

    That's ludicrous.

      > get_sub_by_name

      which get_sub_by_name?

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

        He probably means something hypothetical...

        I was actually thinking about this seriously, and I would likely do something as:

        sub get_sub_by_name { my ($pkg, $name) = @_ ; no strict 'refs' ; if ( exists ${$pkg . '::'}{$name} ) { if ( ref ${$pkg . '::'}{$name} eq 'CODE' ) { return *{$pkg . '::' . $name} ; } } ; }

        Maybe it can be written more pretty, and I don't know if any odd-cases are trapped (e.g. what about __ANON__)

        edit: Comes to think of it, maybe the line 'exists' is not even needed?

        Some code that gets a reference to the sub by name. What that code is doesn't matter, though it could be as simple as \&{ $pkg . '::' . $name }.

      > You're suggesting that we use

      >  my $sub = @{ get_array_by_name($pkg, 'ISA') } ? get_sub_by_name($pkg, $name) : $pkg->can($name);

      No, I don't!

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

        uh, then feel free to clarify why you meant by "one can easily check @ISA".

Log In?
Username:
Password:

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

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

    No recent polls found