Why does my perl script behave differently if I run it from the icon vs running it from the command line?

157 Views Asked by At

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:

  1. Take an html file, copy it, and give it an .xml extension.

  2. Run tidy.exe on the newly formed .xml file to make it well-formed xml.

  3. 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);
    }
  }
}
2

There are 2 best solutions below

4
Borodin On

The reason your program isn't working via a shortcut may be that it is looking for HTML files in the wrong directory. When you run perl tidy_cleanup.pl from the command line it looks in your current working directory, however when you set up a shortcut you need to specify the current directory in the field marked Start in:.

However, as I said in my comment, you are processing only a single line of the file when you copy from HTML to XML because you close the file handles inside the while loop.

This is how I would write what I think you want.

use strict;
use warnings;
use autodie;

use File::Copy 'copy';

my $tidy = 'C:\Tidy\tidy.exe';
die "'tidy.exe' not found" unless -f $tidy;

for my $html_file (glob '*.html') {

  (my $xml_file = $html_file) =~ s/\.html\z/.xml/;
  copy $html_file, $xml_file;

  print qq{Tidying "$xml_file"\n};

  qx{"$tidy" --output-xml yes --numeric-entities yes --doctype omit --quote-nbsp no -asxml -utf8 -numeric -m "$xml_file"};

  print "Finished running tidy\n\n";

  (my $v2_file = $xml_file) =~ s/\.xml\z/_v2.xml/;
  open my $xml_fh,  '<', $xml_file;
  open my $v2_fh,   '>', $v2_file;

  print qq{Copying "$xml_file" to "$v2_file"\n};

  while (<$xml_fh>) {
    s/\s*xmlns="[^"]+"//;
    s/\s*xml:lang="[^"]+"//;
    print $v2_fh $_;
  }

  print "Copy complete\n\n";
}
0
1723842 On
use strict;
use warnings;
use File::Basename;
use FileHandle;

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(OUT);
my $xmltidy = "c:\\Tidy\\tidy.exe --output-xml yes --numeric-entities yes --doctype omit --quote-nbsp no -asxml -utf8 -numeric -m \"$newfile\"";
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);
}
}    
}
close(IN);
}
}