Before writing this program,I thought that our is a package scope variable and my is
a file scope variable.But,After did that program,I am get confused.
My program is,
#!/usr/bin/perl
use strict;
use warnings;
package one;
our $val = "sat";
my $new = "hello";
print "ONE:val =>$val \n";
print "ONE:new =>$new \n\n";
package two;
print "TWO:val =>$val \n";
print "TWO:new =>$new \n";
which outputs
ONE:val =>sat
ONE:new =>hello
TWO:val =>sat
TWO:new =>hello
So,what is the difference between my and our.Whether the both are the same or it having any difference?
As you see, both
myandourhave lexical effect.mycreates a lexically scoped variable.ourcreates a lexically scoped alias to a package variable.Just because you say
packagein no fashion changes the lexical scope, so your$valis still an alias to$one::valeven after having seen thepackage twostatement.If you don’t see a close curly, you haven’t finished your scope. (Or EOF or end of string in a string
eval).