#!/bin/perl -w use strict; use XML::Twig; my $t= XML::Twig->new; $t->parse( \*DATA); # tag all matching strings with $1 # returns the list of newly created elements my @xref=$t->root->mark( qr/(@\w+)/, 'a', { class => 'xcode'}); foreach my $xref ( @xref) { $xref->set_content( substr( $xref->text, 1) ); # remove the leading @ $xref->set_att( href => $xref->text . ".html"); } $t->print; __DATA__ text with a @reference to whichever @document I want