my $data; # a ref to the data structure XML::Twig->new( twig_handlers => { foo => sub { foo( @_, $data); }) ->parsefile( 'my.xml'); sub foo { my( $twig, $foo, $data)= @_; # update the data referenced by $data }