Ireneusz Pluta <ipl...@wp.pl> writes: > Harry Putnam wrote: >> #!/usr/local/bin/perl >> >> use strict; >> use warnings; >> >> my $var1 = 'whoopdee'; >> my $var2 = 'do'; >> >> my %dispatch = ( >> y => \&yy($var1,$var2),
> this, actually, is not a code reference but a return value reference > of the &yy($var1, $var2) subroutine call, executed right at the time > of %dispatch assignment. By using (...) after a subroutine name you > have the subroutine executed and get its return value. Then, the > leading '\' in this syntax makes a reference to that return value. Thanks. I'm not sure I really understand, why is the subroutine NOT called immediately if (..) is not present? I guess what I'm asking is how is it that: y => \&yy, Is not executed but: y => \&yy($var,$var), is executed? If you remove the reference notation in other code like this: my $it = &yy; my $it = yy($var,$var); Either one would execute immediately right? > To see what has happened, try to run your code in debug mode (perl -d) > and view the contents of your %dispatch hash, or place > > use Data::Dumper; > print Dumper { %dispatch }; Thats a good tip... thanks. I recall having seen this mentioned before but I didn't understand what it meant. I see now how you can tell that \&yy($var,$var) executed immediately. [...] > 'y' => \'You pressed `y\',whoopdee do .. but why? [...] >> my $code = $dispatch{$selection} || $dispatch{'error'} ; >> $code->(); > this is the place you really want to call your subroutine, so also the > place to pass your arguments to it: > > $code->($var1, $var2); >> } Ok, thanks, I hadn't understood how that notation `$code->()' worked. I started by copying a googled example of a dipatch table and tried to edit it for my own use, but I have A LOT of trouble following along in code... it seems some of the posters here read it like a book. Hopefully I will be able to eventually too. However, using your suggestion: > $code->($var1, $var2); and the Dumper lines: use Data::Dumper; print Dumper { %dispatch }; I don't see the expected result when I press `y'. (The code is at the end) It seems to do nothing. The dump shows: $VAR1 = { 'y' => sub { "DUMMY" }, 'n' => sub { "DUMMY" }, 'q' => sub { "DUMMY" }, 'error' => sub { "DUMMY" } }; I guess it just shows that nothing happens... But the code is at the end... no doubt riddled with more mistakes. >> sub yy { >> my ($var1,$var2); >> ($var1,$var2) = (shift,shift); > why not: > > my ($var1, $var2) = @_; I guess I wasn't sure that would get the right result but knew ($var1,$var2) = (shift,shift); would. Your example is simpler and more obvious. Thanks > my $var1 = shift; > my $var2 = shift; Is that actually better in some way? Or do you just mean it looks nicer? ------- --------- ---=--- --------- -------- non working code: #!/usr/local/bin/perl use strict; use warnings; my $var1 = 'whoopdee'; my $var2 = 'do'; my %dispatch = ( y => \&yy, n => sub { print "You pressed \`n' \n";}, q => sub { print "Goodbye\n" and exit;}, error => sub { print "invalid selection\n" } ); use Data::Dumper; print Dumper { %dispatch }; while(1) { print "press y \n", "press n \n", "press q to Exit\n"; chomp(my $selection = <STDIN>); my $code = $dispatch{$selection} || $dispatch{'error'} ; $code->($var1,$var2); } sub yy { my ($var1,$var2); ($var1,$var2) = @_; my $retstr = sprintf "You pressed \`y',$var1 $var2 .. but why?\n"; return $retstr; } ------- --------- ---=--- --------- -------- output from running: ./dispatch (the yy() function doesn't do anything. $VAR1 = { 'y' => sub { "DUMMY" }, 'n' => sub { "DUMMY" }, 'q' => sub { "DUMMY" }, 'error' => sub { "DUMMY" } }; press y press n press q to Exit y press y press n press q to Exit n You pressed `n' press y press n press q to Exit q Goodbye -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/