I made a few additional changes reflecting the currently widely accepted good practices.

you missed one ;)
- open (my $F, $file) or die "cannot open file $!"; + open (my $F), '<', $file or die "cannot open file $!";

@OP: If for some reason you have a file '>123.txt', then open (my $F, $file) would open the file for writing and truncate it.

Two purely cosmetical changes would be to replace $F with $fh and also add the filename to the error message, like "cannot open file '$file': $!".

@OP: I would consider replacing 'die' with 'warn and next', so that you can process the remaining files even if one is not readable to the current user.

Variant a)

open (my $F), '<', $file or warn "cannot open file '$file': $!"; defined $F or next;

Variant b)

open (my $F), '<', $file or warn "cannot open file '$file': $!" and next;

I prefer Variant a) because chaining conditionals may lead to surprises. Mostly because 'or' short circuits the evaluation and due to operator precedence.


In reply to Re^2: File handling basics by Monk::Thomas
in thread File handling basics by ElectroRed

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.