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

Discussion in 'Perl Misc' started by Henry Law, Apr 23, 2004.

  1. Henry Law

    Henry Law Guest

    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
    Henry Law, Apr 23, 2004
    #1
    1. Advertising

  2. 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
    Glenn Jackman, Apr 23, 2004
    #2
    1. Advertising

  3. Henry Law

    Paul Lalli Guest

    Re: Object method and file handle creates "bareword" error. How tocode?

    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
    Paul Lalli, Apr 23, 2004
    #3
  4. Henry Law

    Uri Guttman Guest

    >>>>> "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
    Uri Guttman, Apr 23, 2004
    #4
  5. Henry Law

    Henry Law Guest

    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
    Henry Law, Apr 23, 2004
    #5
  6. Henry Law

    Henry Law Guest

    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
    Henry Law, Apr 25, 2004
    #6
  7. Re: Object method and file handle creates "bareword" error. Howto code?

    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
    Michel Rodriguez, Apr 25, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mt35
    Replies:
    8
    Views:
    101
  2. Replies:
    4
    Views:
    347
    Michele Dondi
    May 10, 2006
  3. Gunnar Hjalmarsson

    Bareword errors?

    Gunnar Hjalmarsson, Nov 28, 2006, in forum: Perl Misc
    Replies:
    31
    Views:
    337
    Martijn Lievaart
    Dec 9, 2006
  4. Ronny
    Replies:
    11
    Views:
    316
    comp.llang.perl.moderated
    May 8, 2008
  5. DaLoverhino

    bareword question

    DaLoverhino, Mar 30, 2009, in forum: Perl Misc
    Replies:
    6
    Views:
    132
    J├╝rgen Exner
    Apr 6, 2009
Loading...

Share This Page