pike_uk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, If you undef a var, or assign that var to something esle, then how does this happen?
## the test script use Report::testob; my $testo; $x=0; while($x<10){ undef $testo; $testo="ooof"; $testo=testob->new(); $testo->printblah(); $testo="yess"; $x++; } ##### # the "object" package testob; sub new { my $class=shift; my $self={@_}; bless($self,$class); return $self; } sub printblah{ my $self=shift; $blah++; $self->{blah}++; print "blah is $blah \n"; print "self blah is ".$self->{blah}."\n"; } 1; ##### # the output blah is 1 self blah is 1 blah is 2 self blah is 1 blah is 3 self blah is 1 blah is 4 self blah is 1 blah is 5 self blah is 1 blah is 6 self blah is 1 blah is 7 self blah is 1 blah is 8 self blah is 1 blah is 9 self blah is 1 blah is 10 self blah is 1
## how does this happen? I thought that assigning $testob to a string or calling undef would remove any reference to the memory location of the object, but it is clear here that $blah in the "object" retains this as does $testob?? please enlighten me! thanks in advance.

Replies are listed 'Best First'.
Re: Undef doesnt work?This has got me stumped
by broquaint (Abbot) on Feb 09, 2004 at 10:20 UTC
    but it is clear here that $blah in the "object" retains this as does $testob??
    You are in fact modifying the package variable $blah, and like all package variables, are visible throughout the execution of the program. Whereas a new instance of $testob is being created every time as indicated by the value of $self->{blah} always being at 1 post-increment. See. perlobj for more info on objects in perl.
    HTH

    _________
    broquaint

      Thanks for the reply. I understand the package variable bit, thanks, but when (or how) does this get destroyed? Does the package reference survive until the script dies?
        Correct, package variables live until perl exits, or the variable is explicitly destroyed. If you want shorter lived variables then you'll want to be using lexical variables who's life-span is tied to the length of the surrounding lexical scope (unless they're referenced from a higher scope). For more information on lexical variables see. Lexical scoping like a fox.
        HTH

        _________
        broquaint

Re: Undef doesnt work?This has got me stumped
by borisz (Canon) on Feb 09, 2004 at 10:19 UTC
    It works as expected. self->{blah} is only one on every iteration. But you increment also the global var blah every time you call  printblah. try to start your script with use strict; to note such errors.
    Boris
Re: Undef doesnt work?This has got me stumped
by Corion (Patriarch) on Feb 09, 2004 at 10:22 UTC

    I'm not exactly sure what you expect to happen, but I'm trying to interpret what you see:

    You're not using strict, which is always a bad idea. In your case, it is a bad idea, because it allows you to be lax about where $blah is defined and used. Perl thinks you want $blah to be used as a global variable, that is why you see the value of $blah increasing in each call to printblah.

    In testob::new(), you never give $self->{blah} a value, so it will be 1 after you increment it, which is then what is printed. If you want to hold on to $testo in your main loop, you would need to keep it declared on the outside of the loop, or turn your testob::new() subroutine into a subroutine that only returns one and the same object (fancy speaking people call this a "Singleton"):

    { my $singleton; sub new { if (! $singleton) { my $class=shift; $singleton = {@_}; bless($singleton,$class); }; return $singleton; };