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 | |
|
Re^9: Getting an unknown error (thats just the way it is)
by Anonymous Monk on Apr 25, 2015 at 01:10 UTC |