I'm trying to get a grip on what 'our' does, and got a bit of a shock. The manual says "An our declaration declares a global variable that will be visible across its entire lexical scope, even across package boundaries." but I don't think that's what I see.
So I'm playing around and come up with this. I'm expecting to be able to change the $d arrayref in the top subroutine and see the changes in the package that called it. But I don't, insted I see the arrayref changing back as we go back up the stack. That would be cool if it were a "my", but it's not. Can anyone shed some light?
Thanks
package Foo;
sub top {
my($fn) = @_;
our($d) = ["a", "b"];
printf "Top before %s [%s]\n", $d, join(",", @$d);
$fn->($d);
printf "Top After %s [%s]\n", $d, join(",", @$d);
}
package Bar;
Foo::top(sub {
our($d) = @_;
printf " Before %s [%s]\n", $d, join(",", @$d);
$d = [];
printf " After %s [%s]\n", $d, join(",", @$d);
});
C:\Scripts>perl scope.pl
Top before ARRAY(0x183f218) [a,b]
Before ARRAY(0x183f218) [a,b]
After ARRAY(0x183f248) []
Top After ARRAY(0x183f218) [a,b]
C:\Scripts>perl -v
This is perl, v5.6.1 built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)
Copyright 1987-2001, Larry Wall
Binary build 635 provided by ActiveState Corp. http://www.ActiveState.
+com
Built 15:34:21 Feb 4 2003