Say your module set is Dpommert::Module, and I'm writing Kyle::Logger. I'd probably set it up this way.

package Kyle::Logger; use Dpommert::Module; Dpommert::Module->set_default_logger( __PACKAGE__ ); package Dpommert::Module; my $default_logger_class = 'Dpommert::Logger'; sub set_default_logger { my $class = shift; die 'class method' if ref $class; $default_logger_class = shift; } sub get_logger { my $self = shift; return $self->{logger} ||= $default_logger_class->new(); } sub set_logger { my $self = shift; $self->{logger} = shift; }

In set_logger and set_default_logger, you may want to use UNIVERSAL::can to make sure what you get has the interface you need.

The way the above works is that a logger class, when it's used, registers itself with the module set. On top of that, any instance of the module can have its logger changed from the default. In this scenario, it's important that Dpommert::Module is used first, contrary to the usage you laid out.


In reply to Re: Polymorphism through use by kyle
in thread Polymorphism through use by dpommert

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.