Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: if(my) scope

by Anonymous Monk
on Apr 16, 2009 at 10:54 UTC ( #757934=note: print w/ replies, xml ) Need Help??


in reply to Re: if(my) scope
in thread if(my) scope

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?


Comment on Re^2: if(my) scope
Re^3: if(my) scope
by targetsmart (Curate) on Apr 16, 2009 at 10:57 UTC
    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

        Agreed. It's a bug: the compile-time and run-time scopes differ.

        Dave.

        It is stated in the docs (as dada pointed out), so this is not a bug.

        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?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (13)
As of 2014-07-22 10:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (109 votes), past polls