Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: if(my) scope

by targetsmart (Curate)
on Apr 16, 2009 at 10:50 UTC ( [id://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.

Replies are listed 'Best First'.
Re^2: if(my) scope
by ikegami (Patriarch) 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
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.
        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?
        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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-19 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found