Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^5: foreach $1

by LanX (Saint)
on Jun 07, 2020 at 02:19 UTC ( [id://11117772]=note: print w/replies, xml ) Need Help??


in reply to Re^4: foreach $1
in thread foreach $1

> $" also seems to work, but $\ doesn't:

ehm ... that's surprising

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^6: foreach $1
by ikegami (Patriarch) on Jun 08, 2020 at 22:12 UTC

    Aside from the fact that Perl itself uses them, $_, $a, $b and $" aren't special.[1] They are just ordinary package variables.

    $/ and $\ are special: They are magical vars. I don't know why they are magical vars.

    $1 is special: It's a magical var. It's also implicitly localized.

    $ perl -MDevel::Peek -e' for my $sym (qw( _ a " / \ 1 )) { warn "\$$sym\n"; Dump($$sym); warn "\n"; } ' $_ SV = NULL(0x0) at 0x7fffc959c850 REFCNT = 1 FLAGS = () $a SV = NULL(0x0) at 0x7fffc95545e0 REFCNT = 1 FLAGS = () $" SV = PV(0x7fffc9554d40) at 0x7fffc95796f0 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x7fffc957e030 " "\0 CUR = 1 LEN = 10 $/ SV = PVMG(0x7fffc95732b0) at 0x7fffc95544f0 REFCNT = 1 FLAGS = (GMG,SMG,POK,pPOK) IV = 0 NV = 0 PV = 0x7fffc9574ba0 "\n"\0 CUR = 1 LEN = 10 MAGIC = 0x7fffc9574b40 MG_VIRTUAL = &PL_vtbl_sv MG_TYPE = PERL_MAGIC_sv(\0) MG_OBJ = 0x7fffc95544d8 MG_LEN = 1 MG_PTR = 0x7fffc9574b80 "/" $\ SV = PVMG(0x7fffc95b9a80) at 0x7fffc95797f8 REFCNT = 1 FLAGS = (GMG,SMG) IV = 0 NV = 0 PV = 0 MAGIC = 0x7fffc957aba0 MG_VIRTUAL = &PL_vtbl_sv MG_TYPE = PERL_MAGIC_sv(\0) MG_OBJ = 0x7fffc95797e0 MG_LEN = 1 MG_PTR = 0x7fffc958fb70 "\\" $1 SV = PVMG(0x7fffc9573a00) at 0x7fffc9584420 REFCNT = 1 FLAGS = (GMG,SMG) IV = 0 NV = 0 PV = 0 MAGIC = 0x7fffc9591e50 MG_VIRTUAL = &PL_vtbl_sv MG_TYPE = PERL_MAGIC_sv(\0) MG_OBJ = 0x7fffc9584408 MG_LEN = 1

    1. local $_ is special, though.

    Update: Added mention that local $_ is special and that $1 is implicitly localized. Added code.

      Oh, and why does that matter? Well, since $" isn't special, Perl actually uses $" when you interpolate an array in double-quotes. It doesn't matter that the loop made $" refer to a different SV. But it does matter for magical vars. Since the SV that replaces $\ doesn't have the magic that $\, the underlying variable that Perl actually uses (instead of $\) isn't updated when $\ is changed.

      $ perl -MDevel::Peek -e'Dump($\); for $\ (1) { Dump($\); }' SV = PVMG(0x7fffc3c4baa0) at 0x7fffc3c15ab0 REFCNT = 1 FLAGS = (GMG,SMG) IV = 0 NV = 0 PV = 0 MAGIC = 0x7fffc3c3b9e0 MG_VIRTUAL = &PL_vtbl_sv MG_TYPE = PERL_MAGIC_sv(\0) MG_OBJ = 0x7fffc3c15a20 MG_LEN = 1 MG_PTR = 0x7fffc3c3c4f0 "\\" SV = IV(0x7fffc3c15a40) at 0x7fffc3c15a50 REFCNT = 2 FLAGS = (IOK,READONLY,PROTECT,pIOK) IV = 1
      > $_, $a, $b and $" aren't special.

      Where do you get this definition of "special" from, they are listed under perlvar#SPECIAL-VARIABLES.

      The docs specify the terminology

      > $/ and $\ are special: They are magical vars. ... $1 is special: It's a magical var

      OK, so your point seems to be that "magical vars", i.e. variables with a MAGIC attribute fail to work as expected as loop variables.

      So you agree that at least those vars should raise a warning in this case?

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Where do you get this definition of "special" from

        They are special in the sense that they are used by some Perl builtins, but the scalar itself is just an ordinary scalar. You can check using Devel::Peek's Dump. (As shown.)

        So you agree that at least those vars should raise a warning in this case?

        No. Warning when a magical var is used as the loop iterator would issue far too many incorrect warnings. Most of the time, the fact that the aliased variable doesn't have the original magic is a good thing.

        sub g { for (qw( abc def )) { # $ENV{PATH} didn't get changed. } } sub f { for (@_) { # $_ now aliased to $ENV{PATH} g(); } } f($ENV{PATH}); # A magical var

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11117772]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 13:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found