vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Today I was learning about the FileHandle module from CPAN, they have given the example like how to use

use FileHandle; $fh = new FileHandle "file", "r"; if (defined $fh) { print <$fh>; undef $fh; # automatically closes the file }

In this example they used 'undef' to close the named file handle, I changed the 'undef' as 'close' it works fine, whether undef and close are identical here ?

Vinoth,G

Replies are listed 'Best First'.
Re: FileHandle: undef vs close
by lostjimmy (Chaplain) on Apr 30, 2009 at 14:16 UTC

    I'd say they do equivalent things. When a file handle has goes out of scope (or has no more references to it), it is automatically closed. undefing it will drop its refcount to zero, which is why it automatically closes.

    I believe you would see the same behavior if you had done a simple open my $fh, "r".

    At any rate, they aren't identical, but the outcome is the same. I would recommend using close, but only because I think its intent is more clear.

      Forgive my ignorance, but do you know if using undef leaves the closure of the file handle to garbage collection rather than executing the system call straight away and so is undef more efficient? Just wondering.

        You're doing IO and a system call and this is a micro-optimization, so this is a lousy place to optimize, but if you want to close the filehandle unambiguously, use close directly. undef removes one reference count from the filehandle; other variables may point to the same filehandle and prevent Perl's GC from reclaiming it (and closing it implicitly).

        Of course, if other variables point to the same filehandle and you close it explicitly underneath them, you may cause other problems.

      Thanks!!

      Vinoth,G
Re: FileHandle: undef vs close
by JavaFan (Canon) on Apr 30, 2009 at 16:08 UTC
    undef will close the filehandle, just as close. The difference is that the return value of undef is always undef, while close will return false if there's a failure. Most likely you won't care about failures on closing a handle you have open for reading, but you ought to check for failures when closing a file you've written data to.

    I cannot recall the last time I undeffed a filehandle. I either close the filehandle explicitely, let the filehandle go out of scope, or reuse the handle in a different open.

Re: FileHandle: undef vs close
by Corion (Patriarch) on Apr 30, 2009 at 14:13 UTC

    That's what the documentation says. What is your question?

Re: FileHandle: undef vs close
by Crackers2 (Parson) on Apr 30, 2009 at 18:03 UTC
    They're not 100% identical, as the following code shows:
    use warnings; use FileHandle; $fh = new FileHandle "file", "r"; if (defined $fh) { print <$fh>; undef $fh; # automatically closes the file } $fh2 = new FileHandle "file", "r"; if (defined $fh2) { print <$fh2>; close $fh2; # automatically closes the file } print <$fh>; print <$fh2>;
    Output:
    Use of uninitialized value in <HANDLE> at t line 16. readline() on unopened filehandle at t line 16. readline() on closed filehandle GEN1 at t line 17.

    Doing a close seems to maintain some information about the filehandle, and the variable stays defined. Doing an undef unsurprisingly undefs the variable, so you get a slightly different warning, and an extra one about it being udnefined.