Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Explanation of warning created when over using "my"

by Tatnall (Beadle)
on Sep 21, 2006 at 14:53 UTC ( [id://574140]=perlquestion: print w/replies, xml ) Need Help??

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

Often, I add "my" to variables that have previously been declared generating this warning: "my" variable $pet masks earlier declaration in same scope.

I use "my" again to 'clobber' the old value, I could use undef($pet); too I guess.

In perldiag I found this:

(W) A lexical variable has been redeclared in the same scope, effectively eliminating all access to the previous instance. This is almost always a typographical error. Note that the earlier variable will still exist until the end of the scope or until all closure referents to it are destroyed.

I understand what the warning means, but I don't understand why it generates a warning

I'm missing something important. Is it bad coding style? Does it take up more memory? What are the drawbacks to redeclaring vesus undefining and then using the variable again?

Example:
#!/usr/bin/perl use strict; use warnings; my $pet = 'dog'; my $pet = 'cat'; my $pet = 'ferret';

Thanks, Tatnall

"Recognizing who we aren't is only the first step toward knowing who we are." - Os Guinness

Replies are listed 'Best First'.
Re: Explanation of warning created when over using "my"
by davorg (Chancellor) on Sep 21, 2006 at 15:05 UTC

    Well the extended message that you quote seems clear enough in explaining that the earlier instance of the variable still exists and still takes up memory. You just can't access it. That would seem to me to be enough of a reason not to do this.

    But you also need to think about what you're trying to do here. When you declare a variable, it should be for as small a scope as possible and each variable you declare should have only one use.

    What are you really trying to achieve by redeclaring the variable? If you just want to put a new value into it then just overwrite the existing value. If you want to reset the variable to an undefined state then allocate undef to it. But if you're doing this to indicate that you're using the variable for a different purpose now, then that's a bad idea and you should be declaring a new variable with a different name.

    So redeclaring a variable within a scope like this is a bad practice - partly because, yes, you are wasting a small amount of memory, but most importantly because it's unclear what you are trying to indicate by doing this and it is very likely to confuse your maintenance programmer.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      daveorg,

      Thank you for the very detailed answer! You provided a wonderful reply.

      But if you're doing this to indicate that you're using the variable for a different purpose now, then that's a bad idea and you should be declaring a new variable with a different name.

      That piece hit me between the eyes. Often, I'm using "my" to indicate a new purpose.

      Thanks, Tatnall

      "Recognizing who we aren't is only the first step toward knowing who we are." - Os Guinness
Re: Explanation of warning created when over using "my"
by liverpole (Monsignor) on Sep 21, 2006 at 15:12 UTC
    Hi, Tatnall,

    Simply put, you may be overwriting a variable that don't want to.

    Consider:

    use strict; use warnings; my $x = very_important_number_calculation(); printf "Your very important value is $x\n"; # time passes ... my $x = temporary_value(); my $y = temporary_value(); printf "(x + y) = ($x + $y) = ", $x + $y; # more time passes ... print "Your very important number calculation yielded: $x\n";

    See how that could ruin your day if you weren't warned about it?

    There's a simple solution if you're sure you want to reuse the variable, though.  Just do it, but omit the my after the first time:

    #!/usr/bin/perl use strict; use warnings; my $pet = 'dog'; $pet = 'cat'; $pet = 'ferret';

    And I wouldn't call that "bad programming practice".  It's clear that you're intentionally resetting the variable to different values, and there's nothing (inherently) wrong with that.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Explanation of warning created when over using "my"
by derby (Abbot) on Sep 21, 2006 at 15:07 UTC

    You don't you just use *assingment* to clobber an old value?

    my $pet = 'dog'; ... $pet = 'cat'; ... $pet = 'ferret';

    -derby
Re: Explanation of warning created when over using "my"
by lyklev (Pilgrim) on Sep 21, 2006 at 16:06 UTC
    Try this:

    use strict; use warnings; my $a = 1; # a simple variable my $ref_a = \$a; # refers to the original $a my $a = 5; # obscures the old $a, but # ref_a still points to the old $a print "'a' = $a; 'ref_a' points to ", ${$ref_a}, "\n";

    Now of course you see what to expect, but in a larger program this behaviour can leave you puzzled. You should of course have created a new variable instead.

Re: Explanation of warning created when over using "my"
by graff (Chancellor) on Sep 21, 2006 at 22:09 UTC
    Sometimes I wish that Perl could be even more sensitive about this, and issue the same sort of warning for cases like:
    my $variable = "current value"; while ( whatever() ) { ... my $variable = "new value"; # if "my" was just typed by habit here ... } print $variable; # then there's probably a bug when we get here
    At present, perl does not warn about that sort of situation, and on the whole, it's probably better that it doesn't. So I just have to watch myself...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-18 04:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found