http://www.perlmonks.org?node_id=1048135


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.