There are two problems with your script:
One (the main problem), when you are opening the file that you wish to read from, i.e.
open (FIL, ">>$FILE") || die $!;, you are using the
append operator (>>) on the $FILE, instead of the
read (<) operator. This will not allow you to read in the file line by line, in fact it will not allow you to read the file at all.
Two, as already pointed out, the regexes are problematic. You're using single quotes as delimiters, instead of the standard forward slash (which is allowable), but to do so you have to put
m in front of the first single quote, i.e.
$word =~ m'}' to let Perl know that you're going to use something other than the forward slash. Or you could just use a forward slash,
$word =~ /}/. And you have to put a backslash in front of the "{", because it's a quantifying metacharacter.
Here is a rewrite of your script that should work:
#!/usr/bin/perl -w
open (LOG, ">>threadcheck.log") || &Error($!);
$now = localtime;
print LOG "Log Created on $now \r\n";
my $lines = 0;
foreach $FILE (<*.c *.C *.cpp *.CPP *.h *.H>)
{
open (FIL, "<$FILE") || die $!; #changed ">>" to "<"
print LOG "File $FILE was opened on $now \r\n";
while (<FIL>)
{
$lines++;
my $brackets;
foreach $word ( split )
{
if ($word =~ /\{/ ) #or if ($word =~ m'\{' )
{
print LOG "New open bracket \r\n";
$brackets++;
}
if ($word =~ /}/ ) #or if ($word =~ m'}' )
{
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
higle
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.