Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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 ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Function reference undef when extract from hash?
by reneeb (Chaplain) on Mar 03, 2008 at 11:09 UTC | |
by Anonymous Monk on Mar 03, 2008 at 18:25 UTC | |
by ikegami (Patriarch) on Mar 04, 2008 at 00:55 UTC | |
|
Re: Function reference undef when extract from hash?
by chromatic (Archbishop) on Mar 03, 2008 at 17:37 UTC | |
by Anonymous Monk on Mar 04, 2008 at 05:25 UTC | |
by chromatic (Archbishop) on Mar 04, 2008 at 06:51 UTC | |
|
Re: Function reference undef when extract from hash?
by hipowls (Curate) on Mar 03, 2008 at 11:05 UTC | |
by ysth (Canon) on Mar 03, 2008 at 21:06 UTC | |
|
Re: Function reference undef when extract from hash?
by grizzley (Chaplain) on Mar 03, 2008 at 11:43 UTC |