I'm trying to add newlines to each <> line in the XML output of XML::LibXML
from https://stackoverflow.com/a/2934794/19508169.
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 $tag = $doc->createElement($name);
    my $value = $elements{$name};
    $tag->appendTextNode($value);
    $root->appendChild($tag);
}
$doc->setDocumentElement($root);
print $doc->toString();
But, when I tried this code, I got the result without newlines:
%> perl test2.pl
<?xml version="1.0" encoding="utf-8"?>
<my-root-element some-attr="some-value"><color>blue</color><metal>steel</metal></my-root-element>
I expected the below:
<?xml version="1.0" encoding="utf-8"?>
<my-root-element some-attr="some-value">
    <color>blue</color>
    <metal>steel</metal>
</my-root-element>
How to add newlines for each XML output?
If add " \n" into print $doc->toString(); as print $doc->toString(), " \n";, it does not work.