Skip to content

Instantly share code, notes, and snippets.

@pdl
Created February 24, 2014 14:03
Show Gist options
  • Save pdl/9188955 to your computer and use it in GitHub Desktop.
Save pdl/9188955 to your computer and use it in GitHub Desktop.
Quickly create a new element using XML::LibXML
sub element {
my $name = shift;
my $element = XML::LibXML::Element->new($name);
my $next = shift;
if (ref $next eq ref {}){
foreach my $attrName (keys %$next){
$element->setAttribute ($attrName, $next->{$attrName});
}
$next = shift;
}
if (defined $next) {
my $addChild = sub {
my $child = shift;
unless (ref $child){
$child = XML::LibXML::Text->new($child)
}
$element->addChild($child)
};
if (ref $next eq ref []){
$addChild->($_) for @$next;
}
else{
$addChild->($next)
}
}
return $element;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment