Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^7: foreach $1

by LanX (Saint)
on Jun 08, 2020 at 23:40 UTC ( [id://11117840]=note: print w/replies, xml ) Need Help??


in reply to Re^6: foreach $1
in thread foreach $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

Replies are listed 'Best First'.
Re^8: foreach $1
by ikegami (Patriarch) on Jun 09, 2020 at 17:57 UTC

    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://11117840]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2024-03-28 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found