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
In reply to Explanation of warning created when over using "my" by Tatnall
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |