XML::Twig::XPath - strange problem

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

The following short test program is the kernel of a problem I'm having
using xpath searches with XML::Twig::XPath elements.  Can someone help
me find out what I'm doing wrong?  It seems that if I use my own
subroutine to do the xpath search I get an error from the Twig code, but
if I code the same statements inline the results are what I expect.

============ Test program ================

use strict;
use warnings;
use XML::Twig::XPath;
# ----------------- problem section of code -------------------
# This way fails
#my $da_xml = new XML::Twig::XPath;
#$da_xml = get_xml();

# This way works
my $da_doc = new XML::Twig::XPath;
my $da_xml = $da_doc->root;
# -------------------------------------------------------------

my @f = $da_xml->findnodes("//bkfile[b_filename='testfile']");
print "\n";

sub get_xml {
         my $da_xml = new XML::Twig::XPath;
         return $da_xml->root;

============= test.xml ==============

My results, using the program as above:
[nfb@neptune lib]$ ./tryerr.pl

Switching the comments so the "failing" section is active I get:
[nfb@neptune lib]$ ./tryerr.pl
Can't use an undefined value as a HASH reference at
/usr/lib/perl5/site_perl/5.8.6/XML/Twig/XPath.pm line 114.

Environment is Fedora Core 4, Perl 5.8.6; XML::Twig::XPath is "v 1.15
2005/07/05 10:47:19"


Henry Law       <><     Manchester, England

Re: XML::Twig::XPath - strange problem

the author of XML::Twig reads perlmonks.org regularly. he also is
responsive to direct emails, so you might shoot him one. He's a good
guy (mirod on perlmonks, MIchael Rodriguez in real life.)

Re: XML::Twig::XPath - strange problem

Henry Law wrote:
Quoted text here. Click to load it


It looks like when returning from the sub, the twig field for the root
element is lost (it becomes undef), which causes problems later on (the
XML::XPath object is attached to the twig itself).

I'll look some more (you can file a bug with RT if you want)



Re: XML::Twig::XPath - strange problem

Henry Law wrote:
Quoted text here. Click to load it

It is a problem of scope: the XML::Twig::XPath object goes out of scope
at the end of the sub, so you can't use it any more.

You have to have get_xml return the XML::Twig::XPath object.

and then write:

my $da_doc= get_xml();      # so the document object hangs around
my $da_xml= $da_doc->root;

I can't decide whether this is a bug or a feature though (read: if it is
a bug, it's a hard one to fix ;--). The document (XML::Twig::XPath
object) is destroyed because the link from the root node
(XML::Twig::XPath::Elt) to the document is weak, to avoid memory leaks.
If I don't weaken that link, then memory leaks happen.

A better error message might be all I can do.

Does this help?


Re: XML::Twig::XPath - strange problem

Michel Rodriguez wrote:

Quoted text here. Click to load it

Perfectly, and the way you've written it makes sense too, now that I
know.  The workround isn't difficult.  If I were you I'd claim this as a
"feature" and document it.

Thanks for your prompt and informative reply.


Henry Law       <><     Manchester, England

Site Timeline