Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Why no warning when redclaring a variable in same scope

Reply
Thread Tools

Why no warning when redclaring a variable in same scope

 
 
Mintcake
Guest
Posts: n/a
 
      09-21-2007
#!/usr/local/bin/perl

use strict;
use warnings;

for my $i (1..10)
{
my $i = 0; # Why no warning
}

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      09-21-2007
Mintcake wrote:
> #!/usr/local/bin/perl
>
> use strict;
> use warnings;
>
> for my $i (1..10)
> {
> my $i = 0; # Why no warning
> }


Different scope.


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      09-21-2007
On Sep 21, 12:25 am, Mintcake <(E-Mail Removed)> wrote:
> #!/usr/local/bin/perl
>
> use strict;
> use warnings;
>
> for my $i (1..10)
> {
> my $i = 0; # Why no warning


> }


It's not the same scope. It's a subset of the other scope. The first
$i's scope is for the entire loop, ncluding the loop header - the list
of 1..10. The second $i's scope is only for the body of the loop,
not for its header.

{
my $i;
my $i; # Warning!
{
my $i; # No warning!
}
}

Paul Lalli

 
Reply With Quote
 
Mintcake
Guest
Posts: n/a
 
      09-24-2007
On Sep 21, 2:51 pm, "John W. Krahn" <(E-Mail Removed)> wrote:
> Mintcake wrote:
> > #!/usr/local/bin/perl

>
> > use strict;
> > use warnings;

>
> > for my $i (1..10)
> > {
> > my $i = 0; # Why no warning
> > }

>
> Different scope.
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order. -- Larry Wall


Why different scope? - both instances of $i exist only with the {}

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      09-24-2007
Mintcake wrote:
>
> On Sep 21, 2:51 pm, "John W. Krahn" <(E-Mail Removed)> wrote:
>>
>> Mintcake wrote:
>>>
>>> #!/usr/local/bin/perl
>>>
>>> use strict;
>>> use warnings;
>>>
>>> for my $i (1..10)
>>> {
>>> my $i = 0; # Why no warning
>>> }

>>
>> Different scope.

>
> Why different scope? - both instances of $i exist only with the {}


The braces {} define scope (within a file) except (Perl has a lot of
exceptions for the variable declared for a foreach loop which has "special"
scope.

$ perl -wle'
for my $i ( 0 .. 9 ) {
print __LINE__, ": $i";
my $i = 20;
print __LINE__, ": $i";
}
continue {
print __LINE__, ": $i";
}
print __LINE__, ": $i";
'
Name "main::i" used only once: possible typo at -e line 10.
3: 0
5: 20
8: 0
3: 1
5: 20
8: 1
3: 2
5: 20
8: 2
3: 3
5: 20
8: 3
3: 4
5: 20
8: 4
3: 5
5: 20
8: 5
3: 6
5: 20
8: 6
3: 7
5: 20
8: 7
3: 8
5: 20
8: 8
3: 9
5: 20
8: 9
Use of uninitialized value in concatenation (.) or string at -e line 10.
10:



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments