Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: execute a string as a function

by mtve (Deacon)
on Oct 09, 2013 at 07:49 UTC ( [id://1057500]=note: print w/replies, xml ) Need Help??


in reply to execute a string as a function

dispatchers are good, but with DRY principle you may use code like this

use warnings; use strict; sub unpack_a { print "a(@_)\n"; 11 } sub unpack_b { print "b(@_)\n"; 22 } my $RECORD_SEQ_ID = 'a'; local $_ = 0; my @lines = 42; my $res = do { no strict 'refs'; &{"unpack_$RECORD_SEQ_ID"} ($lines[$_ +]) }; print $res;

Yes, it's not so secure if you don't understand what you're doing, and it's around 20% slower then dispatchers which is usually does not matter.

Log In?
Username:
Password:

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

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

    No recent polls found