Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl foreach loop variable scope

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?

like image 370
chammu Avatar asked Feb 03 '15 18:02

chammu


2 Answers

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

like image 152
PrgmError Avatar answered Sep 20 '22 20:09

PrgmError


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

like image 21
mpapec Avatar answered Sep 22 '22 20:09

mpapec