use strict; use XML::Twig; my $string =<

here the paragraph comes

here the paragraph comes

here the paragraph comes

here the paragraph comes

here the paragraph comes here the paragraph comes here the paragraph comes here the paragraph comes here the paragraph comes here the paragraph comes here the paragraph comes here the paragraph comes here the paragraph comes here the paragraph comes here the paragraph comes here the paragraph comes EOXML my $seen_a = 0; my $twig = new XML::Twig( twig_handlers => { p => sub { $_->add_id() }, a => sub { $_->set_id_seed('sec') unless $seen_a++ ;$_->add_id() }, }, pretty_print => 'indented' ); $twig->set_id_seed( "fig" ); $twig->parse($string); print $twig->sprint;