Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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.

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.


Comment on Re^3: if(my) scope
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.

    citromatik

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

      Dave.

        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.

        Oha

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

      citromatik

        Is a documented bug any less of a bug?
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 +.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://757935]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-11-26 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (160 votes), past polls