in reply to Re: XML::LibXML creating nodes with a string OR a node
in thread XML::LibXML creating nodes with a string OR a node

That misses the point. The calling function does not know, and the called function does not care.

The answer I have come up with is a wrapper function that does what I want XML::LibXML to do:

sub _addNode { my ($parent, $child) = @_; if(ref($child) eq 'XML::LibXML::Element'){ $parent->appendChild($child); }elsif(ref($child) eq ''){ $parent->appendTextNode($child); }else{ die "'_addNode child is type: ".ref($child). " Do not know what to do with that"; } return $parent; }

Is the answer to my original question "yes" or "no"?

To be rule compliant here is the runnable code....

#!/usr/bin/perl -w use strict; use XML::LibXML; # The XML Document my $DOC = XML::LibXML->createDocument( "1.0", "UTF-8" ); # Set the root my $root = $DOC->createElement('owner'); $DOC->setDocumentElement($root); sub createOwner { if(rand() < 0.5){ return "John Smith"; }else{ my $ret = XML::LibXML::Element->new('href'); $ret->appendTextNode('http://johnsmith.com'); return $ret; } } sub _addNode { my ($parent, $child) = @_; if(ref($child) eq 'XML::LibXML::Element'){ $parent->appendChild($child); }elsif(ref($child) eq ''){ $parent->appendTextNode($child); }else{ die "'_addNode child is type: ".ref($child). " Do not know what to do with that"; } return $parent; } my $owner = &createOwner(); $root = &_addNode($root, $owner); print $DOC."\n";

Replies are listed 'Best First'.
Re^3: XML::LibXML creating nodes with a string OR a node
by Anonymous Monk on Jun 07, 2015 at 23:31 UTC
    Is the answer to my original question "yes" or "no"?

    AFAIK it is "no". A thorough reading of the documentation will give you a definite answer though.

    The calling function does not know, and the called function does not care.

    But why? If the sub createOwner is under your control, then the fix suggested by choroba solves the problem in only a few characters. I may be wrong, but I get the feeling from this and the previous questions you have posted that you are trying to bend the module to your wishes instead of accepting the API the way it is and just using it...

    But anyway, here's an interesting hack ("just enough rope" etc. etc.): In your code, replace "sub _addNode {" with "sub XML::LibXML::Element::appendTextOrElement {" and then replace "$root = &_addNode($root, $owner);" with "$root->appendTextOrElement($owner);". Does that fit your expectations better? ;-)

      But why? If the sub createOwner is under your control, then the fix suggested by choroba solves the problem in only a few characters. I may be wrong, but I get the feeling from this and the previous questions you have posted that you are trying to bend the module to your wishes instead of accepting the API the way it is and just using it...

      This is off topic, but the reason is that createOwner is just an example. I will have many functions like that one, and the nodes they are filling can take plain text, or subnodes, depending on input I do not control.

      Worik

Re^3: XML::LibXML creating nodes with a string OR a node
by Anonymous Monk on Jun 08, 2015 at 04:50 UTC

    ...what I want XML::LibXML to do

    There is a meditation about that, see XML::LibXML - WHAR HASH TREES WHAR?! and wish upon a star :)(standards aren't always sugar, sometimes they're standards)

      There is a meditation about that, see XML::LibXML - WHAR HASH TREES WHAR?! and wish upon a star :)(standards aren't always sugar, sometimes they're standards)

      Thank you. That is an interesting read. I concur with the criticisms the OP makes. I try to be more moderate in my language, but SineSwiper is bang on

      Worik