Object method and file handle creates "bareword" error. How to code?

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
 
G

Glenn Jackman

Henry Law said:
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; [...]
# 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;
[...]

The XML::Twig docs show:
print ($optional_filehandle, %options)
Prints the whole document associated with the twig. To
be used only AFTER the parse.

So, $optional_filehandle being a scalar, try this:

my $xmlout;
# you did want to *write* to this file, right?
open $xmlout, '>', 'f:/p/xmlout.xml';
$fd->print($xmlout);
close $xmlout;

Oh, perhaps the filehandle is optional but the options are mandatory.
In that case add an empty hash to the end:

$fd->print($xmlout, ());
 
P

Paul Lalli

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."
Completely untested, the first thing I'd try is using a lexical
filehandle, rather than the bareword:

my $fh;
open ($fh, 'F:\p\xmlout.xml');
unless ($fd->print($fh)){
pritn "Failed to write modified XML file: $!\n";
}
close $fh;

__END__

I honestly have no idea if it'd work, but it is definately what I'd try.

Paul Lalli
 
U

Uri Guttman

GJ> The XML::Twig docs show:
GJ> print ($optional_filehandle, %options)
GJ> Prints the whole document associated with the twig. To
GJ> be used only AFTER the parse.

GJ> Oh, perhaps the filehandle is optional but the options are mandatory.
GJ> In that case add an empty hash to the end:

GJ> $fd->print($xmlout, ());

that is no different from $fd->print($xmlout). the called method
couldn't tell the diff from those two calls.

and they are called 'options' so they are optional :)

uri
 
H

Henry Law

Completely untested, the first thing I'd try is using a lexical
filehandle, rather than the bareword:

my $fh;
open ($fh, 'F:\p\xmlout.xml');
unless ($fd->print($fh)){
pritn "Failed to write modified XML file: $!\n";
}
close $fh;

.... and Glenn Jackman made the same suggestion, but also wondered
perhaps the filehandle is optional but the options are mandatory.
In that case add an empty hash to the end:

$fd->print($xmlout, ());

Well, those suggestions gave me several other things to try (starting
with putting in my missing ">" character, the lack of which won't have
helped any..) but I'm back to report that I still have no success.

This code...

my $xmlout;
open $xmlout, '>F:\p\xmlout.xml';
unless ($fdtwig->print($xmlout)) {
print "Failed to write modified XML file:$!\n";
}

.... runs but the program itself dies with "Failed to write modified
XML file:Bad file descriptor". It's the same with an empty hash for
the print options, viz

unless ($fdtwig->print($xmlout, () )) { # etc ...

I also blundered around with *$xmlout and \*$xmlout with the same
failure message. So with your help I've at least cured the syntax
error and obtained a consistent error message. Is that progress?

If you're not all bored with this already I'm hoping for more
suggestions.

Henry Law <>< Manchester, England
 
H

Henry Law

I also blundered around with *$xmlout and \*$xmlout with the same
failure message. So with your help I've at least cured the syntax
error and obtained a consistent error message. Is that progress?

This is getting really odd ... hoping someone can help me understand
this. I examined the blundering about options more. This code

open XMLOUT, '>F:\p\xmlout.xml';
my $rc = $fdtwig->print(\*XMLOUT);
unless ($rc) {
print "\$rc:$rc.\n";
print "Failed to write modified XML file:$!\n";
}
close XMLOUT;

in which I have split out the return from the "print" method for
inspection, WORKS but produces an error message. In other words
xmlout.xml is created, with the desired new element in it, but the
return code is a zero-length string (failure) and $! is set. Here's
the output:

F:\p>perl tryit.pl
$rc:.
Failed to write modified XML file:Bad file descriptor

Can I safely ignore the return value, would you say? I'd rather not -
what would happen if it *really* failed.

Henry Law <>< Manchester, England
 
M

Michel Rodriguez

Uri said:
GJ> The XML::Twig docs show:
GJ> print ($optional_filehandle, %options)
GJ> Prints the whole document associated with the twig. To
GJ> be used only AFTER the parse.

GJ> Oh, perhaps the filehandle is optional but the options are mandatory.
GJ> In that case add an empty hash to the end:

GJ> $fd->print($xmlout, ());

that is no different from $fd->print($xmlout). the called method
couldn't tell the diff from those two calls.

and they are called 'options' so they are optional :)

Indeed, the options are optional!

The problem in the initial post was to use XMLOUT instead of \*XMLOUT to
pass the filehandle to the sub, and then to check the return of print,
which doesn't return anything meaningfull.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top