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

This probably has a striaghtforward answer but i couldnt find one on the net or in a book.

I am requiring several .pl scripts from a .cgi script with success (eg require "datasource.pl";). everything works fine.

I have however seen many perl script which use the following code before requiring anything,

use lib qw(     /usr/local/etc/httpd/cgi-bin);

My question is
1)Do you HAVE to use 'use lib' if you are requireing other scripts.
2)In what situation would i use 'use lib'.

THanks

Costas

Replies are listed 'Best First'.
Re: Use lib() and require
by merlyn (Sage) on Apr 06, 2001 at 17:36 UTC
    require is fine if your sub-program is located somewhere in @INC. use lib extends @INC.

    Remember that @INC includes "dot" (the current directory) except in taint mode. But also recall that this means the current directory of the running process, not necessarily the directory of the original script. So, if your subprograms are contained within the directory of the running process, you'll not need to extend @INC.

    -- Randal L. Schwartz, Perl hacker

      Just to amplify this, the current working directory of the script is determined by how the web server runs the script. It sounds like your webserver (in its current configuration) sets the current working directory to be the same as the location of your script. It also sounds like you don't have taint checking turned on.

      I suggest you add use lib so that your script will continue to work if something changes in the web server configuration that affects what current working directory your script receives when it runs.

      I also suggest that you make a test version of your script and turn on taint checking for it and then fix any problems that are reported.

              - tye (but my friends call me "Tye")
Re: Use lib() and require
by Caillte (Friar) on Apr 06, 2001 at 17:44 UTC

    You can use lib to add paths to the @INC variable. The main use ofr it would be if you wanted to include a file from a non standard directory. (non-standard being not the normal perl lib directories or the directory you are running the script from.)

    Another way to achieve this effect is to push the new directory directly onto the @INC array.

    $japh->{'Caillte'} = $me;

Re: Use lib() and require
by Anonymous Monk on Apr 06, 2001 at 18:06 UTC
    Hi,
    The perldoc says about lib:-

    "This is a small simple module which simplifies the manipulation of @INC at compile time.
    It is typically used to add extra directories to perl's search path so that later C<use> or C<require> statements will find modules which are not located on perl's default search path."

    The @INC variable is set to look in the various places AND the current directory:
    mine is set for these paths:-
    /usr/libdata/perl/5.00503/mach ,br> /usr/libdata/perl/5.00503
    /usr/local/lib/perl5/site_perl/5.005/i386freebsd
    /usr/local/lib/perl5/site_perl/5.005
    and .

    so if I want to 'use'/'require' .pl progs in say /home/perl then I can specify that in the code as

    use lib /home/perl; # note lib will take a list

    then I can 'require' or 'use' anything in that directory.

    Hope this helps some.

    Cheers
    Steve

      1)DO I need to use 'use lib' if i am requesting script in the CURRENT directory. In other words does @INC always include the path "."?
        If anyone happens to stumble on this, and still needs help...
        Unless @INC shows that it includes '.', don't assume that it does.
        perl -e 'print @INC;'

        --
        paul

Re: Use lib() and require
by suaveant (Parson) on Apr 06, 2001 at 17:37 UTC
    use lib add that path to the front of your @INC, much like perl -I/usr/local/etc/httpd/cgi-bin
    That's all, just another way to do it.
                    - Ant