Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: execute a string as a function

by Corion (Pope)
on Aug 06, 2013 at 15:30 UTC ( #1048135=note: print w/ replies, xml ) Need Help??


in reply to execute a string as a function

While what you describe is possible in principle through Perl built-ins, it's usually best to use the technique of a dispatch table:

my %record_seq_handler= ( 'A' => \&unpack_a, 'B' => \&unpack_B, 'C' => \&unpack_C, ); my $handler= $record_seq_handler{ $RECORD_SEQ_ID } or die "Unknown record sequence '$RECORD_SEQ_ID'"; $handler->( $lines[$_] );

The way you described directly would be through eval, but this approach has various pitfalls that the above code avoids.


Comment on Re: execute a string as a function
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2015-07-03 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (48 votes), past polls