#!/usr/bin/perl

#lc.pl - line count

use strict;
use warnings;

my $lastFileName = ''; #no previous file name, yet
my $lineCount;		#lines in current file
my $totalCount;		#lines in all fies

if (!@ARGV) { #if no command line arguments print the usage
    #@ARGV is a special variable that is pre-populated by Perl when a 
    # script starts.  It receives the command line arguments that 
    # follow the script name.  It does not include the script name 
    # itself.  
    print "Usage: lc.pl fileName {,fileName} * \n";
    exit(0);
}

while (<<>>) {	#get each line of each file in turn
    if ($ARGV ne $lastFileName) { #a 'new' file being read, so show last
        # $ARGV is set by Perl to the name of the file being read
        if ($lastFileName) { #assuming there was a last name to show
            print "$lastFileName: $lineCount\n";
        }
        $lastFileName = $ARGV; #so we know when new file seen
        $lineCount = 0; #new file, so no lines yet
    }
    $lineCount++;	#one more line
    $totalCount++;
}
#at the end of the last file there was no "next" file to trigger the 
# print of the stats, so do it now.
print "$lastFileName: $lineCount\n"; #the last was not followed by a new one
print "TOTAL: $totalCount\n";

exit(0);	#we did it
