in reply to Explanation of warning created when over using "my"

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

  • Comment on Re: Explanation of warning created when over using "my"

Replies are listed 'Best First'.
Re^2: Explanation of warning created when over using "my"
by Tatnall (Beadle) on Sep 21, 2006 at 15:19 UTC

    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