http://www.perlmonks.org?node_id=254628

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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 ?"