use XML::Rules; my $parser=new XML::Rules(rules=>{}); ... $XMLContent .= $parser->toXML(packet_ref => {packID => [$foo], packInfo => [$bar]}) . "\n"; # or, with formatting # $XMLContent .= $parser->toXML(packet_ref => {packID => [$foo], packInfo => [$bar]}, 0, ' ') . "\n";