XML::Twig::XPath - strange problem
XML::Twig::XPath - strange problem
am 10.11.2005 10:44:44 von Henry Law
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 ================
#!/usr/bin/perl
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;
$da_doc->safe_parsefile("test.xml");
my $da_xml = $da_doc->root;
# ------------------------------------------------------------ -
my @f = $da_xml->findnodes("//bkfile[b_filename='testfile']");
$f[0]->print;
print "\n";
sub get_xml {
my $da_xml = new XML::Twig::XPath;
$da_xml->safe_parsefile("test.xml");
return $da_xml->root;
}
============= test.xml ==============
445
/nfb
testfile
=====================================
My results, using the program as above:
[nfb@neptune lib]$ ./tryerr.pl
445/nfbtestfile
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
am 11.11.2005 10:19:00 von metaperl
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
am 11.11.2005 16:12:01 von Michel Rodriguez
Henry Law wrote:
> 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.
Weird,
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)
Thanks.
--
mirod
Re: XML::Twig::XPath - strange problem
am 11.11.2005 16:29:14 von Michel Rodriguez
Henry Law wrote:
> 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.
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?
--
mirod
Re: XML::Twig::XPath - strange problem
am 11.11.2005 23:21:05 von Henry Law
Michel Rodriguez wrote:
> 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?
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