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


In reply to Re^3: anonymous filehandes? by grinder
in thread anonymous filehandes? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.