#!/usr/bin/perl -0777 # This is a script to count the number of lines of code in a C file my @lines = (); my $numofifs = 1; my $commentstarted = 0; print "@inputlines"; while(<>) { s/\/\*[^\*\/]*\*\/?/cOmMeNt/gms; s/?/cOmMeNt/gms; s/\n/:/g; @tmp = split(/:/); for(@tmp) { if (/cOmMeNt/) { unless(/cOmMeNt/ ) { $_ = $_ . "\n"; push(@lines, $_); next; } else { if((/[\S]+[\s]*cOmMeNt/) || (/cOmMeNt[\s]*[\S]+/)) { $_ = $_ . "\n"; push(@lines, $_); } } } else { #slurp #if 0 comments if( (/[\s]*#[\s]*if[\s]+0/) || ($commentstarted == 1) ) { if ( $commentstarted == 0) { $commentstarted = 1; next; } if(/[\s]*#[\s]*if/) { $numofifs = $numofifs + 1; next; } unless(/[\s]*#[\s]*endif/) { next; # slurp the comments } else { # we found an endif, but we don't know if it is the right one... $numofifs = $numofifs - 1; if($numofifs == 0) { $commentstarted = 0; $numofifs = 1; # found the end of comment , so just fall thro' } next; } } #leave out // comments and # comments if( (/[\s]*\/\//) || (/[\s]*#/)) { next; } #leave out blank lines if(/^[\s]*$/) { next; } # Now leave out =head section till =cut in perl scripts if( (/^=head/) || ($commentstarted == 1) ) { if ( $commentstarted == 0) { $commentstarted = 1; next; } unless(/^=cut/) { next; # slurp the comments } # Now found cut, just fall thro' } $_ = $_ . "\n"; push(@lines, $_); my @lines = (); } } } print "The number of lines of code in file is  " . @lines . "\n"; exit @lines; =head1 countlines.pl Countlines - A script to count the number of lines of code in a C,C++,Java,perl,shell program file or html file =head1 DESCRIPTION This script leaves out the blank lines, C style comments, C++ style comments and #if 0 style comments and counts the remaining lines in the C/C++ source file. Something similar is done for perl and shell scripts. Theresult is printed to the terminal. The input is STDIN which means that a file can be redirected through a pipe or given in the command line. =head1 README If there is any text in this section, it will be extracted into a separate README file. =head1 PREREQUISITES None =head1 COREQUISITES None =pod OSNAMES any =pod SCRIPT CATEGORIES Educational/ComputerScience =cut