# ... my $twig = XML::Twig->new(); while (<$fh>) { chomp; my $msg = {}; $twig->parse($_); for my $child ($twig->root->children) { my $handler = $child->tag; if ($dispatch{$handler}) { $dispatch{$handler}->($child, $msg); } else { die "Haven't written handler for '$handler' yet"; } } # do something with $msg }