Say I have the following code snippet:
my $compiled = eval 'sub { print( "Hello World\n" ); }';
I can call it by writing:
$compiled->();
So far so good. Now imagine that I am creating 10 functions:
my @fns = (); for ( my $i = 0; $i < 10; $i++ ) { push( @fns, eval "sub { print( 'I am function $i\n' ); }" ); }
I can name these 10 functions as follows:
foreach ( @fns ) { $_->(); }
Now I want to create a dynamic function that explicitly calls each of my 10 functions:
my $evalcode = "sub {"; foreach ( @fns ) {
Is it possible to take a gated link to a subprogram and call it directly?
PS why, you ask? Since I would like to write a dynamic routine that builds a chain of if ( m/.../ ) { } elsif ( m/.../ ) { } ... checks, which then call dynamic functions depending on the input string.
source share