XML::Twig->new( twig_roots => { 'div' => \&div } )->parsefile("my_file"); sub div { my ($t, $elt) = @_; # do something return 1; }