Skip to content

Instantly share code, notes, and snippets.

@numberwhun
Created November 28, 2011 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save numberwhun/1399635 to your computer and use it in GitHub Desktop.
Save numberwhun/1399635 to your computer and use it in GitHub Desktop.
Count lines in a file
#!/usr/bin/perl
use strict;
use warnings;
######################################################################################
# Script: count.pl
# Author: Jefferson Kirkland
#
# Description:
# The purpose of this subroutine is to take a file name as an input parameter, and
# then return to the user the count of the number of lines in that file.
#
# USAGE: line_count(filename);
#
# where "filename" is the name of the file (using absolute path if not in the same
# directory as the script being run.
######################################################################################
sub line_count{
############################################################
# Put the file name into a variable to be passed elsewhere
############################################################
my ($file) = $_[0];
############################################################
# Open the file for reading so we can count the lines
############################################################
open(FILE, "< $file");
############################################################
# Initialize the counter to zero (0)
############################################################
my $count = 0;
############################################################
# Count the lines in the file. This is pretty non-trivial.
# All it does is increment the counter once for each line
# in the file.
############################################################
while(<FILE>)
{
$count++;
}
############################################################
# Close the file as counting is completed.
############################################################
close(FILE);
############################################################
# Print the count of the number of lines in the file.
############################################################
print("$count \n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment