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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Creating dispatch table with variable in function name
by nysus (Parson) on Nov 21, 2017 at 19:52 UTC | |
by choroba (Cardinal) on Nov 23, 2017 at 10:07 UTC |