Harry Putnam wrote:
I don't see the expected result when I press `y'.
you never print it
(The code is at the end)
It seems to do nothing.
...
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/