#!/usr/bin/perl -w use strict; use XML::Twig; my $xml = XML::Twig->new( keep_spaces_in => [ 'pre' ], # safer, other tags might need to be included pretty_print => 'indented', twig_roots => { 'body' => \&insert_form_tags, }, ); $xml->parsefile('index.html'); $xml->print; sub insert_form_tags() { my ($t, $body) = @_; my $form= $body->insert( form => { method => "Post", action => "submit.cgi" }, ); $form->insert_new_elt( first_child => input => { type => "text", name => "foo_is" }); }