Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^4: Implicit closing of files

by ikegami (Patriarch)
on Jun 17, 2008 at 13:05 UTC ( [id://692506]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Implicit closing of files
in thread Implicit closing of files

Everything I've seen says that destruction in Perl is timely. Destruction occurs as soon as the value's ref count reaches zero. Do you have any documentation to contradict that?

This is in contrast with Java, where garbage collection is not as predictable.

Update: The only documentation bit I've found is in perlref:

Hard references are smart--they keep track of reference counts for you, automatically freeing the thing referred to when its reference count goes to zero.

And it goes on to say

If that thing happens to be an object, the object is destructed.

There's no mention of a delay or of it being deferred. Although it doesn't say explicitly say "immediately", I'm still convinced it is because my belief is based on much more than that one statement.

Replies are listed 'Best First'.
Re^5: Implicit closing of files
by dragonchild (Archbishop) on Jun 17, 2008 at 13:20 UTC
    Ah. I have found my misremembering. Calling of DESTROY is neither timely nor ordered. Destruction in Perl is timely for lexicals. But, I can't find anything about destruction of localized variables. I would figure that the recovery of the hidden value is timely. But, that doesn't imply that the destruction of the covering value is timely.

    I, of course, would love to hear from TimToady, chromatic, or some other guts person. And, frankly, being able to depend on timely destruction would be nice.


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      And, frankly, being able to depend on timely destruction would be nice.

      It's a Perl5 feature! Don't know about Perl6.

      Destruction of SVs is not related to what has a hard reference (C pointer) to it. As soon all the lexical variables, package variables, stack reference, etc disappear, destruction occurs (including calling the appropriate DESTROY).

      use Devel::Peek; sub DESTROY { print STDERR "*Destroyed*\n" } { local $foo; { my $bar = bless {}; $foo = $bar; Dump($foo); print STDERR ("Exiting inner\n"); } Dump($foo); print STDERR ("Exiting outer\n"); } print STDERR ("Exiting file scope\n");

      Here's the relevant lines of the output:

      REFCNT = 2 Exiting inner REFCNT = 1 Exiting outer *Destroyed* Exiting file scope

      Re^2: deleteing references might help. It doesn't matter whether a value is referenced by a lexical var, by a package var, or by the underlying memory structure of an array. As soon as something's refcount reaches zero, it is destroyed. In the figure, "Automatically" can be read as "Immediately".

      Calling of DESTROY is neither timely nor ordered.

      You're thinking of global destruction. Scope-exiting destruction in Perl 5 is indeed timely and deterministic (though you probably shouldn't rely on the order of destruction of $foo, $bar, and $baz in the following example):

      { my $foo = 1; my $bar = 2; my $baz = 3; }

      Localized restoration uses the same reference-counting mechanism, so the appropriate destruction there is timely.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-24 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found