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.
In reply to Can't locate object method in my Moo based class by Lotus1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |