In general, mixing OO and exporting is a bad idea. In this case, Log::Agent does only exporting and does no OO. So use base qw( Log::Agent ); is of no value as Log::Agent contains no methods (subroutines that expect an object as the first argument) and so you can never use inheritance with Log::Agent.

It sounds like you want to share all package variables with Log::Agent but replace or wrap some of its functions with your own versions.

You can share package variables between two (or more) packages by importing/exporting the variables so that both packages contain aliases to the same variables. Since Log::Agent doesn't list its package variables as being available for export (quite understandably since you aren't expected to modify them directly), you'd have to import "by hand", that is, do yourself what Exporter.pm usually does for you:

for my $var ( qw( Driver Prefix Trace Debug Confess Caller Priorities Tags DATUM ) ) { no strict 'refs'; *{$var}= \${"Log::Agent::$var"}; } *prio_cache= \%Log::Agent::prio_cache;
Then any time in your module code that you access or change, for example, $Driver, you are also accessing/changing $Log::Agent::Driver, because those are now both aliases to the same scalar variable.

However, changing $Log::Agent::Driver directly is not covered in the Log::Agent documentation so doing so violates the defined interface to the module and a future version of the module is free to change how $Log::Agent::Driver is used (or turn it into a lexical, etc.) such that your module would no longer work. This is the primary reason why this is a really bad idea.

I strongly encourage you instead to try to patch Log::Agent to extend it to also meet your needs and then get your patch incorporated into a new version of Log::Agent.

Alternately, in your code you could exclusively use defined interfaces, like logconfig().

The next step is to have your module export the routines from Log::Agent that you don't want to change, export your own routines, and give your own routines access to Log::Agent routines (probably both those that you haven't replaced and those that you have).

I've thought of many ways to do that but I think perhaps the easiest of them is something like:

# import _all_ that Log::Agent exports: use Log::Agent qw( /./ ); # Have us export the same stuff Log::Agent does: sub import { my( $selfPkg )= shift( @_ ); local( $Exporter::ExportLevel )= 1 + $Exporter::ExportLevel; Log::Agent->import( @_ ); } # For each routine we redefine, unimport the function # before we redefine it in order to avoid a warning: BEGIN { undef &logcarp; } sub logcarp { # preprocess stuff Log::Agent::logcarp( @_ ); # postprocess stuff }
But that is still abusing Log::Agent in ways that make your module sensitive to breaking for future versions of Log::Agent.

        - tye (but my friends call me "Tye")

In reply to (tye)Re: Inheritance and exported methods by tye
in thread Inheritance and exported methods by johanvdb

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.