Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: A Few Questions About IO::File

by topher (Scribe)
on Mar 18, 2013 at 17:32 UTC ( [id://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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-25 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found