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

I'm having a bit of trouble with my cgi script. I've written a module for it, and was hoping to be able to include it using use lib "." I do this on my win32 machine and it works beautifully, however, on my apache server, it gives me a 500 error. Any ideas? The code looks like this:
use lib "."; use myModule; $mm = myModule->new;
There's a lot more to my code, too much to paste here. I don't know of anyplace else the code could go wrong, as I said, it works great on my win32 machine.

Replies are listed 'Best First'.
Re: Problem with use lib?
by runrig (Abbot) on Dec 27, 2000 at 12:44 UTC
    You assume that the code is executing in the same directory as the script, which may not be true. Put the full path to the directory in the 'use lib' statement and see if that works.
Re: Problem with use lib?
by davorg (Chancellor) on Dec 27, 2000 at 15:04 UTC

    The current directory is, by default, the last item in the @INC array. The only difference that your use lib statement makes is to make it also the first item in @INC.

    It's already been suggested to you that the current directory might not be the one that contains the running script. To add the script's directory to @INC you should look at the FindBin module.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      See FindBin is broken (RE: How do I get the full path to the script executing?) for my thoughts on FindBin. It mostly works but it does so by spinning its wheels in the most perverse ways. The whole design is based around a false assumption. If you have Perl 5.6 or higher, I'd recommend the following:

      use File::Spec; use File::Basename qw(dirname); use lib dirname( File::Spec->rel2abs($0) );
      but that may be overkill since it can end up putting the full path to "." in the front of @INC, so you can probably get away nicely with:
      BEGIN { use File::Basename qw(dirname); use lib dirname( $0 ); }
      but be warned that doing a chdir() between this and a require could cause surprises.

              - tye (but my friends call me "Tye")
Re: Problem with use lib?
by blueAdept (Beadle) on Dec 27, 2000 at 20:41 UTC
    Use this instead to put your path in front of what is your default @INC, change 'unshift' to 'push' if you want it to be at the end. IMHO this is easy enough that I have never used 'use lib'. Perhaps 'use lib' provides some extra hints to the compiler that this method doesn't, but I have yet to encounted any problems.
    sub BEGIN { unshift @INC, qw(d:\\mydir\\mylibs); }
Re: Problem with use lib?
by Fastolfe (Vicar) on Dec 27, 2000 at 22:34 UTC
    Are you sure that this 'use lib' pragma is what is causing your 500 error? You may want to examine the server's error logs for details about 500 Server Errors. You may also be interested in CGI::Carp, which will send your script's fatal errors to the browser instead of someplace like the server logs, which can be cumbersome for some ill-configured web installations:
    use CGI::Carp 'fatalsToBrowser'; ...