You seem to be having problems with scalars vs arrays. Maybe a consequence of using jQuery in a previous life? ;--)
In any case, your sub would work if you write this:
sub Tag_Alt{
my ($Tag_move, $tagm) = @_;
my @c = $tagm->findnodes('//front/sample/');
my @children= map { $_->cut_children } @c;
foreach my $child (@children) {
$child->paste('first_child', ($tagm->findnodes('//body/'))[0]);
}
}
I would write this differently though:
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
my @samples;
my $Tag_move = XML::Twig->new(
twig_handlers =>{ # cut and store the everything within sample
sample => sub { push @samples, $_->cut_children; },
# paste the samples within the body
body => sub { foreach my $sample (@samples)
{ $sample->paste( first_child => $_); }
},
},
pretty_print => 'indented',
empty_tags => 'expand', # otherwise the empty sample is output as <sample/>
);
$Tag_move->parsefile( "input.xml")
->print;
BTW, do yourself a favour, and use strict
and warnings
. This will catch a lot of the mistakes you make. You will get errors instead of allowing unknown variables to be created silently.