cedar has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: 'Our' limits?
by Ieronim (Friar) on Jul 14, 2006 at 16:43 UTC | |
|
Re: 'Our' limits?
by polettix (Vicar) on Jul 14, 2006 at 16:51 UTC | |
|
Re: 'Our' limits?
by ikegami (Patriarch) on Jul 14, 2006 at 17:08 UTC | |
|
Re: 'Our' limits?
by jmcada (Acolyte) on Jul 14, 2006 at 16:44 UTC |