use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new(pretty_print => "indented"); $twig->parse(''); my $root = $twig->root(); foreach my $i (qw(bar baz qux)) { my $inserted = $root->insert($i); $inserted->set_atts({bark => 'woof'}); my $second_inserted = $root->insert($i . 'second_tag'); $second_inserted->set_atts({type => 'deep'}); } $twig->print;