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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |