Hi Monks,
I encounter some trouble when I try to dispatch function via hash:
If I call the build_sth like this:sub write_flag{ print "Success for flag.\n"; } sub write_sets{print "Success for Set.\n"; } sub do_nothing{print "Exit Here\n";} my %Op = ( set_to_flag => \&write_flag, set_tree => \&write_sets, default => \&do_nothing ); sub build_sth{ my ($op_code) = @_; print "\n\nCode: $op_code:\n"; my $op_exec = $Op{$op_code} ; if(defined($op_exec)){ print "it exist: $op_exec\n"; }else{ print "Oop\n"; } }
It give me:build_sth('set_to_flag');
If I try to execute the function reference (by &$op_exec()) in build_sth, I will get:Oop
What happened? How can I get it works?Can't use string ("") as a subroutine ref while "strict refs" in use a +t ...
In reply to Function reference undef when extract from hash? by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |