rehmanz has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to figure out the scoping rules for "our" variable types. I have the following two questions:
1) If I have decared a variable x in packages Foo, Bar and in the main program, what would be the output of the program below?
2) What would happen if I use the "use strict" option?
Thanks in advance!
# Begin Package Foo package Foo; our $x = 1; # End Package
# Begin Package Bar package Bar; our $x = 2; # End Package
# Main Program use Foo; use Bar; our $x = 3; print "x without package reference = $x"; print "Foo x = $foo::x; # Output should be "Foo x = 1" print "Foo x = $bar::x; # Output should be "Bar x = 2" $x = 4; print "x without package reference = $x"; print "Foo x = $foo::x; # Output should be "Foo x = 1" print "Foo x = $bar::x; # Output should be "Bar x = 2"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Scoping Rules For "our" Variables
by wind (Priest) on Jun 25, 2011 at 22:38 UTC | |
|
Re: Scoping Rules For "our" Variables
by roboticus (Chancellor) on Jun 26, 2011 at 14:31 UTC | |
by rehmanz (Novice) on Jun 27, 2011 at 15:23 UTC |