Gossamer Forum
Home : General : Perl Programming :

Count number of "tests" in a file?

Quote Reply
Count number of "tests" in a file?
Is there a way to simply count the number of "tests" or "routines" in a perl file?

I could do a count of "if's" perhaps...

(I run a script that does some error checking comprised of if/then statements and I thought it'd be really cool to be able to say "450 test ran - no errors found" or something.)
Quote Reply
Re: [Watts] Count number of "tests" in a file? In reply to
I believe the following is what you want.

Code:
open(FILE, "./file.txt");
while (<FILE>)
{
$counter++ if ($_ =~ /tests|routines/i);
}
close(FILE);
print "Found 'test' or 'routines' $counter times.\n";
Quote Reply
Re: [Watts] Count number of "tests" in a file? In reply to
It might be worth a look at the Test:: modules on CPAN.
Quote Reply
Re: [xtremenw] Count number of "tests" in a file? In reply to
I think that will accomplish the task. I was secretly hoping there was some kind of global variable that would indicate the number of commands Perl has "received" such as open, if, else, close, unlink, print - stuff like that. However I think simply counting the number of "if's" will work for my needs.

Thanks!