pritesh has asked for the wisdom of the Perl Monks concerning the following question:
Respected Monks,
I wanted to confirm if a variable delcared in a file is accessible and can be modified throughout the file, even inside a function, and that seems to be true.
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 st +ored at ", \$val, "\n"; second(); print "After second function, \$val is $val stored at ", \$val, "\n"; pritesh@mintpad ~ $
The output is as expected.
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 S +CALAR(0x1c307c8) In the second function $val is 100 stored at SCALAR(0x1c307c8) After second function, $val is 100 stored at SCALAR(0x1c307c8) pritesh@mintpad ~ $
Now, what I don't get is, if within the function, I declare the $val as my $val (lexical scope), no warning message is shown. Nothing like "masking earlier declaration of $val". It just treats it as if it were a different variable.
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 st +ored at ", \$val, "\n"; second(); print "After second function, \$val is $val stored at ", \$val, "\n";
The output is:
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 SC +ALAR(0xb2d7c8) In the second function $val is 100 stored at SCALAR(0xb2e290) After second function, $val is 10 stored at SCALAR(0xb2d7c8) pritesh@mintpad ~ $
I am confused. I thought that the second time I would get an error, but then it appears that, each my $var has scope limited to it's function or outside of the function. But if that's the case, I should have gotten an error the first time, stating "the variable requires explicit package name. Did you forget to declare my $var?" or something like that for the $val inside the function(s). I think I am missing something obvious here. Kindly help.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl Variables storage location and scope question.
by pryrt (Abbot) on Jan 15, 2018 at 21:40 UTC | |
|
Re: Perl Variables storage location and scope question.
by kcott (Archbishop) on Jan 16, 2018 at 09:40 UTC | |
|
Re: Perl Variables storage location and scope question.
by haukex (Archbishop) on Jan 16, 2018 at 08:10 UTC | |
|
Re: Perl Variables storage location and scope question.
by BillKSmith (Monsignor) on Jan 16, 2018 at 05:02 UTC | |
|
Re: Perl Variables storage location and scope question.
by ikegami (Patriarch) on Jan 17, 2018 at 17:36 UTC | |
|
Re: Perl Variables storage location and scope question.
by pritesh (Scribe) on Jan 16, 2018 at 10:35 UTC |