Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^3: if(my) scope

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

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

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.

Comment on Re^3: if(my) scope
Replies are listed 'Best First'.
Re^4: if(my) scope
by oeuftete (Monk) on Apr 16, 2009 at 11:21 UTC
    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?
Re^4: if(my) scope
by citromatik (Curate) on Apr 16, 2009 at 11:13 UTC
    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.


        Is a documented bug any less of a bug?
      Agreed. It's a bug: the compile-time and run-time scopes differ.


        not a bug, as already explained, but a sin...
        right now i always seen DESTROY called exactly when the refcount fall to zero. was pretty predictable.

        anyway by being clearly documented i expect changes in the future, and that force me to find another way.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (13)
As of 2015-10-13 12:24 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (304 votes), past polls