in reply to How to safely use $_ in a library function?
I think I like the basic approach of hdb here (elaborated by vsespb here) best, but if it's absolutely necessary to use $_ come Hell or high water, maybe something like:
>perl -wMstrict -le "for (qw(zik zok)) { print qq{for before: '$_'}; foo('wilma'); print qq{for after: '$_' \n}; } ;; sub foo { local $_ = 'fred'; sub { local $_ = 'XXX'; goto &bar; }->(@_); } ;; sub bar { my ($passed) = @_; print qq{gone from foo, passed = '$passed', \$_ = '$_'}; } " for before: 'zik' gone from foo, passed = 'wilma', $_ = 'fred' for after: 'zik' for before: 'zok' gone from foo, passed = 'wilma', $_ = 'fred' for after: 'zok'
Note that local-ization within the calling context of goto (i.e., within the sub { ... }) is still wiped out.
Note also: Try:
sub foo { for (qw(fred pebbles)) { sub { for (qw(hoo ha)) { goto &bar; } }->(@_); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to safely use $_ in a library function?
by perl-diddler (Chaplain) on Sep 18, 2013 at 18:28 UTC |