in reply to Re^7: Getting an unknown error (foreach $1 breaks m//atch)
in thread Getting an unknown error

... it breaks $1 and m//atching operator ... No warnings are issued, matching is broken in a different scope ...

It certainly aliases  $1 and this is certainly not a good idea, but matching is not broken. (Please see example code below.) A match is made properly and the truth of the match is returned. The content of capture group 1 cannot be directly accessed (because the  $1 symbol has been aliased to something else), but the content of this group is still proper and can be accessed indirectly if a kind of "preemptive strike" aliasing of  $1 (or rather of the data it symbolizes) has been done. Other variables associated with this group still function as expected. I don't see your point about matching in a different scope (which I take to mean outside the scope of the for-loop alias) being broken; again, see code below. As to warnings... well, as weird as this (ab)use of  $1 is, in programmng as in life, it's just not possible to warn about all the weird things one might do; at some point one must fall back upon common sense.

c:\@Work\Perl\monks>perl -wMstrict -le "for my $one ($1) { $_ = 'xx333333xx'; for $1 (666) { print qq{A: '$one' $-[1] $+[1] '$2'} if /(333)\1(xx)/; print qq{\$1 '$1'}; } } ;; print qq{B: '$1' $-[1] $+[1] '$2'} if /(\d\d\d)\1(\D\D)/; " A: '333' 2 5 'xx' $1 '666' B: '333' 2 5 'xx'

Bottom line: This is just the way aliasing works (insofar as I understand it), but you don't have to use something just because it works. Again, common sense.


Give a man a fish:  <%-(-(-(-<

Replies are listed 'Best First'.
Re^9: Getting an unknown error (foreach $1 breaks m//atch)
by choroba (Cardinal) on Apr 24, 2015 at 15:39 UTC
    Using normal variables instead of numbered ones works, too:
    for $1 (666) { if (my ($match) = 333 =~ /(...)/) { print "$match\t$1\n"; } }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^9: Getting an unknown error (thats just the way it is)
by Anonymous Monk on Apr 25, 2015 at 01:10 UTC

    ...It certainly aliases $1 and this is certainly not a good idea, but matching is not broken. (Please see example code below.) ... This is just the way aliasing works

    this is just the way something works is the weakest of argument ...

    AnomalousMonk , its a bug, warnings doesn't warn

    It never occurred to me to try using $1 for anything other than retrieving regex matches

    It never even occurred to me to try $1='something' but you can't do that

    This is not a feature, its just an oversight

    warnings should help the idiot who uses $1 for something other than retrieving regex matches

    documentation updates are not any kind of solution

    $ perl -le" for my $1 ( 666 ){ print $1 } " Can't use global $1 in "my" at -e line 1, near "my $1 " Execution of -e aborted due to compilation errors.