This is only the second perl script I have written, so any constructive help/advice would be greatly appreciated. Also, note that I am working on a Windows machine, using Strawberry Perl. I am aware that a Tidy module exists for Perl, but (for reasons that aren't worth explaining in this note) would prefer to call tidy.exe from the script, as opposed to using the module.
What I want my perl script to do:
Take an html file, copy it, and give it an .xml extension.
Run tidy.exe on the newly formed .xml file to make it well-formed xml.
Strip the xhtml namespace from the newly created, well-formed .xml file
When I run it from the command line using the following command G:\TestFolder>perl tidy_cleanup.pl
it produces the desired result. However, when I fire the script from the icon, it skips step 2 listed above. Based on the code posted below, do you have any idea why it behaves this way?
Here is my code:
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
use FileHandle;
my $basename;
my @files = glob("*.html");
foreach my $file (@files) {
my $oldext = ".html";
my $newext = ".xml";
my $newerext = "v2.xml";
my $newfile = $file;
$newfile =~ s/$oldext/$newext/;
my $newerfile = $newfile;
$newerfile =~ s/$newext/$newerext/;
open IN, $file or die "Can't read source file $file: $\n";
open OUT, ">$newfile" or die "Can't write on file $newfile: $!\n";
print "Copying $file to $newfile\n";
{while(<IN>)
{
print OUT $_;
close(IN);
close(OUT);
}
my $xmltidy = "for \%i in ($newfile) do c:\\Tidy\\tidy.exe --output-xml yes --numeric-entities yes --doctype omit --quote-nbsp no -asxml -utf8 -numeric -m \"\%i\"";
system($xmltidy);
print "\nfinished running tidy \n\n";
}
{
open NEWIN, "$newfile" or die "Can't read source file $newfile: $!\n";
open NEWOUT, ">$newerfile" or die "Can't write on file $newerfile: $!\n";
print "Copying $newfile to $newerfile\n";
{
while (<NEWIN>) {
if ( /(\<html)( xmlns="http:\/\/www.w3.org\/1999\/xhtml" xml:lang="en-GB")(.*)/ ) {
print NEWOUT "<html$3";
}
else {
print NEWOUT $_;
}
}
close(NEWIN);
close(NEWOUT);
}
}
}