#!/usr/bin/perl
$logdir = "/home/billw/var/logs";
$logfile = "test-referer.log";
$reflog = "$logdir/$logfile";
$crlf = "\x0d\x0a";
print "content-type: text/html$crlf";
print "$crlf";
open(REFLOG, "<$reflog") or die "cannot open $reflog!\n";
while(<REFLOG>) {
chomp;
print qq(<a href="../module5/$_">$_</a><br>\n) unless $refhash{$_};
$refhash{$_} += 1;
}
close REFLOG;
This program opens the same file, but this time in read mode. Then it uses the special-case version of the
while()
loop,
which automatically assigns the next line from the file to the special
$_
variable.
The special case of the
while()
loop is probably the most common construct that you will use with streams, so let us take a closer look at it.
To help you understand this construct, take a look at the following two lines of Perl. They are functionally identical:
while(<REFLOG>) { print } # print the whole stream
while($line = <REFLOG>) { print $line } # print the whole stream
In Perl, the special variable
$_
is the default argument to a number of functions, including
print()
and
chomp()
, which you will use a lot when working with streams. This special case of the
while()
loop assigns the value of the current line from the stream to the
$_
variable.
This makes it possible to write an entire
cat
program like this:
#!/usr/bin/perl
while(<>) { print }
<> is a special file stream, which defaults to the standard input stream, or each of the files referenced on the command line.
Or, if you want to print only the lines with matched angle brackets:
Let us break down the difference between `chomp` and `slurp` in Perl:
chomp
slurp
Key Differences
Feature |
chomp |
slurp |
Input |
A string (usually a line of text) |
A file path |
Output |
The same string with the newline removed (if present) |
The entire contents of the file as a single string |
Modification |
Modifies the input string directly |
Creates a new string containing the file content |
Core vs. Module |
Built-in Perl function |
Function from a module (`File::Slurp`, `File::Slurper`, etc.) |
Example Combining chomp and slurp
use File::Slurp;
my $line = read_file("myfile.txt"); # Read the first line of the file
chomp $line; # Remove the trailing newline from the line
When to Use Each
- chomp: Primarily used for processing line-by-line input, such as reading from a file with a loop or handling user input.
- slurp: Ideal when you need to read the entire content of a file at once for further processing (e.g., parsing configuration files, analyzing log files).
Are you starting to see how convenient this is? These same techniques work with all the different types of streams that Perl offers, including pipes.
Click the Exercise link below to write a simple Perl script that opens a file and prints to the screen.
Reading From File Streams - Exercise