I'm trying to refactor some code to eliminate duplication, and am having trouble figuring out how to pass a config variable up to the superclass. I assume this should be simple....

My duplicated method is a 'begin()' that I'm overriding in order to set some specific logging functionality. Right now, it looks like this:

package MyApp::Foo; use namespace::autoclean; use Moose; # ... BEGIN { extends MyApp::Utilities; } # override begin method (fr Catalyst::Action::Deserialize) to access l +og stuff sub begin : Private { my ($self, $c) = @_; my $logfile = $c->config->{ 'foo_logfile'}; # This is stored in e +xternal config file; name isn't predictable my $log = Log::Dispatch->new( outputs => [ [ 'File', min_level => 'debug', mode => '>>', newline => 1, f +ilename => $logfile ], ], ); $c->stash->{log_object} = $log; $c->forward('deserialize'); # ultimately goes to our callback } sub deserialize : ActionClass('Deserialize') {}

All I want to do is move this into 'MyApp::Utilities', which already contains some generic stuff. The only thing that differs for each subclass is the name of the logfile. But I can't figure out how to tell the superclass this name. If I try '__PACKAGE__->config( logfile => 'foo_logfile' );' that fails because the 'extends' is called before this is read. If I try to move the '__PACKAGE__->config' into the 'BEGIN' block, that fails because it doesn't know about 'config()' at this point. What's my solution?


In reply to Catalyst/Moose: passing specific config value to superclass by Anonymous Monk

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.