Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] References

Expand Messages
  • Chad Perrin
    ... It doesn t stay in scope. When you create the $stuff variable inside that subroutine, mySub, you are incrementing the reference count for it from zero to
    Message 1 of 5 , Apr 7, 2006
    • 0 Attachment
      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";
      > 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";

      It doesn't stay in scope.

      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
    Your message has been successfully submitted and would be delivered to recipients shortly.