Perl (Scripting) - XML::Twig output to a file

by
Jeremy Canfield |
Updated: March 19 2021
| Perl (Scripting) articles
Let's say you want to create an XML file using XML::Twig. In this example, foo.xml is created.
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
my $twig = new XML::Twig;
$twig->parse('<?xml version="1.0" encoding="UTF-8" standalone="no"?><acme><name>Bugs Bunny</name></acme>');
my $file = "/path/to/foo.xml";
if (open my $fh, ">", $file) {
$twig->print($fh);
close $fh;
}
else {
print "failed to open $file \n";
exit 1;
}
Did you find this article helpful?
If so, consider buying me a coffee over at