Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: $1 not "freezing" in an addition

by CountZero (Bishop)
on Dec 14, 2012 at 17:13 UTC ( #1008883=note: print w/replies, xml ) Need Help??

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

Without having tested this, could it be that $1 being a global variable, is continously overwritten in each next iteration of this recursive subroutine, until the last step where $1 becomes undef due to the regex failing? That undef value then "trickles" back up the recursive subs and gives this error.

By "interpolating"/"quoting" the $1 perhaps the acctual value of $1 at that stage in the recursion gets indeed saved somehow.

It is always a good idea to immediately save those special variables (such as $_, @_, $1, ...) into a lexical variable to avoid these pitfalls. We tend to forget that these are global variables that are therefore prone to "action at a distance".


A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: $1 not "freezing" in an addition
by Anonymous Monk on Dec 14, 2012 at 17:29 UTC
    I think the logic is the same as using Foo($1,$2) versus Foo("$1","$2"), it seems its a good habit to quote $1,$2 when you're not doing assignment

      it seems its a good habit The safest thing you can do is to stringify $1,$2 when you're not doing assignment using them in an lvalue context.

      f("$1") for ("$1") \"$1"

        The safest thing you can do is to stringify $1,$2 when them in an lvalue context.

        I'm not sure that is much clearer. perlglossary lists lvalue but not lvalue context, but when you compare context, scalar context and list context, and plug-in lvalue, it doesn't quite work , unless you lookn up expression and then value.

        Putting all that together assignment is also lvalue-context, but I wouldn't recommend needlesly quoting $1 in  my $one = $1;

        So I might rephrase as

        When using $1,$2... in an expression ( anything you can legally say in a spot where a value is required ) you should quote to stringify and preserve the current value.

        foo("$1") instead of foo($1)

        $bar = "$1" + foo(); instead of $bar = "$1" + foo();

        No need to quote straight assignment  if(m/.../){  $bar = $1; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1008883]
erix joining the EU after all

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2017-10-17 10:36 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (225 votes). Check out past polls.