Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Object method and file handle creates "bareword" error. How to code?

Reply
Thread Tools

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

 
 
Henry Law
Guest
Posts: n/a
 
      04-23-2004
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
 
Reply With Quote
 
 
 
 
Glenn Jackman
Guest
Posts: n/a
 
      04-23-2004
Henry Law <> wrote:
[...]
> 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, ());

--
Glenn Jackman
NCF Sysadmin

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      04-23-2004
On Fri, 23 Apr 2004, Henry Law wrote:

> 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
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      04-23-2004
>>>>> "GJ" == Glenn Jackman <> writes:

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
 
Reply With Quote
 
Henry Law
Guest
Posts: n/a
 
      04-23-2004
On Fri, 23 Apr 2004 14:45:30 -0400, Paul Lalli <>
wrote:

> 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
 
Reply With Quote
 
Henry Law
Guest
Posts: n/a
 
      04-25-2004
On Fri, 23 Apr 2004 23:18:13 +0100, Henry Law <>
wrote:

>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
 
Reply With Quote
 
Michel Rodriguez
Guest
Posts: n/a
 
      04-25-2004
Uri Guttman wrote:
>>>>>>"GJ" == Glenn Jackman <> writes:

>
>
> 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.

--
Michel Rodriguez
Perl &amp; XML
http://www.xmltwig.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Dumb question: in documentation, why Object#method, and not Object.method ? Elf M. Sternberg Ruby 15 07-29-2009 01:20 AM
"Back" button on IE, creates a new Page object? Amelyan ASP .Net 2 05-25-2005 06:25 PM
Troubleshooting: re.finditer() creates object even when no match found Chris Lasher Python 8 12-18-2004 12:51 PM
File Handle Reading Blues: Rereading a File Handle for Input Dietrich Perl 1 07-22-2004 10:02 AM



Advertisments