6.7. Process an entire file line by line (while, cat)

  • while - executes as long as there is something in $line, as long as there are lines in the file

  • Loop over file (name hardcoded) and print every line (UNIX cat)

Example 6-5. examples/files/cat.pl

#!/usr/bin/perl
use strict;
use warnings;

my $filename = "input.txt";
open(my $fh, "<", $filename) or die "Could not open '$filename'\n";
while (my $line = <$fh>) {
    print $line;
}

Instead of printing the line you could do anything with it.


If you are interested in on-site trainings by the author, please contact me directly.

Online courses:

Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed