I think what's going on is that since @_ are aliases, this actually clears out the argument as well.

That would be the behaviour you'd get if local simply backed up the current value of a variable and restored it on scope exit.

As you discovered, that's not what local does. Instead, it creates a new variable, aliases the name to the new variable, and aliases the name back to the original variable on scope exit.

$ perl -e' use feature qw( say ); say 0+\$x; { local $x; say 0+\$x; } say 0+\$x; ' 94031376424552 94031376229560 94031376424552

That means the fact that $_[0] is an alias for the localized variable shouldn't matter. And it normally doesn't.

$ perl -e' use feature qw( say ); $x = 2; say 0+\$x; say $x; sub { local $x = 3; say 0+\$x; say $x; say $_[0]; }->( $x ); say 0+\$x; say $x; ' 94300947464040 Address of pre-local $x 2 Value of pre-local $x 94300947268840 Address of post-local $x 3 Value of post-local $x 2 Value of pre-local $x 94300947464040 $x is restored 2 Value of pre-local $x

But $! is magical. And more specifically, it's a proxy for a C variable. local copies the magic onto the new scalar it creates. So you end up with two variables (the $! passed as argument and the new $! created by local) which both use the same C variable for storage.

$ perl -e' use feature qw( say ); $! = 2; say 0+\$!; say 0+$!; sub { local $! = 3; say 0+\$!; say 0+$!; say 0+$_[0]; }->( $! ); say 0+\$!; say 0+$!; ' 93839899443048 Address of pre-local $! 2 Value of pre-local $! (aka value of errno) 93839899247848 Address of post-local $! 3 Value of post-local $! (aka value of errno) 3 Value of pre-local $! (aka value of errno) 93839899443048 $! is restored 3 Value of pre-local $! (aka value of errno)

The net effect is that local $! doesn't really localize $! at all!

$? is similarly a proxy for a C variable. $@ isn't magical.


In reply to Re^2: Carp: $! vs "$!" in croaking (updated) by ikegami
in thread Carp: $! vs "$!" in croaking by cavac

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.