10.5 Using Filehandles
After a filehandle is open for reading, you can read lines from it just as you can read from standard input with open (FIL,"some_file"); while (<FIL>) { chomp; print "I saw $_ in some_file!\n"; }
Note that the newly opened filehandle is used inside the angle brackets, just as we have used If you have a filehandle open for writing or appending, and if you want to print to it, you must place the filehandle immediately after the print keyword and before the other arguments. No comma should occur between the filehandle and the rest of the arguments: print LOGFILE "Finished item $n of $max\n"; print STDOUT "hi, world!\n";
In this case, the message beginning with
Here's a way to copy all of the text from a file specified in
open(IN,$a) || die "cannot open $a for reading: $!"; open(OUT,">$b") || die "cannot create $b: $!"; while (<IN>) { # read a line from file $a into $_ print OUT $_; # print that line to file $b } close(IN) || die "can't close $a:$!"; close(OUT) || die "can't close $b:$!"; |
|