http://www.perlmonks.org?node_id=1008830


in reply to Re: $1 not "freezing" in an addition
in thread $1 not "freezing" in an addition

ok I added the whole code

PS. Modifying your code, here is a simple version that reproduces the error:

sub butter { my $_ = shift; return $1 + butter($_) if s/(\d+)//; return 0; } print butter 'effect / 7ACV06';

Replies are listed 'Best First'.
Re^3: $1 not "freezing" in an addition
by Anonymous Monk on Dec 14, 2012 at 14:30 UTC

      "It ought to be easy to recognize an unquoted $1 as a function argument and issue a warning"

      The problem occurs in:

      return $1 + sub_that_does_regex_capturing(...);

      $1 is not a function argument in that. $1 hasn't been a function argument in any of the examples in this thread.

      The problem is writing an expression where one of the operands has a side-effect that can alter the value of another operand. This is very hard to detect through static analysis of source code; it's probably not especially easy to detect at run-time either. Heuristics may be able to catch some common cases.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        Interesting. This gave me the idea of trying this code:

        $_ = 1; print $_ + $_++;

        Amazingly enough, this prints 3, not 2

        But this does print 2:

        $_ = 1; print 0+$_ + $_++;

        The problem occurs in... This is very hard to detect through static analysis of source code; it's probably not especially easy to detect at run-time either.

        Yes, I recognized that, but since PPI certainly detects  $1 and s/// as $1 and substitution operator, I see no reason perlcritic couldn't do the same, it uses PPI

        I also recognize the abilities of warnings/lint, they actually run the code and can detect this case as well -- sure it might be too much to ask from warnings (could be slow) , but its not too much to ask of lint :)