Hello, monks. I'm trying to use the File::Find module to add a line to a file in a directory if that directory also contains a file with the extension
.old. When I run the script, the preprocess and postprocess functions never execute. Here is the script:
#!/usr/bin/perl
use File::Find;
my $old_flag = 0;
find (
{
wanted => \&process,
preprocess => \&preprocess,
postprocess => \&postprocess
},
'/home/greg/mydir'
);
sub process {
print "Processing\n";
if (/.*\.old$/) {
$old_flag = 1;
}
}
sub preprocess {
print "Pre-Processing\n";
my @file_list = @_;
$old_flag = 0;
return @file_list;
}
sub postprocess {
print "Post-Processing\n";
my $old_CPB = "CPB.old";
my $new_CPB = "CPB";
if ($old_flag == 1) {
if (rename $new_CPB, $old_CPB) {
open IN, $old_CPB or die "Unable to open input CPB: $!";
open OUT, ">$new_CPB" or die "Unable to open output CPB: $!";
while (<IN>) {
chomp;
if (/^cat.*/) {
print OUT "cat\t/kat/src/all/bld_pipe.4go \\\n";
s/^cat\t/\t/;
}
print OUT "$_\n";
}
} else { #not found
return;
}
}
close IN;
close OUT;
}
Can you help me figure out why the code isn't working as I expect? I am using Perl 5.005_03 on AIX.
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.