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

Hi,

I'm trying to port a system to mod_perl. Basically, I've got a 'global.pl' script that I require() from my other scripts to load functionality such as cookies, DB connections, etc. This is an extract of my global.pl file:

#!/usr/bin/perl use DBI; use CGI; use Image::Size; sub env() { $query = $ENV{'QUERY_STRING'}; $cookie = $ENV{'HTTP_COOKIE'}; ($cookiename,$cookievalues) = split(/=/,$cookie); ($cookietype,$cookieuser) = split(/\&/,$cookievalues); $imgpath = "/home/e-smith/files/ibays/Primary/html/departments"; $imgpathweb = "/departments"; } sub form_read() { $q = new CGI; %fr = (); foreach ($q->param()) { $fr{$_} = $q->param($_); } } sub mysql_connect() { $dbh = DBI->connect("dbi:mysql:departments","user","passwd") or die "S +QL Connection Error."; }

So then, from my CGI scripts I can do the following:

#!/usr/bin/perl require '../global/global.pl'; &env(); &mysql_connect(); $sth = $dbh->prepare("select label,notes from departments where depart +ment='$query'"); $sth->execute; ($departmentlabel,$departmentnotes) = $sth->fetchrow_array; $sth->finish; $dbh->disconnect; &form_read(); print $fr{aformfield};

This all runs just fine as plain CGI, but starts crashing under Apache::Registry. Every a couple of reloads, I get an Internal Server Error, and in Apache's error log:

Undefined subroutine &Apache::ROOTdepartments::mod_2dperl::site::department_2epl::env ....

I tried searching the web for solutions, but the information I found is a little confusing.

Anyone knows what should be changed within the code above so that it runs correctly with Apache::Registry?

Any help is really appreciated.

Thanks,
Ralph

Replies are listed 'Best First'.
Re: Porting to mod_perl issues
by hardburn (Abbot) on Jun 14, 2004 at 13:34 UTC

    Coding under Apache::Registry all but requires you to use strict. The use of a lot of package globals will cause just the sort of problems you're experiancing. However, use strict won't magically make your code mod_perl-compliant, either. I suggest reading CGI to mod_perl Porting. mod_perl Coding guidelines.

    ----
    send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Re: Porting to mod_perl issues
by perrin (Chancellor) on Jun 14, 2004 at 13:50 UTC
Re: Porting to mod_perl issues
by Anonymous Monk on Jun 14, 2004 at 17:11 UTC

    Just added the following to my scripts before the require():

    use strict; use lib qw(.);

    And to global.pl:

    package Northlands::Global;

    And restarted Apache but I still get Internal Server Errors. Am I doing things right here?

      I just removed use strict; and left package Northlands::Global; on global.pl. By using &Northlands::Global::somesub(); I can now call my subroutines from another script. However, this isn't working with $dbh for the DB connection, as it returns "Can't call method "prepare" on an undefined value".

      - Ralph.

      Ok, getting better now. By calling $Northlands::Global::dbh->prepare() it's working now. However, when I do something such as &Northlands::Global::somesub("param","param");, global.pl isn't receiving the parameters when I read them with @_;. Any ideas?

      Thanks a lot :-)
      Ralph

        That should work fine. Are you sure that reading @_ is the problem? By the way, we don't use the "&" before subroutine calls anymore, except in special circumstances. See the perlsub man page.