Re: same variable defined twice

[email protected] (Rob Dixon)
Newsgroups perl.beginners
Message-ID <[email protected]>
Octavian Rasnita wrote:
> 
> I've tried the following script and it works fine:
> 
> use strict;
> my $text = 1;
> my $text = 2;
> print $text;
> 
> Shouldn't perl disallow defining the $text variable a second time in the same
> script if using "use strict"?

Lexical variables are intended for temporary use in a limited scope. Using them
properly alleviates the burden of finding multiple names for variables with an
identical purpose in different scopes.

Declaring a variable with the same name in the same scope is valid semantically
(it discards the previous variable and provides a new one) but is usually a
mistake, and compiling the program with warnings enabled will generate the message

  "my" variable $var masks earlier declaration in same scope

You should always enable lexical warnings as well as strictures.

HTH,

Rob
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.