Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm not sure I have seen this documented anywhere, but what is the difference between these two code examples?

First explicit filehandling:

for my $fname (@ARGV) { open(IN, $fname); print while <IN>; close IN; }
Now, anonymous filehandling:
print while <>;
All candor shared would be greatly appreciated.

Replies are listed 'Best First'.
Re: anonymous filehandes?
by toolic (Bishop) on Jul 09, 2007 at 16:05 UTC
    One difference is that your "anonymous filehandling" will also work on STDIN, but your "explicit filehandling" will not.

    For example, the following will print something:

    cat file1.txt file2.txt | anonymous.pl

      I came up with the term anonymous filehandles on-the-fly as I didn't know what else it is called.

      Is there a formal name for this feature?

      Thanks.

        Perl Cookbook just refers to it as "<>" and doesn't mention any name for it.

        Regarding your other response, yes, perl is smart enough to automagically close any open files when the program exits, regardless of how/when they were opened. It may close files opened by <> sooner (e.g., when each of them hits EOF), but I haven't found anything which clearly states that one way or the other.

Re: anonymous filehandes?
by ides (Deacon) on Jul 09, 2007 at 16:01 UTC

    Well the first is readable and the second one is too much magic for my tastes. The second requires a level of Perl knowledge you can't always assume in your fellow programmer.

    Also, the first option gives you the chance to test your call to open for any failures ( permissions, file doesn't exist, etc ) and handle them.

    In short the first version is more correct, the second should be relegated to perl one liners...

    Frank Wiles <frank@revsys.com>
    www.revsys.com

      In the first example, files are opened & closed as iteration progresses. Is it correct to assume that in the second case files are collectively closed at the end of the script? Is the Perl interpreter smart enough to close files in the same manner as the first example?
        Is the Perl interpreter smart enough to close files in the same manner as the first example

        I'm 99.9999% sure that that is the case, without looking at the source. You can use the magic $ARGV variable to see where you are:

        my $prev; while (<>) { if (not defined $prev or $prev ne $ARGV) { print "now reading from $ARGV\n"; } print; $prev = $ARGV; }

        When run as "reader f1.txt f2.txt f3.txt" you'll be able to see when the program begins to read from the next file in the list. If you pipe into STDIN, it'll say "reading from -". I believe the most unambiguous terminology is to say that you are reading from the diamond operator.

        This is not obscure... it's useful.

        update: Oh, and, if you need to know when you reach the end of a file (not just when the new one begins), you can do that too, with eof.

        while (<>) { if (not defined $prev or $prev ne $ARGV) { print "reading $ARGV\n"; } print; print "and that's the end of $ARGV\n" if (eof); $prev = $ARGV; }

        When I get to that level of convoluted logic, however (especially if lots of other stuff is going on), my head usually explodes.

        • another intruder with the mooring in the heart of the Perl