in reply to foreach $1

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

Replies are listed 'Best First'.
Re^2: foreach $1
by kcott (Archbishop) on Jun 06, 2020 at 17:50 UTC

    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

        Yes, $a and $b seem to be OK. $" also seems to work, but $\ doesn't:
        for $\ (qw( a b c d )) { print "@{[1, 2, 3]}"; } # Output: # 1 2 31 2 31 2 31 2 3
        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^2: foreach $1
by ikegami (Patriarch) on Jun 08, 2020 at 22:25 UTC

    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