#!/usr/bin/perl # name: Code or Comment? # author: Matt Rankin # purpose: Determines the ratio of actual code to comments in a program. # details: Lines beginning with (/ * #) are evaluated as comment, lines # beginning with any other character are evaluated as code. # usage: cdcm file 1,[file 2,...file n] (wildcard allowed) foreach(@ARGV){ $code=0,$comment=0,$ratio=0; open(FILE,"<$_") or die("\"$_\" caused an Epic File Error!\n"); print("\n$_\n"); &match while(); print("Lines of code: $code\nLines of comment: $comment\n"); unless($comment==0){ $ratio=$code/$comment; print(sprintf "Code/Comment ratio: %.2f\n",$ratio); } close(FILE); } sub match{ if(m/^\s*[\/\*#]/){ $comment++; }elsif(!/^\s*$/){ $code++; } }