Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: XML-Twig paste

by Anonymous Monk
on Oct 16, 2012 at 20:01 UTC ( #999415=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://999415]
help
Chatterbox?
[Lady_Aleena]: What?! Can't stat ~/Music: No such file or directory? The ~/ doesn't expand?
[Lady_Aleena]: And the env variable $HOME doesn't work either. !@#$
[shmem]: no, that's a shell feature. Use $ENV{HOME}
[marioroy]: my $ans = qx{ find \$HOME -name "*.pl" -exec ls -l {} \\; };
[marioroy]: must backslash $ inside qx
[Lady_Aleena]: I haven't written perl for a while, I've been trying to do things at the command line. I'm having to relearn a lot.
[marioroy]: LA can you post the qx find again
[Lady_Aleena]: Things I would have written a script for while I was on Windows, I can run simply on the command line.

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2017-04-23 21:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (432 votes). Check out past polls.