in reply to Re^6: Getting an unknown error
in thread Getting an unknown error

Wow, what amazing discovery, somebody should perlbug this in a hurry

The bug is that it breaks $1 and m//atching operator, and it doesn't warn...

$ perl -wle " $_ = 333; for $1 ( 666 ){ print $1 if /(...)/; }" 666 $ perl -wle " $_ = 333; for $1 ( 666 ){ local our $1; print $1 if /(. +..)/; }" Use of uninitialized value $1 in print at -e line 1.

No warnings are issued, matching is broken in a different scope, its a bug ... kind of amazing people don't trip on it every day

Kinda related
Lexical $_ in given/when vs. BLOCK arguments
 Bug #115834 for perl5: Successfull match $_[0] =~ /foo(.+)/ overrides $_[0] if function called as foo($1)

Replies are listed 'Best First'.
Re^8: Getting an unknown error (foreach $1 breaks m//atch)
by ww (Archbishop) on Apr 25, 2015 at 00:44 UTC

    Re Re^7: Getting an unknown error (foreach $1 breaks m//atch) by an Anonymous Monk: perldoc perlvar again...

    $_ is by default a global variable. However, as of perl v5.10.0, you can use a lexical version of $_ by declaring it in a file or in a block with my. Moreover, declaring our $_ restores the global $_ in the current scope. Though this seemed like a good idea at the time it was introduced, lexical $_ actually causes more problems than it solves. If you call a function that expects to be passed information via $_ , it may or may not work, depending on how the function is written, there not being any easy way to solve this. Just avoid lexical $_ , unless you are feeling particularly masochistic.
Re^8: Getting an unknown error (foreach $1 breaks m//atch)
by AnomalousMonk (Archbishop) on Apr 24, 2015 at 15:27 UTC
    ... 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:  <%-(-(-(-<

      Using normal variables instead of numbered ones works, too:
      for $1 (666) { if (my ($match) = 333 =~ /(...)/) { print "$match\t$1\n"; } }
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      ...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.