QUOTE(Sun751 @ 23 Jun, 2009 - 09:12 AM)

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!!!!
This one is too long for me to easily wrap my head around it, so I won't do a full refactoring this time, but that leads directly into my first suggestion: Break the code up into smaller subroutines with each do a single conceptual task. It might get a little longer, but so what? It will be much, much easier to read and to maintain.
CODE
sub dispatch
{
my $HRR_resource = shift;
my $HRR_wnt = shift;
my $HRR_unx = shift;
my $HRR_Scomponent = shift;
my $HRR_Wcomponent = shift;
This can be tightened up considerably by using list assignment instead of shift:
CODE
my ($HRR_resource, $HRR_wnt, $HRR_unx,
$HRR_Scomponent, $HRR_Wcomponent) = @_;
CODE
my $Pkey;
my $Wkey;
my $Ukey;
my $SCkey;
my $WCkey;
my $Flag;
Similarly, you can use
CODE
my ($Pkey, $Wkey, $Ukey, $SCkey, $WCkey, $Flag);
here.
CODE
open(FR, "<$source_file")|| die "Unable to open input file: $source_file $!";
open(FW, ">$dest_file")|| die "Unable to open output file: $dest_file $!";
You should use the three-argument form of open rather than the two-argument form, as it will help to protect you against certain classes of errors and security vulnerabilities:
CODE
open(FR, '<', $source_file)|| die "Unable to open input file: $source_file $!";
open(FW, '>', $dest_file")|| die "Unable to open output file: $dest_file $!";
It's also currently recommended to use lexical filehandles (e.g., open(my $fr, '<', $source_file)...) instead of typeglob filehandles (FR), but that's less of an issue.
CODE
if ($line)
{
Conditional clauses spanning multiple pages are bad news, as they obscure the flow of control. Moving the huge blocks of processing out into separate subroutines will help considerably with this, but, if you're doing a bunch of processing if $line is true and just going on to the next iteration of your loop if it's false, it's much cleaner and tighter to just use
CODE
next unless $line;
CODE
$val =~ /.*?>(.*?$)/;
$val = $1;
$line =~ s/>.*?</>$val</;
I strongly advise taking a look at CPAN (
http://search.cpan.org ) to find an appropriate module to handle the XML parsing and manipulation for you. Parsing XML using your own hand-rolled regexes is extremely difficult to get right. (Yes, I know you said your current code works with the data you've given it so far. XML contains plenty of corner cases which will eventually trip you up and cause much wailing and gnashing of teeth if your hand-rolled code proves successful enough that it one day needs to deal with data from other sources.)
As for the rest, pulling it out into subroutines, replacing the XML parsing with an appropriate CPAN module, and applying the techniques I've shown you in my previous replies should go a long way towards taming this code.