Reading Files

Copy the text of each script and run it in Perl. You'll have to reset the name of "test.txt" to a file in the same directory in which you'll save the scripts, or you won't see any output.

The first script simply opens a file for reading and returns the input to screen.

# readFile1.pl

# program opens a file, reads it, and prints
# the content to STDOUT

# first, create a variable that holds the path to the source file

$source = "test.txt"; # assuming the file is in the same dir as the script

# second, create a filehandle named 'SOURCE':
# filehandles are created with the open()  function;
# the first parameter is the name of the filehandle,
# the second is the path and name of the file

open(SOURCE, "<$source");  # < is optional for reading files

# get input from SOURCE

while (<SOURCE>) # < > is a line input operator
{
     # input now goes to $_
     print $_;
}

close(SOURCE);  # for good measure

The second script performs a simple string comparison and skips blank lines.

# readFile2.pl

# program opens a file, reads it, and prints
# the content to STDOUT

# first, create a variable that holds the path to the source file

$source = "test.txt"; # assuming the file is in same dir as the script

# second, create a filehandle named 'SOURCE':
# filehandles are created with the open()  function;
# the first parameter is the name of the filehandle,
# the second is the path and name of the file

open(SOURCE, "<$source");  # < is optional for reading files

# get input from SOURCE

while (<SOURCE>) # < > is a line input operator
{
     # same as before, but now filter blank lines
     next if ($_ eq "\n");
     print $_;
}

close(SOURCE);  # for good measure

If the line-input operator is used in a list context, it returns a list in which each element is a line of the source file. The following script reads a file into an array and then iterates over the array.

# readFile3.pl

# program opens a file, reads it into an array, sorts it by line,
# and prints the content to STDOUT

# first, create a variable that holds the path to the source file

$source = "test.txt"; # assuming the file is in same dir as the script

# second, create a filehandle named 'SOURCE':
# filehandles are created with the open()  function;
# the first parameter is the name of the filehandle,
# the second is the path and name of the file

open(SOURCE, "<$source");  # < is optional for reading files

@file = <SOURCE>; # read file content into array @file

close(SOURCE); 

@sorted = sort(@file); # sort file content by line

for ($i = 0; $i < @sorted; ++$i)
{
  print $sorted[$i]; # print in sorted order
}

Reading from STANDARD IN

Reading input from the keyboard works just the same way, except that the filehandle is set to <STDIN>. The input, or "diamond," operator <  > passes each line of keyboard input to the default input variable, $_. The diamond operator can also be used on its own as in: while(< >), but has a special use for inputting multiple files at runtime.