Harry Putnam wrote:
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?
The present of the parenthesis tells the parser that you want the
subroutine called and a reference to is returned value stored in the hash.
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?
Yes.
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...
Not exactly. Data::Dumper places a dummy sub in its output since it
can't disassemble the code. See "BUGS" in `perldoc Data::Dumper`.
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?
my $var1 = shift @_; # $var1 contains foo
my $var2 = shift @_; # $var2 contains bar
# One variable per line is a good way to comment on them
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
I like Perl; it's the only language where you can bless your
thingy.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/