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";
In reply to Re^2: XML::LibXML creating nodes with a string OR a node
by worik
in thread XML::LibXML creating nodes with a string OR a node
by worik
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |