Click here to get back home

XML::Twig::XPath - strange problem

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
XML::Twig::XPath - strange problem Henry Law 11-10-2005
Posted by Henry Law on November 10, 2005, 9:44 am
Please log in for more thread options


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 ==============
<bkdirectory>
<bkfile>
<dbid>445</dbid>
<b_path>/nfb</b_path>
<b_filename>testfile</b_filename>
</bkfile>
</bkdirectory>
=====================================

My results, using the program as above:
[nfb@neptune lib]$ ./tryerr.pl
<bkfile><dbid>445</dbid><b_path>/nfb</b_path><b_filename>testfile</b_filename></bkfile>

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


Posted by metaperl@gmail.com on November 11, 2005, 1:19 am
Please log in for more thread options


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



Posted by Michel Rodriguez on November 11, 2005, 4:12 pm
Please log in for more thread options


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


Posted by Michel Rodriguez on November 11, 2005, 4:29 pm
Please log in for more thread options


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


Similar ThreadsPosted
Help with XML::Twig xpath syntax, please October 16, 2005, 6:56 pm
Problem Parsing Huge XML file using XML::Twig April 23, 2007, 10:24 pm
Strange errors August 9, 2004, 7:17 pm
strange behavior in File::Basename July 22, 2004, 5:32 pm
Net::SFTP / Net::SSH::W32Perl strange debug messages? August 13, 2006, 11:06 am
XPath to Line Number? June 26, 2007, 2:39 pm
XML::XPath delete function November 26, 2007, 3:35 pm
Parsing Doctype Entitites using XML::XPath November 5, 2004, 10:13 am
XML Twig help April 8, 2005, 2:18 pm
Using XML::Twig December 19, 2005, 4:59 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap