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

I've got a quick architecture question related to mod_perl. I have a set of instructions that I want to be executed on every page of the site. So basically I had a collection of .pl files that represented pages, and the first line of each page was a 'do startupcode.pl'. While this works it might as well not be running on mod_perl for all the efficiency it gives away. I changed my startupcode.pl into a package and updated the httpd.conf file to preload it. My problem is that the 'pages' on the site just do some logic to generate html code and then I pass it off to a subroutine in the sharedcode module to output it on the screen in a standard layout. My issue is I don't know how to get the $r handle of my active apache session to be valid when I call a subroutine in my package. Basically if I put $r = shift; $r->print("Hello World") into my page.pl file it works file. But if I put that some code into a 'sub printContent {}' routine in my package and call printContent("hello world") from my page.pl file I get no output in the browser. I assume my problem is $r = shift; inside the package, but I don't know how to properly define $r in a package so that it refers to my active apache session.

Replies are listed 'Best First'.
Re: mod_perl v2
by perrin (Chancellor) on Apr 15, 2008 at 01:54 UTC
    Either pass $r to each subroutine when you call it or have each sub get $r like this:
    my $r = Apache2::RequestUtil->request;
      I was trying to avoid passing it into the subroutine as I want as few lines of duplicate code in each of the individual pages. I tried your alternate suggestion and I keep getting the error 'Can't locate object method "request" via package Apache2::RequestUtil"'. I had been loading that module with the httpd.conf and I tried adding it to the top of my package as well.
        Loading Apache2::RequestUtil doesn't give you any errors?
Re: mod_perl v2
by pc88mxer (Vicar) on Apr 14, 2008 at 23:20 UTC
    If printing is your only problem, you can dispense with referring to $r:
    sub printContent { print $_[0]; # works in CGI and mod_perl environments }