pritesh@mintpad ~ $ more test_var_loc.pl
use strict;
use warnings;
my $val = 10;
print "Outside the functions \$val is $val stored at ", \$val, "\n";
sub first {
$val = 200;
print "In the first function \$val is $val stored at ", \$val, "\n";
}
sub second {
$val = 100;
print "In the second function \$val is $val stored at ", \$val, "\n";
}
first();
print "After first function, before second function, \$val is $val stored at ", \$val, "\n";
second();
print "After second function, \$val is $val stored at ", \$val, "\n";
pritesh@mintpad ~ $
####
pritesh@mintpad ~ $ perl test_var_loc.pl
Outside the functions $val is 10 stored at SCALAR(0x1c307c8)
In the first function $val is 200 stored at SCALAR(0x1c307c8)
After first function, before second function, $val is 200 stored at SCALAR(0x1c307c8)
In the second function $val is 100 stored at SCALAR(0x1c307c8)
After second function, $val is 100 stored at SCALAR(0x1c307c8)
pritesh@mintpad ~ $
####
pritesh@mintpad ~ $ more test_my_var_loc.pl
use strict;
use warnings;
my $val = 10;
print "Outside the functions \$val is $val stored at ", \$val, "\n";
sub first {
my $val = 200;
print "In the first function \$val is $val stored at ", \$val, "\n";
}
sub second {
my $val = 100;
print "In the second function \$val is $val stored at ", \$val, "\n";
}
first();
print "After first function, before second function, \$val is $val stored at ", \$val, "\n";
second();
print "After second function, \$val is $val stored at ", \$val, "\n";
####
pritesh@mintpad ~ $ perl test_my_var_loc.pl
Outside the functions $val is 10 stored at SCALAR(0xb2d7c8)
In the first function $val is 200 stored at SCALAR(0xb2d888)
After first function, before second function, $val is 10 stored at SCALAR(0xb2d7c8)
In the second function $val is 100 stored at SCALAR(0xb2e290)
After second function, $val is 10 stored at SCALAR(0xb2d7c8)
pritesh@mintpad ~ $