File Handles
open (FILE_HANDLE, “file.name”);
Open for reading:
open (SONGS, “song_list.txt”);
open (SONGS, “song_list.txt”) or die “Can’t open that file! Error is $!”;
Retrieve lines using < > :
open (SONGS, “song_list.txt”) or die “Can’t open that file!”;
for $line (<SONGS>) {
print $line;
}
Open for overwriting:
open (SONGS, “>song_list.txt”) or die “Can’t open that file!”;
# Be aware that any original contents are now gone!
Open for appending:
open (SONGS, “>>song_list.txt”) or die “Can’t open that file!”;
# The original contents are preserved, just appended onto.
Writing to the file:
open (SONGS, “>song_list.txt”) or die “Can’t open that file!”;
print SONGS “Stairway to Heaven\n”;
# Everything else is gone
Appending to the file:
open (SONGS, “>>song_list.txt”) or die “Can’t open that file!”;
print SONGS “Copacabana\n”;
File Operations
Steve Litt’s PERLs of Wisdom: PERL File Input, Output and Sorting
(also see the root of this “tips” area, Steve Litt’s Perls of Wisdom)
Formatting and Printing: sprintf()
Perl File Handling: open, read, write and close files at Perlfect Solutions
-note the use of the returned error code (which isn’t always an error), $!
-and the “current line” special variable $_
-and the operator <FILE>
The Kinder, Gentler File Opening Tutorial at Perldoc.perl.org
File Handling at DevelopingWebs.net