First off, I wouldn't use the leading underscore. In Perl convention, that means "don't call (or override) me". If that's what you really want, that's fine. But I don't think it is.

Second, why are you only using the root of the object's namespace? That is, if I have both Foo::Bar::Blah and Foo::Baz::Flubbar, you're expecting them both to share the same Foo::Debug? That seems a bit of a strange convention.

Finally, you're evaling way too many strings there ;-)

sub action { if ( DEBUG ) { my $loggerClass = $self->_getLoggerClass(); my $loggerHandle = $loggerClass->getLoggerHandle(); } }
Two major differences here. First, this doesn't use eval STR. Any time you can avoid eval STR is a good thing. Not just because of possible nasty code-injection attacks (which probably aren't a major concern here), but also because we can avoid the overhead of another parse/compile session.

Second major difference is that we'll be passing in the $loggerClass as the first parameter to getLoggerHandle. In this case, it doesn't look like a bad thing since you aren't passing anything in anyway, so most likely getLoggerHandle will just ignore its parameter, no harm done to your API. A side effect here is that if _getLoggerClass returns a full-fledged object, that will get passed in instead, though that likely won't change anything. Another side effect is that if the logger class does not define getLoggerHandle, but one of its parent classes (if any) do, this will get properly dispatched up the @ISA hierarchy. This is handy if, for example, your logger class has a generic form, and the one we're using just overrides a few things.

On to your question. (That & is always unnecessary - unless you know what you're doing, but that's another SoPW.) Try this out:

my $loggerClass = $self->_getLoggerClass(); $loggerClass->writeToLoggerHandle($self, $text);
As I said earlier, this will pass in $loggerClass as the first parameter. Unlike the getLoggerHandle function which didn't take any parameters, this one does. So you'll need to take that into account inside the function, by shifting off that first parameter, and likely as not discarding it. There is another way, however. But it's rarely used, and will probably confuse more than a few people, so I don't suggest it - I merely bring it up for completeness.
$loggerClass->can('writeToLoggerHandle')->($self, $text);
Here we ask the perl interpreter whether the class can do this method. It will check the @ISA hierarchy for us, and return a CODE reference if it succeeds. We then dereference that code reference with the next arrow, and pass in its parameters.

But don't do that. It's confusing. ;-)


In reply to Re^2: Getting parent class data by Tanktalus
in thread Getting parent class data by nmerriweather

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.