|
|
Perl makes it very easy to open and close files on the file system. Listing C.7 shows you how to open a file and display an error message if the file cannot be opened for any reason (e.g., the file does not exist).
Listing C.7 openFile1.pl
my($fileName) = "somedata.txt"; # open the input file... open(INPUT, "<$fileName") || die "cannot open $fileName: $!\n"; # some processing happens here... # close the input file... close(INPUT);
You can launch the Perl script multiHash1.pl in Listing C.7 from the command line as follows:
perl -w openFile1.pl
If the file does not exist, you will see the following message:
cannot open somedata.txt: No such file or directory
If the file was opened and closed successfully, then you will not see anything because we haven't done anything with the file. The next example shows you how to read the contents of a text file.
|
|