pilot_vijay: This is an adaptation on the method I proposed last night in the Chatterbox when you asked this question. Also remember the suggestion to read perlop, perlretut and perlre. Therein you will gain a greater understanding of the concepts at work.
The method I proposed last night was to put your RE patterns as values in a hash, "quoted" using the qr// mechanism so that possibly the RE's could be precompiled for efficiency. My earlier suggestion was based on the idea that the keys to the hash would be the replacement value. Iterating over the hash would produce replacement/pattern pairs. But that was based on the idea that you had multiple replacement values. Now that the question is better defined, I see that there is little need for a hash, and an array is a good choice.
That said, here's my recommendation:
# Prebuild the RE patterns for efficiency's sake.
my @patterns = ( qr(pattern1),
qr(pattern2),
qr(pattern3),
qr(pattern_n) );
# Use paragraph mode in case your pattern spans multiple lines.
# This may need to be combined with the /s switch on your
# regexp. Since we don't know what the patterns look like,
# I'll use this mechanism to be on the safe side.
{
local $/ = "\n\n";
while ( my $paragraph = <FILE> ) {
$paragraph =~ s/$_/pagingRAC/gs foreach @patterns;
print TEMP_OUT $paragraph;
}
}
My example prints the results of the substitution out to a temporary output file that presumably you open earlier in the script. And again, paragraph mode is used so that patterns that span linebreaks will work ok. But you may not need that part. Also, if your list of RE's is short, you may just join them all together into a big alternation chain. And if the file you're running through is small enough, you may just want to slurp it in, though that doesn't scale well.
Is this question related to the barrage of questions you posted a month or so ago about converting PDF files to HTML, DOC files to HTML, and so on? If so, that might provide additional context that we could relate to in giving helpful answers.
Update: Fixed the foreach. Thanks Roy Johnson for the correction. The foreach was in my head but failed to make it to my fingertips. ;)
|