H
Henry Law
I can see from Googling that this is a common area of difficulty for
the inexperienced; but the workrounds that I have found don't produce
the expected results. Can someone put me straight?
Using XML::Twig I am reading in an XML file, adding an element to it
and writing it out to a new file on disk. The problem comes with the
coding of the file handle which represents the open file. Here's a
distilled test case:
---------------------
use strict;
use warnings;
use XML::Twig;
# Get the XML file
my $fdtwig = new XML::Twig;
$fdtwig->parsefile('F:\p\NFConfig.XML');
my $fd = $fdtwig->root;
# Create a new twig and paste it into the XML doc
my $stuff = "somestuff";
my $elt = new XML::Twig::Elt('stuff',$stuff);
$elt->paste('last_child',$fd);
# Write the modified XML document to a new file
open (XMLOUT, 'F:\p\xmlout.xml');
unless ($fd->print(XMLOUT)) {
print "Failed to write modified XML file:$!\n";
}
close XMLOUT;
---------------------
With the code as pasted above Perl objects to the "print" statement
with "Bareword "XMLOUT" not allowed while "strict subs" in use at
tryit.pl line 20."
But the three possible workrounds that Google found all produce error
messages; namely:
(1) Line 20 as unless ($fd->print(*XMLOUT)) {
Error: "invalid pretty print style '' at tryit.pl line 20" (Which is
an error from XML::Twig, who is assuming that *XMLOUT is a pretty
print specification. That's odd too, since the first argument to the
twig's "print" method is the file handle)
(2) Line 20 as unless ($fd->print(\*XMLOUT)) {
Error: "Failed to write modified XML file:Bad file descriptor"
(3) Lines 20/21 as open ("XMLOUT", 'F:\p\xmlout.xml');
unless ($fd->print("XMLOUT")) {
Error: "invalid pretty print style 'xmlout' at tryit.pl line 20"
again.
Can someone point me in the right direction to fix the coding problem?
Henry Law <>< Manchester, England
the inexperienced; but the workrounds that I have found don't produce
the expected results. Can someone put me straight?
Using XML::Twig I am reading in an XML file, adding an element to it
and writing it out to a new file on disk. The problem comes with the
coding of the file handle which represents the open file. Here's a
distilled test case:
---------------------
use strict;
use warnings;
use XML::Twig;
# Get the XML file
my $fdtwig = new XML::Twig;
$fdtwig->parsefile('F:\p\NFConfig.XML');
my $fd = $fdtwig->root;
# Create a new twig and paste it into the XML doc
my $stuff = "somestuff";
my $elt = new XML::Twig::Elt('stuff',$stuff);
$elt->paste('last_child',$fd);
# Write the modified XML document to a new file
open (XMLOUT, 'F:\p\xmlout.xml');
unless ($fd->print(XMLOUT)) {
print "Failed to write modified XML file:$!\n";
}
close XMLOUT;
---------------------
With the code as pasted above Perl objects to the "print" statement
with "Bareword "XMLOUT" not allowed while "strict subs" in use at
tryit.pl line 20."
But the three possible workrounds that Google found all produce error
messages; namely:
(1) Line 20 as unless ($fd->print(*XMLOUT)) {
Error: "invalid pretty print style '' at tryit.pl line 20" (Which is
an error from XML::Twig, who is assuming that *XMLOUT is a pretty
print specification. That's odd too, since the first argument to the
twig's "print" method is the file handle)
(2) Line 20 as unless ($fd->print(\*XMLOUT)) {
Error: "Failed to write modified XML file:Bad file descriptor"
(3) Lines 20/21 as open ("XMLOUT", 'F:\p\xmlout.xml');
unless ($fd->print("XMLOUT")) {
Error: "invalid pretty print style 'xmlout' at tryit.pl line 20"
again.
Can someone point me in the right direction to fix the coding problem?
Henry Law <>< Manchester, England