Below is a snippet of code that I wrote to simply insert XML data where appropriate. I've used XML::Parser, and found it to complex for what I needed.
Any suggestions on code improvement?
#!/usr/bin/perl
use warnings;
use strict;
open (READ, "test.xml") || die "ERROR: $!\n";
my @array = <READ>;
close READ;
open (WRITE, ">new.xml") || die "ERROR: $!\n";
foreach (@array) {
if ($_ =~ /\<\!\-\- Testing XML \-\-\>/){
print WRITE '<bar>', "\n";
print WRITE '<name> TEST </name>', "\n";
print WRITE '<type> Foo </type>', "\n";
print WRITE '<!-- PDP Status -->', "\n";
print WRITE '<unknown_sec> 0 </unknown_sec>', "\n";
print WRITE '</bar>', "\n\n";
}
if ($_ =~ /\<\/ Test Tag\>/) {
print WRITE '<bar><value> TEST </value>';
print WRITE '</bar>', "\n";
}
if ($_ =~ /\<\/v\>\<\/row\>\n$/) {
$_ =~ s/\<\/row\>\n$//;
$_ .= '<v> UnKnown </v></row>' . "\n";
print WRITE $_;
}
else {
print WRITE $_;
}
}
close WRITE;
Thanks
2003-05-01 edit ybiC: retitle from "Code efficiency ?"
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|