use warnings; use strict; use XML::Twig; my $str = <<"XML"; \r \r \r \r \r cuff practice.dat\r \r \r XML # Troublesome code print "******* Troublesome version\n"; my @fileList = qw(first second); my $sub = bindArgs (\&insertFiles, \@fileList); my $twig = XML::Twig->new ( twig_roots => {'sourceFiles' => $sub,}, # Insert file list twig_print_outside_roots => 1, # Print the rest ); $twig->parse ($str); sub bindArgs { my ($sub, @args) = @_; return sub {$sub->(@args, @_);}; } sub insertFiles { my ($sourceFiles, $twig, $node) = @_; for (@$sourceFiles) { my $eFile = $twig->root->new (sourcefile => $_); # create the element $eFile->paste (first_child => $node); } $node->print (); } #### ******* Troublesome version secondfirst cuff practice.dat