Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Inheritance when subclass passed as a parameter

by choroba (Archbishop)
on Feb 05, 2020 at 10:31 UTC ( #11112420=note: print w/replies, xml ) Need Help??


in reply to [Solved] Inheritance when subclass passed as a parameter

The template is populated in the constructor of a plugin, but you never call the constructor. Also, you probably don't want to create a hash with just the template in the constructor, you want to call SUPER::new and add template to it...

Update: BTW, /[data]/ matches a character class, not the literal string [data].

Update 2: The real problem is you're using inheritance, but Service is not a parent, it's a factory.

Update 3: Like this:

---./t/00.t---

#!/usr/bin/perl use warnings; use strict; use Test::More; use Service::Factory; my $xml = Service::Factory->instantiate('xml', 'xmldata'); $xml->populate; like $xml->{template}, qr{<data>xmldata</data>}, 'XML OK'; my $json = Service::Factory->instantiate('json', 'jsondata'); $json->populate; like $json->{template}, qr/{"data":"jsondata"}/, 'JSON OK'; done_testing;
---./lib/Service/XML.pm---
package Service::XML; use warnings; use strict; use parent 'Service'; use XML::LibXML; sub new { my $class = shift; my $self = $class->SUPER::new(@_); my $xml = 'XML::LibXML::Document'->new; $xml->setDocumentElement($xml->createElement('data')); $xml->documentElement->appendTextNode('[data]'); $self->{template} = $xml->toString; return $self } __PACKAGE__
---./lib/Service/Factory.pm---
package Service::Factory; use Service::XML; use Service::JSON; use warnings; use strict; my %SUPPORTED = ( xml => 'XML', json => 'JSON'); sub instantiate { my ($class, $plugin) = @_; die "Unknown plugin '$plugin'.\n" unless $SUPPORTED{$plugin}; my $subclass = "Service::$SUPPORTED{$plugin}"; return $subclass->new(@_[2 .. $#_]) } __PACKAGE__
---./lib/Service/JSON.pm---
package Service::JSON; use warnings; use strict; use parent 'Service'; use Cpanel::JSON::XS qw{ encode_json }; sub new { my $class = shift; my $self = $class->SUPER::new(@_); $self->{template} = encode_json({data => '[data]'}); return $self } __PACKAGE__
---./lib/Service.pm---
package Service; use warnings; use strict; sub new { my ($class, $data) = @_; return bless { data => $data, service => $class, }, $class } sub populate { my $self = shift; $self->{template} =~ s/\[data\]/$self->{data}/i; } __PACKAGE__

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11112420]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2020-06-06 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (41 votes). Check out past polls.

    Notices?