Line 357: |
Line 357: |
| === Files and I/O === | | === Files and I/O === |
| | | |
− | You can open a file for input or output using the open() function. It's documented in extravagant detail in perlfunc and perlopentut, but in short: | + | You can open a file for input or output using the [http://perldoc.perl.org/functions/open.html open()] function. It's documented in extravagant detail in [http://perldoc.perl.org/perlfunc.html perlfunc] and [http://perldoc.perl.org/perlopentut.html perlopentut], but in short: |
| | | |
| open(my $in, "<", "input.txt") or die "Can't open input.txt: $!"; | | open(my $in, "<", "input.txt") or die "Can't open input.txt: $!"; |
Line 363: |
Line 363: |
| open(my $log, ">>", "my.log") or die "Can't open my.log: $!"; | | open(my $log, ">>", "my.log") or die "Can't open my.log: $!"; |
| | | |
− | You can read from an open filehandle using the <> operator. In scalar context it reads a single line from the filehandle, and in list context it reads the whole file in, assigning each line to an element of the list: | + | You can read from an open filehandle using the '''<>''' operator. In scalar context it reads a single line from the filehandle, and in list context it reads the whole file in, assigning each line to an element of the list: |
| | | |
| my $line = <$in>; | | my $line = <$in>; |
Line 370: |
Line 370: |
| Reading in the whole file at one time is called slurping. It can be useful but it may be a memory hog. Most text file processing can be done a line at a time with Perl's looping constructs. | | Reading in the whole file at one time is called slurping. It can be useful but it may be a memory hog. Most text file processing can be done a line at a time with Perl's looping constructs. |
| | | |
− | The <> operator is most often seen in a while loop: | + | The '''<>''' operator is most often seen in a '''while''' loop: |
| | | |
| while (<$in>) { # assigns each line in turn to $_ | | while (<$in>) { # assigns each line in turn to $_ |
Line 376: |
Line 376: |
| } | | } |
| | | |
− | We've already seen how to print to standard output using print(). However, print() can also take an optional first argument specifying which filehandle to print to: | + | We've already seen how to print to standard output using '''[http://perldoc.perl.org/functions/print.html print()]'''. However, '''[http://perldoc.perl.org/functions/print.html print()]''' can also take an optional first argument specifying which filehandle to print to: |
| | | |
| print STDERR "This is your final warning.\n"; | | print STDERR "This is your final warning.\n"; |
Line 382: |
Line 382: |
| print $log $logmessage; | | print $log $logmessage; |
| | | |
− | When you're done with your filehandles, you should close() them (though to be honest, Perl will clean up after you if you forget): | + | When you're done with your filehandles, you should '''[http://perldoc.perl.org/functions/close.html close()]''' them (though to be honest, Perl will clean up after you if you forget): |
| | | |
| close $in or die "$in: $!"; | | close $in or die "$in: $!"; |