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; } }->(@_); } }

In reply to Re: How to safely use $_ in a library function? by AnomalousMonk
in thread How to safely use $_ in a library function? by perl-diddler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.