in reply to Object scope and DESTROY

Your code demonstrates the fact that the conditional statement of an "if" has the scope of the surrounding block. If you need something created in it to go out of scope, you could try this:
{ if( defined(my $obj = MyPack->new() ) ){ print "Inside the IF (3)\n"; } } print "Outside the IF (3)\n";

Replies are listed 'Best First'.
Re: Re: Object scope and DESTROY
by Rhandom (Curate) on Sep 24, 2001 at 21:08 UTC
    Thank you for the prompt reply. This certainly would work. What I am looking for though is an explanation of why, even though the object can't be accessed (I would assume it is out of scope), the DESTROY method is still not called.

    UPDATE: I notice that the above post mentions the scope is that of the block surrounding the if. If that is true (I don't know if it is or not) why do I get a compile error when I add the following line at the end of my code (assuming that the $obj should still be in scope):
    print "[".ref($obj)."]\n";


    Any ideas?

    my @a=qw(random brilliant braindead); print $a[rand(@a)];
      Well, that makes it look like it isn't the scope of the surrounding block either, so I'm not sure what scope it has. I suppose you could compare this with what happens when you just do a "my $obj = MyPack->new()" before the last "if".