This code works fine: -------->8-------->8-------->8-------->8 use XML::Twig; $twig=XML::Twig->new(#keep_encoding=>1, twig_handlers=>{ '[@a]' => sub {warn "a handler\n"}, }); $xml=join ('', ); $twig->parse($xml); __DATA__ &ent1; -------->8-------->8-------->8-------->8 and generates: a handler However, if the keep_encoding is uncommented, then it produces this error: Can't use string ("ent1") as a HASH ref while "strict refs" in use at /local/share/oed/lib/perl/XML/Twig.pm line 1251, line 6.