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

In my project i am reading the XML file and writing into new file updating few data in it, for which i have used hash to check if record exist, I have written the code and its working well, but i was think if there is any smart solution I mean less code to do same task!!!! here is my code:-
sub dispatch { my $HRR_resource = shift; my $HRR_wnt = shift; my $HRR_unx = shift; my $HRR_Scomponent = shift; my $HRR_Wcomponent = shift; my $Pkey; my $Wkey; my $Ukey; my $SCkey; my $WCkey; my $Flag; open(FR, "<$source_file")|| die "Unable to open input file: $sourc +e_file $!"; open(FW, ">$dest_file")|| die "Unable to open output file: $dest_f +ile $!"; while (my $line=<FR>) { $line =~tr/\r\n//d; if ($line) { $Flag = 1; if ($line =~ /PLATFORM_INDEPENDENT/){$Pkey = $line}; if ($Pkey) { $Flag = undef; $Pkey =~ s/\s*//; if($Pkey ne '</PLATFORM_INDEPENDENT>') { if ($line =~ /Id="(.*?)".*?>(.*?)</) { my $key_id = $1; my $key_val = $2; if (exists $$HRR_resource{$key_id}) { my $val = $$HRR_resource{$key_id}; $val =~ /.*?>(.*?$)/; $val = $1; $line =~ s/>.*?</>$val</; print FW "$line\n"; delete($$HRR_resource{$key_id}); } else { print FW "$line\n"; } } else { print FW "$line\n"; } } if ($line =~ /<\/PLATFORM_INDEPENDENT>/) { foreach my $addline(keys %$HRR_resource) { print FW "<RESOURCE Id=\"$addline\" $$HRR_resource +{$addline}</RESOURCE>}\n"; } print FW "$line\n"; $Pkey = undef; } } if ($line =~ /WNT/){$Wkey = $line}; if ($Wkey) { $Flag = undef; $Wkey =~ s/\s*//; if ($Wkey eq '<WNT>' && $Wkey ne '</WNT>') { if ($line =~ /Id="(.*?)".*?>(.*?)</) { my $key_id = $1; my $key_val= $2; if (exists $$HRR_wnt{$key_id}) { my $val = $$HRR_wnt{$key_id}; $val =~ /.*?>(.*?$)/; $val = $1; $line =~ s/>.*?</>$val</; print FW "$line\n"; delete($$HRR_wnt{$key_id}); } else { print FW "$line\n"; } } else { print FW "$line\n"; } } if ($line =~ /<\/WNT>/) { foreach my $addline(keys %$HRR_wnt) { print FW "<RESOURCE Id=\"$addline\" $$HRR_wnt{$add +line}</RESOURCE>}\n"; } print FW "$line\n"; $Wkey = undef; } } if ($line =~ /UNX/){$Ukey = $line}; if ($Ukey) { $Flag = undef; $Ukey =~ s/\s*//; if ($Ukey ne '</UNX>') { if ($line =~ /Id="(.*?)".*?>(.*?)</) { my $key_id = $1; my $key_val= $2; if (exists $$HRR_unx{$key_id}) { my $val = $$HRR_unx{$key_id}; $val =~ /.*?>(.*?$)/; $val = $1; $line =~ s/>.*?</>$val</; print FW "$line\n"; delete($$HRR_unx{$key_id}); } else { print FW "$line\n"; } } else { print FW "$line\n"; } } if ($line =~ /<\/UNX>/) { foreach my $addline(keys %$HRR_unx) { print FW "<RESOURCE Id=\"$addline\" $$HRR_unx{$add +line}</RESOURCE>}\n"; } print FW "$line\n"; $Ukey = undef; } } if ($Flag) { print FW "$line\n"; } } } }

Replies are listed 'Best First'.
Re: Looking for smart solution
by GrandFather (Saint) on Jun 23, 2009 at 05:51 UTC

    I don't see where you use XML::Twig. Perhaps you should?


    True laziness is hard work
Re: Looking for smart solution
by Your Mother (Archbishop) on Jun 23, 2009 at 06:34 UTC
      Thanks for recommandation and suggestion, problem is at my work place server where I suppose to run this script unfortunately those modules are not installed and I don't have the right to do that. If You guys have any suggestion regarding above code please!!! Cheers!

        local::lib solves the install problem nattily. If you have libxml installed on your box, you'll be able to install XML::LibXML fine. If not, I think XML::Twig is pure perl and has a nice perly interface to XML. Parsing XML by hand is an error prone royal pain and few here would do it for themselves let alone for a third-party. :)