zorglups has asked for the wisdom of the Perl Monks concerning the following question:
A little question on anonymous subroutines ?
How to declare the sub so that the $parameter gets evaluated at the declaration ?
In a few words, with the following code, I would like to have $print_ref->() always print "param at declaration".
Thanks,
Pierre
#!/usr/bin/perl use strict; my $parameter = "param at declaration\n"; my $print_ref = sub { print $parameter }; $print_ref->(); # prints: param at declaration $parameter = "param after declaration\n"; $print_ref->(); # prints: param after declaration exit;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to get variable evaluation in anonymous subroutines declaration
by Corion (Patriarch) on Jan 21, 2009 at 15:14 UTC | |
by zorglups (Acolyte) on Jan 21, 2009 at 15:45 UTC | |
|
Re: How to get variable evaluation in anonymous subroutines declaration
by AnomalousMonk (Archbishop) on Jan 21, 2009 at 17:34 UTC | |
|
Re: How to get variable evaluation in anonymous subroutines declaration
by kyle (Abbot) on Jan 21, 2009 at 15:32 UTC | |
by zorglups (Acolyte) on Jan 21, 2009 at 15:48 UTC | |
|
Re: How to get variable evaluation in anonymous subroutines declaration
by mpeever (Friar) on Jan 21, 2009 at 20:27 UTC | |
|
Re: How to get variable evaluation in anonymous subroutines declaration
by ig (Vicar) on Jan 21, 2009 at 21:06 UTC |