in reply to Counting XML Blocks in XML file and Calculating Total Amount

pjotrik is right, the example was not XML. If it were, this is a solution using XML::Rules (AZed, neither this solution nor XML::Twig has any memory problems whatsoever with this. Not all XML parsers construct a maze of objects before you can get your hands on the data.)

#!/usr/bin/perl use strict; use warnings; use XML::Rules; print "This will calculate the total amount and To no of transactions +\n"; my $totalamt = 0; my $cnt = 0; my $parser = XML::Rules->new( stripspaces => 7, rules => { _default => '', b => sub {++$cnt;return}, payment_amount => sub {$totalamt+=$_[1]->{_content};return}, }); $parser->parse(\*DATA); print "The total amount found is $totalamt \n"; print "Total Transactions are $cnt \n"; __DATA__ <a> <b> <client_account_cred>68789790390909090489</client_account_ +cred> <Payment_UTR_No>MTRIN10909890896</Payment_UTR_No> <payment_amount>700000</payment_amount> </b> <b> <client_account_cred>9033753053985392INR</client_account_c +red> <Payment_UTR_No>938573895735154</Payment_UTR_No> <payment_amount>1222706</payment_amount> </b> <b> <client_account_cred>9284723472047222INR</client_account_c +red> <Payment_UTR_No>RP JLLKL7687</Payment_UTR_No> <payment_amount>1437865.95</payment_amount> </b> </a>
or if you do not want to use file scoped lexicals (and want the parser to be able to give you the right answer even if you call it several times):
#!/usr/bin/perl use strict; use warnings; use XML::Rules; print "This will calculate the total amount and To no of transactions +\n"; my $parser = XML::Rules->new( stripspaces => 7, start_rules => { a => sub { $_[4]->{pad}{count}=0; $_[4]->{pad}{total}=0; 1; } }, rules => { _default => '', b => sub {++ $_[4]->{pad}{count}; return}, payment_amount => sub {$_[4]->{pad}{total} += $_[1]->{_content +};return}, a => sub {return %{$_[4]->{pad}}} }); my $data = $parser->parse(\*DATA); print "The total amount found is $data->{total}\n"; print "Total Transactions are $data->{count}\n"; __DATA__ <a> <b> ...