10.4 Using Filehandles
Once a filehandle is open for reading, you can read lines from it just as you can read from standard input with
open (EP,"/etc/passwd");
while (<EP>) {
chomp;
print "I saw $_ in the password 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 LOGFILE "Finished item $n of $max\n"; print STDOUT "hi, world!\n"; # like print "hi, world!\n"
In this case, the message beginning with
Here's a way to copy data 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: $!";
|
|