Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

ptkdb: 'my' variable in sub retains value between calls

by leonidlm (Pilgrim)
on Aug 10, 2008 at 15:27 UTC ( [id://703426]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all again :)
How much things you can lern by using ptkdb!
I am running a big perl program with this beautiful debugging module and I noticed something I want someone here to clarify for me:
When I declare a sub routine and there I write:
my $a,$b; while ( somethingHere ) { $b .= "s"; }
If I run this subroutine couple of times the $b value is saved from call to call !!!!
How can it be? I am using a "my" declaration, it should be cleared as fast as the subroutine will end!
Someone please help me to understand this perl's behavior.

Replies are listed 'Best First'.
Re: ptkdb: 'my' variable in sub retains value between calls
by betterworld (Curate) on Aug 10, 2008 at 15:53 UTC
    If you want to declare two my-variables, use either of the following:
    my ($a, $b); my $a; my $b; my $a, my $b;

    By the way, the usage of the variables $a and $b for something other than sort is generally frowned upon. Probably that's why strict (if you use it) did not complain that the variable $b is not declared: Because it's a special variable and it's declared internally.

Re: ptkdb: 'my' variable in sub retains value between calls
by FunkyMonk (Chancellor) on Aug 10, 2008 at 16:07 UTC
    my $a,$b; doesn't declare $b, only $a. Had you included
    use strict; use warnings;

    at the start of the program (see strict and warnings), perl would have told you "Parentheses missing around "my" list ...". The correct way is my ($a, $b);. But, $a and $b aren't good names to use as they are somewhat magical for their use with sort. You should choose more meaningful names.


    Unless I state otherwise, all my code runs with strict and warnings
      Thank you all. The variable names are just for an example, of course I used a proper names in my program, it is more than 2000 lines, I don't think I would manage elsewhere.

        $foo and $bar are good time honored place holder names for variables and are almost as easy to type as $a and $b.


        Perl reduces RSI - it saves typing
        If, in addition to using warnings with its very helpful message referred to in other replies, you had been using strict, Perl would never have let you make the mistake in the first place if you were using names other than $a and $b!
Re: ptkdb: 'my' variable in sub retains value between calls
by Herkum (Parson) on Aug 10, 2008 at 16:05 UTC

    Don't use $b, in fact don't use single letter variable names, EVER!. Besides $a, $b being special Perl variables for use with sorting, it is totally ambiguous. It does not tell you want is supposed to be there and creates maintenance headaches.

    Be polite and spell out what the variable is supposed to represent and the next time you look at this code, you will be able to quickly identify what it is supposed to do.

      Never say "never" (or "ever") in programming. Never using single letter variables would mean you wouldn't use $_. Or for that matter, $i, $j, etc.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://703426]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-18 22:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found