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.

    --MidLifeXis

    A reply falls below the community's threshold of quality. You may see it by logging in.
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 );