Perl Multi-Dimensional Arrays

   



Opening and Closing Files in Perl

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

start example
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);
end example

Remarks

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.



   



Fundamentals of SVG Programming. Concepts to Source Code
Fundamentals of SVG Programming: Concepts to Source Code (Graphics Series)
ISBN: 1584502983
EAN: 2147483647
Year: 2003
Pages: 362

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net