Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

can't call method on an undefined error in perl script

by veerubiji (Sexton)
on Nov 21, 2011 at 12:32 UTC ( #939176=perlquestion: print w/replies, xml ) Need Help??
veerubiji has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am getting error while running perl script "can't call method "findnodes" on an undefined value at c:\work\pm.line 17".My script as shown below.

#!/usr/bin/perl use warnings; use strict; use XML::LibXML; my @xml_files = File::Find::Rule->file->maxdepth(1)->name('*dtc.xml')- +>in( "c:/file/save"); my $bigXML = XML::LibXML::Document->new( '1.0', 'UTF-8'); my $aggregated; for my $xml_file ( @xml_files ) { my $doc = XML::LibXML->new->parse_file( $xml_file ); my ( $specNode ) = $doc->findnodes( '//Specification' ); if ( $aggregated ) { $aggregated = $specNode; } else { my @details = $specNode->findnodes( './Definition' ); $aggregated->addChild( $_ ) foreach @details; } } $bigXML->adoptNode( $aggregated ); $bigXML->toFile( 'aggregated_data.xml' );

I Modified above code like that and this time it gives diffrent error like "XML::LibXML::Document::adoptNode()--node is not a blessed SV reference at c:\work\pm.line 17 " help me with this error I didn't see this type of error befor.

Replies are listed 'Best First'.
Re: can't call method on an undefined error in perl script
by MidLifeXis (Monsignor) on Nov 21, 2011 at 13:26 UTC

    $specNode is undefined - find out why. The call 6 lines above (my ( $specNode ) = $doc->findnodes( './Specification' );) is where it is assigned.


      Hi I you mean sepcnode is not assigned, I can't overcome this error still please help what I have to do in my script.

        Who knows? We don't have the data you are trying to process. My guess is that the xpath pattern being searched for is not found in the document you are trying to process.


        Who said anything about it being unassigned?!? It's undefined which means the findnodes() on line 11 returned an undef. The notoriously bad docs of XML::LibXML do not say anything about what's this method supposed to return if the XPath query doesn't match anything, but an undef is a quite way to return "nothing". Even though rereading the docs it seems it's supposed to return a XML::LibXML::NodeList object in scalar context no matter what.

        Enoch was right!
        Enjoy the last years of Rome.

Re: can't call method on an undefined error in perl script
by Anonymous Monk on Nov 21, 2011 at 12:48 UTC

    I have one more question Glob function giving only .xml files in current working directory, if I want to get from some other directory folder what can I do.

    Write the appropriate glob pattern, see glob / File::Glob

    File::Find::Rule is probably easier to use than learning about glob patterns

    use File::Find::Rule; my @files = File::Find::Rule->file->maxdepth(1)->name('*.xml')->in( @d +irs );

    Or better yet, Path::Class::Rule, its Path::Classy

    use Path::Class::Rule; my @files = Path::Class::Rule->new->file->max_depth(1)->name('*.xml') +->all( @dirs );

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://939176]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2018-06-22 08:07 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (122 votes). Check out past polls.