The loop over the
%FORM hash keys is really confusing. I think you don't need it. From your description you want to check the lines from the input file against the hash keys, but you never do that. I think you need something like that (not tested):
open OLD, $file or die "Can't open $file:$!\n";
open NEW, ">$file.new" or die "Can't open temp file $file.new:
+ $!\n";
my $found =0;
while (<OLD>) {
if ($FORM{$_}==1) {
$found =1;
next;
}
print NEW;
}
}
close NEW;
close OLD;
if ($found) {
rename ("$file.new", $file) or die "Can't rename new $file: $
+!\n";
} else {
print "I didn't find !\n";
}
What I changed:
- if ($FORM{$_}==1) this is checking if the line is a key in the %FORM hash and has a value of 1 (you might change it to if(exists($FORM{$_})) if you need to check just existence).
- Closing the NEW and OLD filehandles.
You could look at File::Atomic if you run this concurrently.
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.