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

Dear Monks,
OK, this is a simple question that most of you know how to do .. but something that I've never learnt to do and it's high time that I did.

How does one go about adding a library to @INC?

Every time I call "use CGI::<anything>" I get the following error:

"Can't locate CGI/Session.pm in @INC (@INC contains: /usr/home/ ...........) at myprogram.pl line 6."

I need instructions on how to add libraries to the path.

Thank you!

Replies are listed 'Best First'.
Re: A simple library question
by gellyfish (Monsignor) on Jul 14, 2004 at 11:02 UTC

    Er, when you install a Perl module in the way that is appropriate for your platform (CPAN, PPM, RPMs or whatever) the files will be placed in the correct location.

    If you have installed (or even simply copied) the modules files into a non-standard location then you have to tell Perl where that is using the lib pragma:

    use lib qw(/my/module/directory ./lib);
    You can read about this in the lib manpage

    /J\

      Yeah, I think this monk needs to focus his attention on why something like CGI::Session isn't in an acceptable place to begin with.

      cranberry13: how did you install it? Did you use CPAN.pm, or did you use "make; make test; make install" or did you do something else?
      rjbs
        Sounds like CGI is either broken, not installed, or..

        if somehow your system has 2 versions of Perl installed, and you did install CGI, you may be invoking the other Perl version inadvertently...

        Adding a path to @INC can be done as described by the other monks but may not be the root of your problem

Re: A simple library question
by borisz (Canon) on Jul 14, 2004 at 10:58 UTC
    use  use lib 'path'; or
    set the env var PERL5LIB with the path(s) that like to add.
    Boris
Re: A simple library question
by pbeckingham (Parson) on Jul 14, 2004 at 11:58 UTC

    1. use lib 'path';
    2. push @INC, 'path'
    3. unshift @INC, 'path'
    4. #! /usr/bin/perl -Ipath
    5. $ENV{PERL5LIB}

Re: A simple library question
by trantor (Chaplain) on Jul 14, 2004 at 11:06 UTC
    Another way:
    #!/usr/bin/perl -w BEGIN { push @INC, 'path'; } use strict; use modulename;
    Update: thanks gellyfish for pointing out that this works as long as the included modules do not depend on XS components or autosplit subroutines. In that case, it would be necessary to push the architecture specific directories as well

      As the manpage says this is *almost* the same as use lib but the disadvantage is that it does not add any architecture specific directories under the path you have used, thus it is likely that any module with XS components or autosplit subroutines that have been installed there will not work unless you add the proper sub directories manually.

      /J\

Re: A simple library question
by justsimple (Initiate) on Jul 14, 2004 at 12:32 UTC
    You should write such line:
    
    use lib '/path_to_your_modules';
    
    I think this should work:)