10.3 Reading, Writing and Filehandles
open FILEHANDLENAME "filename";
In earlier chapters we looked at how to read and write to STDIN and STDOUT respectively. The mechanism for working with your own filehandles is no different. If we wanted to read the contents of file called .mydata. and display the contents to STDOUT we could do so as follows:
open MYDATA, "datafile";
while (<MYDATA>) {
print;
}
Similarly we use the print and printf commands that we previously used to write to STDOUT to write to our filehandle:
Open OUTFILE "<outfile";
printf OUTFILE "This chapter %d of Picking Up Perl.\n", 10;
print OUTFILE "The End\n"
By default the print operators will output to STDOUT unless a filehandle is specified. The default filehandle can be changed using the .select. operator:
Select OUTFILE;