#!/usr/bin/perl { package XML::Simple::Ordered; # subclass overriding new_hashref method use base qw(XML::Simple); use Tie::IxHash; sub new_hashref { my $self = shift; my %hash; tie %hash, 'Tie::IxHash', @_; return \%hash; } } package main; my $simple = XML::Simple::Ordered->new(); my $data = $simple->XMLin($file); #### sub collapse { my $self = shift; # Start with the hash of attributes my $attr = shift; $attr = $self->new_hashref() unless %$attr; # <-- add this line