Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

foreach $1

by choroba (Cardinal)
on Jun 05, 2020 at 21:49 UTC ( [id://11117743]=perlmeditation: print w/replies, xml ) Need Help??

When answering a question on StackOverflow, I noticed an interesting anti-idiom (antidiom?) I haven't seen before (and haven't thought of trying):
foreach $1 (@array)

Interestingly, you can use it under strict and warnings and Perl doesn't protest. But $1 is aliased to the elements of the array and loses its magic behaviour:

#!/usr/bin/perl use warnings; use strict; for $1 ('a' .. 'h') { print $1 if "A" =~ /(.)/; }

Can you guess what the output is?

abcdefgh

So, if you ever get the idea of using special variables for something else than what they were designed to, stop.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re: foreach $1
by LanX (Saint) on Jun 06, 2020 at 12:29 UTC
    Interesting, thanks for showing! ++

    Can anyone guess of a special var other than $_ where aliasing might make sense?

    Otherwise shouldn't we make a feature request to throw a compilation error?

    (At least under strict or warnings?)

    edit

    Hmm ...

    Should be noted here that a special var makes the whole typeglob special.

    I.e. if $a is a special var then @a and %a will act special too, even without carrying a meaning.

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

      G'day Rolf,

      "Can anyone guess of a special var other than $_ where aliasing might make sense?"

      I made a quick scan through perlvar and nothing leapt out at me as being appropriate for this.

      "Otherwise shouldn't we make a feature request to throw a compilation error? (At least under strict or warnings?)"

      I'm not sold on making it a fatal error but I do think a warning may be appropriate. I had a look at "warnings: Category Hierarchy" but didn't think any existing category was really suitable: maybe create a new top-level category or a new sub-category of "syntax".

      — Ken

        Well I'm not sure yet.

        I've tested with $a and the loop var didn't interfere with the sort block.

        Probably we need to decide case by case...

        Though my gut feeling says all non alphabetic symbols should cause a warning when used as a loop alias.

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

      if $a is a special var then @a and %a will act special too, even without carrying a meaning.

      Well, they are considered "declared", but that's the extent of it.

        > I.e. if $a is a special var then @a and %a will act special too, even without carrying a meaning.

        > > Well, they are considered "declared", but that's the extent of it.

        Nope, e.g. $% is a special var which is global, @% isn't documented anywhere, but still global

        DB<1> x @%=1..3 0 1 1 2 2 3 DB<2> x {package A; @%} 0 1 1 2 2 3 DB<3> x @a=1..3 # not global 0 1 1 2 2 3 DB<4> x {package A; @a} empty array DB<5>

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://11117743]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-20 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found