Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Can't locate object method in my Moo based class

by Lotus1 (Vicar)
on Jan 04, 2016 at 22:05 UTC ( [id://1151892]=perlquestion: print w/replies, xml ) Need Help??

Lotus1 has asked for the wisdom of the Perl Monks concerning the following question:

This is my first non-trivial attempt at Perl object oriented programming. The code below is just a first attempt to get something simple working but I'm stumped.

Goal: Add my own methods in my XML::LibXML derived objects.

Problem: The method I added in SSEDocument can't be located when I run my test script. I want my method to be similar to findnodes. findnodes and findvalue are defined in XML::LibXML::Node. The documentation for XML::LibXML::Document states that it inherits all the methods from XML::LibXML::Node. If I make my own version of XML::LibXML::Node then the methods there won't be seen since XML::LibXML::Document will still inherit from XML::LibXML::Node. I don't understand why my method won't work when defined in SSEDocument. Can someone suggest a better approach?

package SSEParser; use Moo; use Data::Dumper; extends 'XML::LibXML'; sub load_sse_file { my ($self, $filename) = @_; die "File not found <$filename>\n" unless -f $filename; die "Not SSE file <$filename>\n" unless $filename =~ /_subeditor\. +xml$/i; my $doc = $self->parse_file($filename); print '='x75, "\n"; print Dumper($doc); print '='x75, "\n"; bless $doc, 'SSEDocument'; return $doc; } 1;
package SSEDocument; use Moo; extends 'XML::LibXML::Document'; sub find_buses { my ($self) = @_; return $self->findnodes("//DNOM/Substation/Bus"); } 1;

testmyclass.pl

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; use SSEParser; my $inst = SSEParser->new(); print Dumper($inst); my $sse_doc = $inst->load_sse_file("test_subeditor.xml"); print Dumper($sse_doc); my @buses = $sse_doc->find_buses();

When I run this here are the results:

$VAR1 = bless( { '_State_' => 0, 'XML_LIBXML_PARSER_OPTIONS' => 4102 }, 'SSEParser' ); ====================================================================== +===== $VAR1 = bless( do{\(my $o = 68387856)}, 'XML::LibXML::Document' ); ====================================================================== +===== $VAR1 = bless( do{\(my $o = 68387856)}, 'SSEDocument' ); Can't locate object method "find_buses" via package "SSEDocument" at C +:\usr\scripts\SSE\sseparser\pm\testmyclass.pl line 12.

Replies are listed 'Best First'.
Re: Can't locate object method in my Moo based class
by Anonymous Monk on Jan 04, 2016 at 22:57 UTC
    Did you forget  use SSEDocument ; ?

      Doh! I didn't exactly forget it, I assumed I didn't need it since when I use XML::LibXML I don't have to worry about 'use'ing all the sub classes. But XML::LibXML must be taking care of all the importing and namespace stuff in the background. Thanks, it works now.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1151892]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-19 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found