http://qs321.pair.com?node_id=1203943


in reply to Creating dispatch table with variable in function name

It "works" fine for me:

use warnings; use strict; use Data::Dumper; my %d = map { $_, \&{'create_' . $_} } qw(first); print Dumper \%d; $d{first}->(); sub create_first { print "first!\n"; }

Output:

$VAR1 = { 'first' => sub { "DUMMY" } }; first!

...so you need to describe what's not working as expected.

Are you wanting to call the sub like this?:

$dispatch{_create_first}->();

If so, you need to map the '_create_' to both arguments to map. This example uses a second map() to modify the $_ variable on the way into the map that creates the actual dispatch table.

use warnings; use strict; use Data::Dumper; my %d = map { $_, \&{$_} } map {'create_' . $_} qw(first); print Dumper \%d; $d{create_first}->(); sub create_first { print "first!\n"; }

Output:

$VAR1 = { 'create_first' => sub { "DUMMY" } }; first!