<div class="gmail_quote">On Fri, Jul 8, 2011 at 9:12 AM, Ed Hurst <span dir="ltr"><<a href="mailto:ehurst@soulkiln.org">ehurst@soulkiln.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">Having never messed with Perl, I can scarcely begin to parse this. What</div>
I need now is how to tell this script to check all the HTML files in a<br>
given directory. I saved it as <a href="http://change.pl" target="_blank">change.pl</a> and learned how to check the<br>
syntax. It's fine, but it does nothing as it stands.</blockquote><div><br></div><div>Sorry about that, Ed. Some documentation would be useful...</div><div><br></div><div>Okay, to change the HTML files run the script like this: <font class="Apple-style-span" face="'courier new', monospace">perl <a href="http://change.pl">change.pl</a> *.html</font></div>
<div><br></div><div>You will end up with a directory full of files that end with .html.new. Check a couple of the .html.new files. I never really trust my own scripts and always want to eyeball the results first. Once you're satisfied that the *.html.new files look okay, rename them to .html.</div>
<div><br></div><div>How the script works...</div><div><div> </div><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
foreach my $file (@ARGV) {<br></blockquote><div>Loop through all of the files passed on the command line.</div><div> </div><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
open my $in, "<$file";<br></blockquote><div>Open the file for reading.</div><div> </div><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
my @slurp = <$in>;<br>close $in;<br></blockquote><div>Read the entire file into memory, split apart by lines. At this point, it's just like sed.</div><div> </div><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
my $all = join( "\n", @slurp );<br></blockquote><div>Combine all of the lines into one long string. We need this to check for the pattern across lines. This is where Perl has the advantage over sed.</div><div> </div>
<blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
$all =~ s/softedges\s*at\s*softhome\s*<u></u>dot\s*net/eddie at soulkiln dot org/m;<br></blockquote><div>Replace the text. The <b>\s*</b> matches any whitespace - including a newline. </div><div> </div><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
open my $out, ">$file.new";<br>print $out $all;<br>close $out;<br></blockquote><div>Write the altered contents out to a different file. Overwriting the originals makes me nervous. I like to eyeball the changes before committing.</div>
<div> </div><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
}<br></blockquote></div><div><br></div><div>-- </div></div>Robert Wohlfarth<br><br>