Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: $1 not "freezing" in an addition

by Anonymous Monk
on Dec 14, 2012 at 12:27 UTC ( #1008825=note: print w/replies, xml ) Need Help??

in reply to $1 not "freezing" in an addition

Prove it :)

$ perl -Mstrict -Mwarnings -l junk 13 $ cat junk sub butter { local $_ = 'effect / 7ACV06'; return $1 + junk($_) if s/(\d+)//; } sub junk { local $_ = $_[0]; return $1 if s/(\d+)//; } print butter(); $

Replies are listed 'Best First'.
Re^2: $1 not "freezing" in an addition
by grondilu (Friar) on Dec 14, 2012 at 12:40 UTC

    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';

        "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'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1008825]
and the fire pops...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2018-05-22 14:20 GMT
Find Nodes?
    Voting Booth?