in reply to Re: Transparently inheriting functions from a parent package
in thread Transparently inheriting functions from a parent package

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

Replies are listed 'Best First'.
Re^3: Transparently inheriting functions from a parent package
by wind (Priest) on Apr 05, 2011 at 00:37 UTC
    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();
      Yeah the class methods don't jive with mod_perl at that level... but Exporter was spot on. I guess all this time I had been using it to import functions I never considered that it truly places them in the 'use'rs namespace. That's awesome and exactly what I needed. For anyone else that stumbles on this post in the future I ended up with something like:
      package Basehandler; BEGIN { use Exporter; our @ISA = qw (Exporter); our @EXPORT = qw (handler .... setup_environment ); } 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 {}