in reply to How to get variable evaluation in anonymous subroutines declaration

Corion is correct. If, for some reason, you don't want to define a sub that does this, you can use an anonymous sub the same way.

my $parameter = qq{param at declaration\n}; my $print_ref = sub { my ($p) = @_; sub { print $p } }->( $parameter ) +; $print_ref->(); $parameter = qq{param after declaration\n}; $print_ref->(); __END__ param at declaration param at declaration

I wouldn't recommend this for anything more complicated than a single line, however. I wouldn't want to read to the end of a block of code to find "->(...)" dangling off of it.

Replies are listed 'Best First'.
Re^2: How to get variable evaluation in anonymous subroutines declaration
by zorglups (Acolyte) on Jan 21, 2009 at 15:48 UTC
    This worked as well. I will stick to Corion's suggestion because i would like to be able to understand my code when I will have to read it next time ;-)

    Thanks a lot for the comment.