Perl foreach loop variable scope

2019-05-06 01:21发布

问题:

I am new to perl and was confused with perl scoping rules after I wrote below code snippet:

#!/usr/bin/perl
my $i = 0;
foreach $i(5..10){
    print $i."\n";
}
print "Outside loop i = $i\n";

I expected output to be like :

5
6
7
8
9
10
Outside loop i = 10

But its giving :

5
6
7
8
9
10
Outside loop i = 0

So the variable $i value is not changing after the loop exits. Whats going on in here?

回答1:

According to the perldoc information regarding foreach loops: here

The foreach loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn. If the variable is preceded with the keyword my, then it is lexically scoped, and is therefore visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop. If the variable was previously declared with my, it uses that variable instead of the global one, but it's still localized to the loop. This implicit localization occurs only in a foreach loop.

If you want to retain the value of $i outside the loop then you can omit $i in the foreach loop call and use perl's special variable $_ an example below:

#!/usr/bin/perl

use strict;
use warnings;

my $i = 0;
foreach (5..10){
    print $_."\n";
    $i = $_;
}
print "Outside loop i = $i\n";

5 6 7 8 9 10 Outside loop i = 10



回答2:

foreach localize variable to the loop.

use strict;
use warnings;

my $adr;
my $i = 0;
foreach $i(5..10){
    $adr = \$i;
    print "$i ($adr)\n";
}
$adr = \$i;
print "Outside loop i = $i ($adr)\n";

output

5 (SCALAR(0x9d1e1d8))
6 (SCALAR(0x9d1e1d8))
7 (SCALAR(0x9d1e1d8))
8 (SCALAR(0x9d1e1d8))
9 (SCALAR(0x9d1e1d8))
10 (SCALAR(0x9d1e1d8))
Outside loop i = 0 (SCALAR(0x9d343a0))

From perldoc,

The foreach loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn. If the variable is preceded with the keyword my, then it is lexically scoped, and is therefore visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop. If the variable was previously declared with my, it uses that variable instead of the global one, but it's still localized to the loop. This implicit localization occurs only in a foreach loop.

To preserve value of $i you can use C like for loop,

my $i = 0;
for ($i = 5; $i <= 10; $i++) { .. }

although it's less readable than perl foreach



回答3:

The variable $i is redefined in foreach scope at

foreach $i(5..10){

So the variable outside foreach will not change.