sachin_chat has asked for the wisdom of the Perl Monks concerning the following question:

in my script there is a variable , say temp. i have initialized it to 1. my script calls itself recursively. in my script i change that variable to 2 depending on some condition. now second time when script is called recursively again it will be initialized to 1 . what i want is that second time when i call script the previously changed value shuld stay because depending on that i wil do some other execution. even i tried with environment variable instead of simple variable but same thing is happening.
  • Comment on want to create a variable whise value do not get change on recursion

Replies are listed 'Best First'.
Re: want to create a variable whise value do not get change on recursion
by holli (Abbot) on Feb 03, 2005 at 13:19 UTC
    You will have to "feed" the value of the variable to the recursion, e.g. using a command-line argument.
    use strict; my $temp = shift @ARGV || 0; print "call no. $temp\n"; if ( $temp < 10 ) { system ("rec.pl", $temp+1); }
    This will print
    call no. 0 call no. 1 call no. 2 call no. 3 call no. 4 call no. 5 call no. 6 call no. 7 call no. 8 call no. 9 call no. 10

    holli, regexed monk
Re: want to create a variable whise value do not get change on recursion
by Corion (Patriarch) on Feb 03, 2005 at 13:18 UTC

    I think you need to show us some code. My guess is that you are using some kind of global variable, which means that you will need to rewrite your code to save the old value, set the new value, recurse, and then restore the old value again. The following Perl code works fine with recursing and automatically resets the old value:

    use strict; print ackermann(3,3); sub ackermann { my ($left,$right) = @_; print "$left,$right\n"; if ($left == 0) { return $right+1 } elsif ($right == 0) { return ackermann($left-1, 1) } else { return ackermann($left-1, ackermann($left, $right-1)) }; };

    The trick is, that this code does not use respectively change any global variables, and thus has no problems invoking itself.

Re: want to create a variable whise value do not get change on recursion
by monkey_boy (Priest) on Feb 03, 2005 at 13:17 UTC
    Can we have a look at the script?

    It sounds like you want to pass the variable value as an argument, as each time the script is executed its a new process with no "memory" of what went on before.



    Im so sick of my Signature...
Re: want to create a variable whise value do not get change on recursion
by borisz (Canon) on Feb 03, 2005 at 13:13 UTC
    Propably you can use
    $temp ||= 1;
    Then $temp is only initialized if it is undef or 0.
    Boris
Re: want to create a variable whise value do not get change on recursion
by bart (Canon) on Feb 03, 2005 at 13:21 UTC
    ...when script is called recursively
    ??? I don't know what exactly you mean by that, but I am quite wary of what it might mean.

    Independent scripts simply don't share variables.

      Indeed, that sentence confused me. The OP might have meant when the function is called recursively ... Here is some code for the OP that shows how to maintain a "global" variable, but not how to share variables between runs of a script.

      #!/usr/bin/perl use strict; use warnings; our $outer = 5; recurse($outer); sub recurse { my $inner = shift; $inner--; return if $inner < 0; print "outer: $outer\ninner: $inner\n"; recurse($inner); }

      UPDATE: You make it seem so crystal clear, holli. Oh well, here is a recursive sub in case someone needs it. :D

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        The OP said my script calls itself recursively. I think that has be to taken as it was said. There may be a good reason to do that.

        holli, regexed monk
      Yes, wary is the word, if the script get posted im expecting to see the dreaded exec in there ...




      Im so sick of my Signature...
Re: want to create a variable whise value do not get change on recursion
by data64 (Chaplain) on Feb 03, 2005 at 13:24 UTC
Re: want to create a variable whise value do not get change on recursion
by ccn (Vicar) on Feb 03, 2005 at 13:17 UTC

    You can store your variable in a file. Start your program with any "invalid" value of the variable, e.g. undef. Then initialize it only if it's value is "invalid".

    $temp = read_from_file(); $temp = 1 unless defined $temp; store_to_file($temp);