Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: calling a sub using a variable's value

by broquaint (Abbot)
on Apr 23, 2004 at 19:19 UTC ( [id://347718]=note: print w/replies, xml ) Need Help??


in reply to calling a sub using a variable's value

As others have said, a dispatch table is almost certainly the way to go. But, of course, there's more than one way to symbolically dereference a subroutine in compliance with strictures
use strict; sub foo { print "this is foo(@_)\n" } sub bar { print "this is bar(@_)\n" } __PACKAGE__->can($_)->('invoked via can()') for qw/ foo bar /; ( \&$_ )->('invoked without a temp var') for qw/ foo bar /; __output__ this is foo(invoked via can()) this is bar(invoked via can()) this is foo(invoked without a temp var) this is bar(invoked without a temp var)
In the first instance we're executing the subroutine returned by UNIVERSAL, and in the second instance we're creating a sub reference and then executing (the parens are necessary for syntactical disambiguation).
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: calling a sub using a variable's value
by dragonchild (Archbishop) on Apr 23, 2004 at 19:21 UTC
    I really like the __PACKAGE__->can() construct. Obfu'ed, but clear at the same time. :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Log In?
Username:
Password:

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

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

    No recent polls found