richill has asked for the wisdom of the Perl Monks concerning the following question:
I've got a query quite similar to the "HTTP::Request pipe through regex" I'm actually trying to search through a lot of html files and perform a search and replace. The strings to be replaced and their replacements are contained in an xml file.
I've managed to get this working, but this is no guarantee its any good. Could you please have a look at it and tell me if there's a better way of doing this.
There are some improvements I'd like to do soon.
The data orginally comes from and excel spreadsheet so converting it to xml using perl would be nice.
Im outputing to stout at the moment but I'll add another file handle later.
For now Im just interested in coding in a better style. I'm worried that creating a new XML::Simple()xml object for every line is a bit wasteful and that I can edit none html files. Is there anything else? Thanks
use Cwd; use XML::Simple; use Data::Dumper; my $dir = "files/html"; my $xmlfile = ""; # need a full filepath because a relative filepath +affected by the File::Find changing the current directory my $file; my $t =0; #### search all files in the directory and perform a search and replac +e using an xml file #### as the sourse of the matched string and the replacement string. use File::Find; find(\&searchlines, $dir); ################### functions ####################### #### searchlines uses a filehandle to open up all the files and searc +h them line by line sub searchlines { #print "$File::Find::name \n"; return unless -f $_; open(FILE,$_) || die "Cannot open $_ \n"; my @data = <FILE>; foreach my $line (@data){ ## pass the curent line of text to the next function ## xmlfeed($line); } close(FILE); } #### xmlfeed uses the xml file to get a list of the strings to repla +ce sub xmlfeed { my $linedata = shift @_; #print $linedata; # create object $xml = new XML::Simple(); # read XML file $data = $xml->XMLin($xmlfile); # processes the xml file one sheet at a time # <Sheet1> # <OriginPage></OriginPage> # <LinkToPage></LinkToPage> # <LinkToPageStatus></LinkToPageStatus> # <LinkToPageTitle></LinkToPageTitle> # <New_location></New_location> # </Sheet1> foreach $e (@{$data->{Sheet1}}) { #print $e->{Sheet1}, "\n"; # print "LinkToPage: ", $e->{LinkToPage}, " \nNew page +", $e->{New_location}, "\n"; &match($linedata , $e->{LinkToPage}, $e->{New_location +} ) #print "\n"; } } #### matches the current line against the string in <LinkToPage></Lin +kToPage> # performs a substitution on the stout. sub match { my $in = shift @_; my $originalURL = shift @_; my $newURL = shift @_; #$in =~ s/$originalURL/$newURL/; #$in =~ s/$originalURL/$newURL/ #print "$originalURL, $newURL\n" ; if($in =~ m/$originalURL/) { $t++; print "Matched x " . "$t \n" ; print "$File::Find::name \n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: using xml and perl to perform a search and replace on html files
by wfsp (Abbot) on Mar 10, 2007 at 10:38 UTC | |
by richill (Monk) on Mar 11, 2007 at 02:03 UTC | |
by wfsp (Abbot) on Mar 11, 2007 at 09:52 UTC | |
by richill (Monk) on Mar 11, 2007 at 16:33 UTC |