Re: [PBML] References
- On Fri, Apr 07, 2006 at 08:22:00PM +0100, Ken Shail wrote:
> I did something like the following by mistake but it appears to work. I.E. it prints "Some Stuff";It doesn't stay in scope.
> How does $stuff stay in scope to be able to supply its string at a later date?
> sub mySub
> my $stuff='Some stuff';
> return \$stuff;
> print &mySub."\n";
When you create the $stuff variable inside that subroutine, mySub, you
are incrementing the reference count for it from zero to one. When you
return a reference to $stuff, you are incrementing it from one to two.
Simultaneously, you are leaving the mySub subroutine, which decrements
it, bringing it from two back down to one. Thus, the reference count
for the place where you're storing that string, 'Some stuff', is still
above zero, even though the $stuff variable itself is out of scope.
Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]
"The measure on a man's real character is what he would do
if he knew he would never be found out." - Thomas McCauley