http://www.perlmonks.org?node_id=999415


in reply to XML-Twig paste

Processing XMl efficiently with XML::Twig - Section 4 shows this that works
#!/usr/bin/perl -- use strict; use warnings; use XML::Twig; my $objectsTwig = XML::Twig->new; $objectsTwig->xparse('<objects />'); print $objectsTwig ->toString, "\n"; my $OBJECT_ROOT = $objectsTwig->root; for( 1 .. 10 ){ XML::Twig::Elt->new( 'object' )->paste( 'last_child', $OBJECT_ROOT ); } print $objectsTwig ->toString, "\n"; __END__ <objects/> <objects><object/><object/><object/><object/><object/><object/><object +/><object/><object/><object/></objects>

A root on the other hand seems doesn't like to be cut/paste while it is a root

#!/usr/bin/perl -- use strict; use warnings; use XML::Twig; my $objectsTwig = XML::Twig->new; $objectsTwig->xparse('<objects />'); print $objectsTwig ->toString, "\n"; my $OBJECT_ROOT = $objectsTwig->root; for( 1 .. 10 ){ my $object = XML::Twig->new; $object->xparse('<object />'); my $orphan = $object->root; $object->set_root(undef); ### crucial $orphan->cut; $orphan->paste('last_child', $OBJECT_ROOT ); } print $objectsTwig ->toString, "\n"; __END__ <objects/> <objects><object/><object/><object/><object/><object/><object/><object +/><object/><object/><object/></objects>

Bug? Intended behaviour? I don't know , but doesn't seem too important to me :)

Replies are listed 'Best First'.
Re^2: XML-Twig paste
by Anonymous Monk on Oct 16, 2012 at 20:05 UTC

    Yup, if you  warn  $root->cut; you'll get undef, naturally

    sub cut { my $elt= shift; my( $parent, $prev_sibling, $next_sibling, $last_elt); # you can't cut the root, sorry unless( $parent= $elt->{parent}) { return; }

    So whatever the reason is, its intended