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
|
|---|
| 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 | |
by Tatnall (Beadle) on Sep 21, 2006 at 15:19 UTC | |
|
Re: Explanation of warning created when over using "my"
by liverpole (Monsignor) on Sep 21, 2006 at 15:12 UTC | |
|
Re: Explanation of warning created when over using "my"
by derby (Abbot) on Sep 21, 2006 at 15:07 UTC | |
|
Re: Explanation of warning created when over using "my"
by lyklev (Pilgrim) on Sep 21, 2006 at 16:06 UTC | |
|
Re: Explanation of warning created when over using "my"
by graff (Chancellor) on Sep 21, 2006 at 22:09 UTC |