vaewyn has asked for the wisdom of the Perl Monks concerning the following question:

Howdy all! I find myself writing tons of mod_perl handlers and they inevitably start with:
package Blah; use Module::I::Always::Use::1; ... use Module::I::Always::Use::35; sub handler { ... same 50 lines of code that do validation/cleansing and then a j +ump table to my real work functions ... } sub setup_environment {} sub work_function_1 {} ... sub work_function_100 {}

Is there any way to have a package that I can inherit all the boilerplate garbage? Copy pasting sucks and is horrible to maintain.

I envision something like:

-- Basehandler.pm -- package Basehandler; use Module::I::Always::Use::1; ... use Module::I::Always::Use::35; sub handler { ... same 50 lines of code that do validation and then a jump table +to my real work functions ... } sub setup_environment { .... } -- MyHandler/ForThisEndpoint.pm -- package MyHandler::ForThisEndpoint; use Basehandler; sub work_function_1 {} ... sub work_function_100 {} -- MyHandler/ForAnotherEndpoint.pm -- package MyHandler::ForAnotherEndpoint; use Basehandler; sub work_function_1 {} ... sub work_function_100 {}

Is there a way to do this? I have done the use parent Blah; thing with object oriented stuff and new() derived objects... this time though I don't really seem to have that option.

I have also thought of *handler = \&Basehandler::Handler(); in my MyHandler::ForThisEndpoint ... but that seems really dirty and once again I am boilerplating for like 5 functions I would like to stick in that same base package.

Thanks to anyone that can give me a pointer or a solution :)

Replies are listed 'Best First'.
Re: Transparently inheriting functions from a parent package
by wind (Priest) on Apr 04, 2011 at 23:41 UTC
    Can't think of any reason why use parent won't fill your needs:
    package MyHandler::ForAnotherEndpoint; use parent qw(Basehandler);

      When mod_perl calls a handler it should be akin to a perl program calling the function "handler" in the referenced package... so I made a simple test case and tried it with parent:

      -- t2.pm -- package t2; sub testing2 { print "Testing from t2.pm\n"; } 1; -- t.pm -- package t; use parent t2; sub testing { print "Testing from t.pm\n"; } 1; -- t.pl -- #!/usr/bin/perl use t; t::testing(); t::testing2();
      Running t.pl produces:
      Testing from t.pm Undefined subroutine &t::testing2 called at ./t.pl line 5.
      This is the first direction I tried cause it seemed correct but... alas I am missing something
        use parent effects class methods, so you gotta use the -> operator to access them.
        -- t.pm -- package t; use parent qw(t2); sub testing { print "Testing from t.pm\n"; } 1; -- t2.pm -- package t2; sub testing2 { print "Testing from t2.pm\n"; } 1; -- test.pl -- #!/usr/bin/perl use t; t->testing(); t->testing2();

        Exporter is what you want if you want static functions:

        -- t.pm -- package t; use t2; sub testing { print "Testing from t.pm\n"; } 1; -- t2.pm -- package t2; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(testing2); sub testing2 { print "Testing from t2.pm\n"; } 1; -- test.pl -- #!/usr/bin/perl use t; t::testing(); t::testing2();
Re: Transparently inheriting functions from a parent package
by Anonymous Monk on Apr 05, 2011 at 00:23 UTC
    Consider use base. Consider require.
Re: Transparently inheriting functions from a parent package
by stonecolddevin (Parson) on Apr 05, 2011 at 20:19 UTC

    use Moose and put all your shared functionality into neatly separated roles.

    Three thousand years of beautiful tradition, from Moses to Sandy Koufax, you're god damn right I'm living in the fucking past