santoshprasad has asked for the wisdom of the Perl Monks concerning the following question:
use strict; use warnings; use Data::Dumper; our $VAR=5.0; sub printit{print "Package Main VAR=$VAR\n";} package A; our $VAR=2.0; sub printit{print "Package A VAR=$VAR\n";} package B; our $VAR=3.0; sub printit{print "Package B VAR=$VAR\n";} package C; our $VAR=4.0; sub printit{print "Package C VAR=$VAR\n";} package main; A::printit; B::printit; C::printit; printit; print "Package Main VAR=$VAR\n";
Output of the above script it.... Package A VAR=2 Package B VAR=3 Package C VAR=4 Package Main VAR=5 Package Main VAR=4
COMMENT : I am confused to see the result. As per my understanding, the last line of the result should be same as second last line. Please help. Thank you.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: package variable scope ('our' has lexical scope)
by LanX (Saint) on Jun 14, 2015 at 12:30 UTC | |
|
Re: package variable scope
by AnomalousMonk (Archbishop) on Jun 14, 2015 at 16:17 UTC | |
|
Re: package variable scope
by shmem (Chancellor) on Jun 14, 2015 at 17:49 UTC |