Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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?

Replies are listed 'Best First'.
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.

    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.

    -- 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 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.


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


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


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://757934]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2018-01-20 12:02 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (226 votes). Check out past polls.