Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: if(my) scope

by targetsmart (Curate)
on Apr 16, 2009 at 10:50 UTC ( #757933=note: print w/ replies, xml ) Need Help??


in reply to if(my) scope

the phenomenon you noted is absolutly normal, if the reference count of $h goes to zero, it is automatically freed by perl, since it is associated with a object, the DESTROY method in the class will be called.
see perltoot Destructors
UPDATE
I generally talked about the way the DESTROY is called, but this is new to me, pardon if it caused some confusion.


Vivek
-- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.


Comment on Re: if(my) scope
Re^2: if(my) scope
by Anonymous Monk on Apr 16, 2009 at 10:54 UTC
    That wasn't the question. The question was why DESTROY is called after the outer { } ? after the if() block $h is no longer reachable, so why still holds a reference to the blessed instance?
      IMO the $h is visible entirely to the outer {} rather than 'if' block. I have to check the docs.

      UPDATE
      thanks to citromatik, but I know that the $h should get deallocated immediately after the 'if' block, but the example shown has confused me a bit, so I just coined my guess!. that is why i put 'In my opinion'. This is kind of learning about DESTROY and I will take it gleefully.

      Vivek
      -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
        IMO the $h is visible entirely to the outer {} rather than 'if' block

        No, it is not, a variable "lives" in the most inner block of code where it has been declared. If it is declared in an "if" condition or in a loop initialization, it is accessible only inside the "if" or loop block.

        citromatik

        It's definitely out of scope.
        package H; sub DESTROY { print "DESTROY\n"; } sub new { my $class = shift; bless {}, $class; } sub foo { } package main; { if ( my $h = H->new() ) { print "IN\n"; $h->foo; } print "\$h out of scope?\n"; eval { $h->foo }; print $@ if $@; } print "why now?\n"; IN $h out of scope? Can't call method "foo" on an undefined value at /home/me/tmp/pm757929 +.pl line 15. DESTROY why now?
Re^2: if(my) scope
by ikegami (Pope) on Apr 16, 2009 at 13:51 UTC

    f the reference count of $h goes to zero, it is automatically freed by perl,

    But why does it go to zero where it does.

    package H; sub DESTROY { print "DESTROY\n"; } sub new { my $class = shift; bless {}, $class; } { print \$h, "\n"; if(my $h = H->new()) { print \$h, "\n"; } print \$h, "\n"; # <- This references the package var # and would cause a strict error. # Object is no longer accessible. print "why now?\n"; } # <- The object is only destroyed here.
    SCALAR(0x1829aac) REF(0x1829abc) SCALAR(0x1829aac) why now? DESTROY

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://757933]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-12-25 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls