#! /usr/bin/perl

use strict;
use warnings;

# required locals
my $pattern;
my $filename;
my $filehandle;

# make sure they invoked the program correctly, end if not
my $numArgs = scalar @ARGV;
if ($numArgs != 2) {
   print "Expected use is $0 pattern filename\n";
   print "to search the file for lines containing the pattern\n";
   exit 0;
} else {
   $pattern = $ARGV[0];
   $filename = $ARGV[1];
   print "Examining file $filename for lines containing pattern $pattern\n";
}

# get the pattern and filename supplied by the user
#$filehandle;

# try opening the file for reading
open $filehandle, '<', $filename
   or die "unable to open $filename for reading.\n";

# process the file, one line at a time
while (<$filehandle>) {
   # get the current line and see if it matches the pattern
   my $line = $_;
   if ($line =~ $pattern) {
      print "$line";
   }
}

# close the file
close $filehandle;

