Two suggestions:
- Use File::Find with File::Glob. One for digging into sub directories, the other one for matching .shtml files. Much less coding on your own.
- When you read files, call read with a big buffer, to read in the whole shtml file by one call. This improves performance. Don't handle line by line, too slow. Memory is not an issue in your case(, unless you have huge shtml files). When you s///, use /g modifier.
- Don't consider +< in this case, as your new content is shorter than the old one. (I am not saying you cannot use it, but using it in this case, requires more coding effort)
use File::Find;
use File::Glob ':glob';
use strict;
find(\&wanted, "c:/perl58/bin"); #replace with your directory
sub wanted {
if ((-d $File::Find::name) && ($_ ne ".") && ($_ ne "..")) {
my @shtml_files = bsd_glob("*.shtml");
foreach my $shtml_file (@shtml_files) {
print $shtml_file;
open(SHTMLFILE, "<", $shtml_file);
my $buffer;
read(SHTMLFILE, $buffer, 10000); #give some big number, wh
+ich exceeds the size of all your .shtml files
close(SHTMLFILE);
$buffer =~ s/<a href="main\.php\?page=(.*?)\"/<a href="mai
+n.php?id=$1"/g;
open(SHTMLFILE, ">", $shtml_file);
close(SHTMLFILE);
}
}
}
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.