Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: calling subroutines from variables

by Fletch (Bishop)
on Aug 21, 2008 at 17:15 UTC ( [id://705883]=note: print w/replies, xml ) Need Help??


in reply to calling subroutines from variables

No, creating a hash of name => code ref is the normal technique. Do a search for "dispatch table".

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: calling subroutines from variables
by Tanktalus (Canon) on Aug 21, 2008 at 18:08 UTC

    This doesn't look like a dispatch table to me. If you're reading in a bunch of sub names from an external source, and calling them, order may matter. Hashes don't preserve that. "But I'm just suggesting using the table to convert from string to code ref! You can use the order of strings to control the order!" True, but why produce another hash when the symbol table (another hash) already exists, with ways to query it?

    $ perl -le 'sub foo { }; print \&foo;print __PACKAGE__->can("foo")' CODE(0xf81930) CODE(0xf81930)
    No non-strict code. And when I add new functions, no need to remember to update the symbol table - perl does that automatically.

    Don't get me wrong. I like dispatch tables. I use them. A lot. This just doesn't seem like a use for them to me, mostly because it seems like the OP is trying to loop through them.

      I'm not sure I want an external source telling me which subs in my package to run without some control over which ones it's allowed to run. A hash of the allowed subrefs with a default error sub makes sense in that context.
      use strict; use warnings; sub one { print "one\n"; } sub two { print "two\n"; } sub tres { # some private sub they don't need to access } my %dispatch = ( 'one' => \&one, 'uno' => \&one, 'two' => \&two, 'dos' => \&two, 'default' => sub { print "sorry, $_[0] not found.\n"; }, ); my @do_these = qw( one two uno dos tres ); foreach my $do_this ( @do_these ) { if ( exists $dispatch{ $do_this } ) { $dispatch{ $do_this }->(); } else { $dispatch{ 'default' }->( $do_this ); } }
Re^2: calling subroutines from variables
by bman1974 (Initiate) on Aug 21, 2008 at 17:21 UTC
    Thanks, I will try the building the hash. I was looking at that and wanted to see if there were other options. For this particular problem that is the way to go.
    Thanks, B

      Of course, if you have a lot of subroutines, you may still want symbolic refs to help construct the hash:

      my %dispatch = map { ; no strict 'refs' ; ($_, \&$_) } qw(sub1 sub2 +sub3 ...) ;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-19 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found