Harry Putnam <[email protected]> asked:
> But even though I can work with that... I'm still not seeing why the
> values in @_ are not available at the call to N() inside like this:
> (incomplete code)
>
> func($val1,$val2);
>
> sub func { %h = ( N => sub { print N(@_) . "\n"; } ); }
If you call the sub like this, it'll create the hash %h containing the key "N"
associated with a code reference to an anonymous subroutine. When that
subroutine is called it will pass its _current_ argument list to function N,
then print the returned values from that function and a line feed.
If you wanted that code reference to be called with the values of @_ at the
time of its creation, you'd have to use a closure and keep a copy of the values
in a lexical variable, probably a bit like this:
#!/usr/bin/perl -w
use strict;
sub label_closure {
my $label = shift;
return sub {
my $name = shift;
print "$name, you're a $label!\n";
}
}
my $laud = label_closure("genius");
$laud->('MJD');
__END__
HTH,
Thomas
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/