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

Re: A Few Questions About IO::File

by topher (Scribe)
on Mar 18, 2013 at 17:32 UTC ( #1024116=note: print w/ replies, xml ) Need Help??

in reply to A Few Questions About IO::File

There's been a good bit of decent discussion on this, and I'm guessing you've gotten most of your questions answered. One thing I didn't really see mentioned is a bit on the "why" of it.

When you create a new IO::File object, you are, obviously, creating an object for your filehandle and assigning it to a variable. After you finish with it, you can call $fh_var->close; to explicitly close your file. Personally, I'm a big fan of this, as it makes your intentions clear and it's a good habit to clean up after yourself.

If you undef the variable, you get the same behavior as the variable going out of scope, which is that Perl will automatically clean up (the equivalent of a DESTROY() method) after you and close the file; as the object no longer has any references to it, there is no way to access the file, so it is "garbage collected", so to speak.

Neither the the explicit close function nor the variable undef is really better or worse than the other, although they do offer different trade-offs. For example, if you do an explicit close, you can re-use for IO::File object to open a new file. If you undefine the object reference, you will need a new object if you wanted to open a new file.

Christopher Cashell

Comment on Re: A Few Questions About IO::File
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2014-10-22 12:46 GMT
Find Nodes?
    Voting Booth?

    For retirement, I am banking on:

    Results (118 votes), past polls