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]

In reply to Re: Inheritance when subclass passed as a parameter by choroba
in thread [Solved] Inheritance when subclass passed as a parameter by davies

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.