$! - error message from the Operating system
Example 6-1. examples/files/open_with_if.pl
#!/usr/bin/perl
use strict;
use warnings;
my $filename = "input.txt";
if (open my $in, "<", $filename) {
# do your thing here
# no need to explicitly close the file
} else {
warn "Could not open file '$filename'. $!";
}
# here the $in filehandle is not accessible anymore
A more Perlish way to open a file and exit with error message if you could not open the file:
| Prev | Home (Copyright Gabor Szabo) Perl Training Israel | Next |
| Opening a file | Up | Opening a missing file |