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

I am a Perl newbie, and I’m running a program and receiving the errors below. Has anyone encountered this before? If so, where am I going wrong?
"my" variable $txt masks earlier declaration in same scope at line 182 +. "my" variable $ct masks earlier declaration in same scope at line 185. "my" variable $ret masks earlier declaration in same scope at line 185 +. "my" variable $link masks earlier declaration in same scope at line 18 +5. "my" variable $link masks earlier declaration in same statement at lin +e 185. "my" variable $ret masks earlier declaration in same scope at line 186 +.

Replies are listed 'Best First'.
Re: "my" variable masks earlier declaration in same scope
by kyle (Abbot) on Aug 28, 2008 at 16:01 UTC

    You may have code like this:

    my $txt = 'hello world'; print "$txt\n"; # prints "hello world" my $txt = 'oh noes'; print "$txt\n"; # prints "oh noes"

    Instead, you may want this:

    my $txt = 'hello world'; print "$txt\n"; $txt = 'oh noes'; # note, no "my" print "$txt\n";

    It does the same thing, but it doesn't produce a warning. If you use diagnostics, it will tell you this about it:

    (W misc) A "my" or "our" variable has been redeclared in the current scope or statement, 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.
Re: "my" variable masks earlier declaration in same scope
by FunkyMonk (Bishop) on Aug 28, 2008 at 15:56 UTC
    You'll probably find Coping with Scoping by Dominus worth reading. Pay particular attention to the section called "Lexical Variables".
Re: "my" variable masks earlier declaration in same scope
by Skeeve (Parson) on Aug 28, 2008 at 15:52 UTC
    my $var; $var=1; my $var; $var=2;

    The second $var "masks earlier declaration in same scope". You declare the same variable name a second time.


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: "my" variable masks earlier declaration in same scope
by Anonymous Monk on Aug 29, 2008 at 06:41 UTC