Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Implicit closing of files

by rovf (Priest)
on Jun 17, 2008 at 11:39 UTC ( [id://692478]=note: print w/replies, xml ) Need Help??


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

This means closing the filehandle is not the same as closing the file??? So, at the end of the block, only the file handle is closed and the file left being open (which of course then means that the buffers are not flushed yet)?
I got my idea for using local file handles and their automatical closing from this article: Seven Useful Uses of local
-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re^3: Implicit closing of files
by dragonchild (Archbishop) on Jun 17, 2008 at 13:01 UTC
    It's not that the file is left open per se. Perl (and every other language worth using) doesn't necessarily flush buffers immediately. close() does an explicit flush and destroys the handle. However, if you let the variable fall off the end of the scope, now you're depending on destruction to close the handle (and flush the buffer). Destruction in Perl is neither ordered nor timely.

    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?
      It's not that the file is left open per se. It's not that the file is left open per se. Perl ... doesn't necessarily flush buffers immediately. ... However, if you let the variable fall off the end of the scope, now you're depending on destruction to close the handle (and flush the buffer).

      I think there is a contradiction in this. Of course I/O is usually flushed, but you if I understand you right, you consider it possible that the file is not open anymore (because the handle falls out of scope), but that the buffer is flushed only if the handle is destroyed (i.e. later). But this makes no sense, IMO: You can't flush a buffer to a file, if the file is already closed.

      This leaves us with two possibilities: Either Perl does a proper close on the file, if the handle goes out of scope (which means that the buffer would be flushed), or that it does not close the file after the handle goes out of scope (and leaves it up to the destructor of the handle to flush the buffer and close the file, i.e. likely at program end).

      The first alternative would make sense. The second alternative would mean that at the end of the block, the file is still happily open.

      At least in the cases I tried, it behaved as if alternative 1 would be implemented, but I was wondering whether or not this is something I can rely to...

      -- 
      Ronald Fischer <ynnor@mm.st>

Log In?
Username:
Password:

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

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

    No recent polls found