in reply to Re: File handling basics
in thread File handling basics
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: File handling basics
by Laurent_R (Canon) on Jul 17, 2015 at 17:58 UTC |