I'm new Perl and Trying to make a xml document. I came across an article from How can I create XML from Perl?
#!/usr/bin/env perl
#
# Create a simple XML document
#
use strict;
use warnings;
use XML::LibXML;
my $doc = XML::LibXML::Document->new('1.0', 'utf-8');
my $root = $doc->createElement('my-root-element');
$root->setAttribute('some-attr'=> 'some-value');
my %elements = (
color => 'blue',
metal => 'steel',
);
for my $name (keys %elements) {
my $element = $doc->createElement($name);
my $value = $elements{$name};
$tag->appendTextNode($value);
$root->appendChild($element);
}
$doc->setDocumentElement($root);
print $doc->toString();
I expected that the output as below
<?xml version="1.0" encoding="utf-8"?>
<my-root-element some-attr="some-value">
<color>blue</color>
<metal>steel</metal>
</my-root-element>
but I got the result as below
% perl test2.pl
Global symbol "$tag" requires explicit package name at test2.pl line 18.
Execution of test2.pl aborted due to compilation errors.
Could you guide me please How do I resolve this problem?