I am trying to write a script that automatically goes through c code and checks for unclosed brackets and other such stuff. I would like to be able to count the brackets in the programs. I tried writing something like:
#!/usr/bin/perl -w
open (LOG, ">>threadcheck.log") || &Error($!);
$now = localtime; # "Thu Oct 13 04:54:34 1994"
print LOG "Log Created on $now \r\n";
my $lines = 0;
foreach $FILE (<*.c *.C *.cpp *.CPP *.h *.H>) {
open (FIL, ">>$FILE") || die $!;
print LOG "File $FILE was opened on $now \r\n";
while (<FIL>)
{
$lines++;
my $brackets;
foreach $word ( split )
{
if ($word =~ '{' )
{
print LOG "New open bracket \r\n";
$brackets++;
}
if ($word =~ '}' )
{
print LOG "New closed bracket \r\n";
$brackets--;
}
print LOG "Number of unclosed brackets in file $FILE: $brackets\r\n";
}
print LOG "Number of lines in file $FILE: $lines\r\n";
}
close FIL
}
close LOG
#####
The problem is, the LOG gets written with "File $FILE was opened on $now \r\n", but nothing else! I've tried writing things to each file on the order of:
while (<FIL>)
{
print FIL "/* I opened you on $now for bracket check*/
...
}
and it worked fine. Do you have any clue what I am doing wrong?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.