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

Wise Monks,

My script:
#!/cadappl/bin/perl -IDoc -Ilib use Doc::DBI; #inherits from Class::DBI
with module
package Doc::DBI; use base 'Class::DBI'; Doc::DBI->connection('DBI:mysql:database=abcdefg;host=host.tld', 'user +', 'passwd'); 1;
Works fine on the command line and as cgi. Now i am trying to make it work under mod_perl. Trying use lib on the command-line fails, with this error:
Can't locate object method "connection" via package "Doc::DBI" (perhap +s you forgot to load "Doc::DBI"?) at /home/hive/25167/perl/Doc/DBI.pm + line 5. Compilation failed in require at /home/hive/25167/cgi-bin/productie/li +b/Ima/DBI.pm line 7. ...
I assume use lib loads the module at another time than -I but why won't it work?

Replies are listed 'Best First'.
Re: 'use lib' versus '-I'
by hardburn (Abbot) on Nov 30, 2004 at 15:36 UTC

    Under mod_perl, the shebang line is mostly irrelevent. It needs to be there for CGIs to give the path to the interpreter, but in mod_perl, the interpreter is built into the web server itself. IIRC, it'll parse some command line options (like -T), but not all.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

      Yes i know. So i'm running it on the command line and it does not work using "use lib".

        It works for me:

        $ ls Test.pm $ cat Test.pm package Test; sub foo { @_ } 1; $ perl -le 'use lib "."; use Test; print Test::foo( "Hello, world!" )' Hello, world! $

        What did you actually put on the command line?

        "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: 'use lib' versus '-I'
by ikegami (Patriarch) on Nov 30, 2004 at 18:16 UTC
    #!/cadappl/bin/perl -IDoc -Ilib use Doc::DBI;

    and

    #!/cadappl/bin/perl use lib 'Doc'; use Doc::DBI;

    should both load Doc/Doc/DBI.pm. (tested)

    If your file is Doc/DBI.pm, you need -I. or use lib '.'.

Re: 'use lib' versus '-I'
by htoug (Deacon) on Nov 30, 2004 at 18:05 UTC
    I assume use lib loads the module at another time than -I

    Testing will show that -I and use lib does exactly the same:

    $ perl -IDoc -Ilib -e 'print join(":", @INC), "\n";' Doc:lib:/usr/lib/perl5/5.8.3/i586-linux-thread-multi:... $ perl -e 'use lib qw(Doc lib); print join(":", @INC), "\n";' Doc:lib:/usr/lib/perl5/5.8.3/i586-linux-thread-multi:...
    So your problems must stem from something else - probably that the current directory is not what you expect when running under mod_perl.

    Try using absolute paths in your use libs.

Re: 'use lib' versus '-I'
by Steve_p (Priest) on Nov 30, 2004 at 21:34 UTC

    Just to get things clear, neither use lib nor -I load modules. Only use and require do that.

    Both use lib and -I control @INC. According to perlrun, -I prepends directories to @INC. use lib also adds directories to the beginning of @INC, but also checks for the existance of an $archname/auto subdirectory of each directory passed to use lib. If an $archname/auto subdirectory exists, $archname/auto and $archname subdirectories are also added to @INC before the directory passed to use lib.

    mod_perl is a different animal when running scripts. It has been pointed out above the mod_perl does not look at the shebang line. That is correct. You'll need to either use lib in your handler module or in your startup script to use modules installed outside of @INC.