Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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 about the Monastery: (14)
As of 2014-10-31 19:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (223 votes), past polls