cat2014 has asked for the wisdom of the Perl Monks concerning the following question:
My problem is with how to get something to be passed into the selected function. It looks like whatever i pass in is turned into "1" -but you can return whatever you want. Am I totally wasting my time getting this to work, or is there actually someway to set up a hash to have a value passed in & run a function on it?
the program:
#!/usr/bin/perl -w use strict; use Data::Dumper; &main; sub main{ my $i = "x"; my @choices = ("a", "b", "c"); my %decision_hash = (a => \&mirror_input, b => \&return_reference, c => \&do_stuff); foreach my $case(@choices) { my @results = &{$decision_hash{$case}}($i); print "$case:"; print Dumper(@results); } } sub mirror_input { my $input = @_; print "input:"; print Dumper($input); print "\n"; return $input; } sub return_reference { my $input = @_; my %temp; $temp{1} = ["z", "x", "y"]; $temp{2} = ["s", "d", "f"]; return \%temp; } sub do_stuff { my $input = @_; my $new = "something new"; return ($new); }
here is the output:
input:$VAR1 = 1; a:$VAR1 = 1; b:$VAR1 = { 1 => [ 'z', 'x', 'y' ], 2 => [ 's', 'd', 'f' ] }; c:$VAR1 = 'something new';
I'm *really* hoping that someone will just tell me that i'm overlooking something incredibly obvious here.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How to pass a variable to function selected from a hash
by eg (Friar) on Jan 18, 2001 at 04:22 UTC | |
by arturo (Vicar) on Jan 18, 2001 at 04:26 UTC | |
by cat2014 (Monk) on Jan 18, 2001 at 04:27 UTC | |
by tilly (Archbishop) on Jan 18, 2001 at 06:25 UTC | |
by dkubb (Deacon) on Jan 18, 2001 at 12:51 UTC | |
by tilly (Archbishop) on Jan 18, 2001 at 13:10 UTC | |
Re: How to pass a variable to function selected from a hash
by chipmunk (Parson) on Jan 18, 2001 at 04:42 UTC | |
Re: How to pass a variable to function selected from a hash
by arturo (Vicar) on Jan 18, 2001 at 04:34 UTC |