Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

how to include a directory in @INC?

by aqw001 (Initiate)
on Mar 02, 2000 at 19:33 UTC ( #4674=perlquestion: print w/replies, xml ) Need Help??

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

Hi, all I want to know how to put a directory in @INC permanently? I.e, in your program, you don't have to do something like 'unshift(@INC, $dir); '. I am using perl5 on Sun. Thanks in advance, Qiang

Replies are listed 'Best First'.
Re: how to include a directory in @INC?
by Anonymous Monk on Mar 02, 2000 at 23:07 UTC
    try: use libs qw# /path/to/your/library #; It effectively does the unshift trick, but it looks nicer Mik
Re: how to include a directory in @INC?
by chromatic (Archbishop) on Mar 02, 2000 at 23:29 UTC
    You can also use the -I flag on the command line or in the #! line at the start of your script: perl -w -I/home/chromatic/my_modules
Re: how to include a directory in @INC?
by mikfire (Deacon) on Mar 03, 2000 at 07:07 UTC
    Assuming you are on a UNIX boxen, and you have sufficient ( ie, root-like ) powers, there is on evil solution. I am uncertain of the security implications, so you may really want to think twice before doing this for your webserver.

    For every directory you wish to "permanently" add to @INC, create a symlink from the directory in your perl's site_perl ( eg, /usr/local/lib/perl5/site_perl ). Perl will cross the symlink when searching the paths.

    This suggestion is expected to be taken at your own risk. DO NOT blame me if somebody figured out how to exploit this from your webserver.

    Mik Mik Firestone ( perlus bigotus maximus )

      That works for only one other directory. Perl doesn't recurse to find an entry that is not directly an element of @INC. All you're doing is moving one of the elements of @INC to a specific different space.

      To be specific, suppose I symlink the/home/merlyn/private directory into my site_lib I can't just add MyModule.pm into that directory and call it with everyone's use MyModule. They'd have to say use private::MyModule. The path is affected. It's not recursive, it's a flat search based on the content of @INC followed by the filename derived from the module name.

      Now, you could effectively copy every entry that you want directly into the site_lib dir (like putting MyModule.pm directly there). But that's not changing @INC, nor making it more flexible. The only way to do this is to recompile Perl, and we're back to the same old solution.

      So, no, this is not "dirty", because it's not even a solution.

      -- Randal L. Schwartz, Perl hacker

Re: how to include a directory in @INC?
by mikfire (Deacon) on Mar 03, 2000 at 19:48 UTC
    Yes, but that makes things a little messy. Especially when trying to ( say ) upgrade your server. With the symlink method, all the local extensions stay in one nice little heap that can be easily moved.

    Speaking from personal experience ( although it could be I am just disorganized ), I have tried both ways and I really like the method I suggested. It is easier to maintain, it is obvious in code when you are accessing the local extentions ( eg, use LOCAL::Module ) and just strikes me as cleaner.

    Just my $0.02 worth

    Mik Firestone ( perlus bigotus maximus )

      If you are root, you can add an @inc path to the /etc/profile aswell, so the path will be set for every user after booting.
      The entry could look like this:
      PERL5LIB="${PERL5LIB}:/home/snadra/my_modules"
      export PERL5LIB

      The problem is, this does not work with CGI, it is only recommandable for people who don't use CGI.
      You need to set $PERL5LIB then inside of the httpd.conf aswell, I think, but I have not tried that yet.

      Edit:
      I am using a symbolic link now. I put that link into one of the @inc paths.
      This is working recursively under Linux, so I can call my modules like:
      use Linked_Dir::Dir::Module;
Re: how to include a directory in @INC?
by btrott (Parson) on Mar 02, 2000 at 23:59 UTC
    Try setting the PERLLIB or PERL5LIB environment variables to the path to your library. Of course, I don't know if this'll work correctly in programs such as CGI scripts; and it also doesn't work w/ tainting, apparently.
      Thanks. This works. But each user needs to set PERLLIB variables. Also, in CGI script, you still need to set @INC or PERLLIB. Qiang
Re: how to include a directory in @INC?
by Anonymous Monk on Mar 02, 2000 at 23:42 UTC
    Thanks for your guys. However, What I need is to build a directory into @INC permanently. I have lots of scripts and I don't want to modify each script to add a directory to @INC. Qiang
Re: how to include a directory in @INC?
by Anonymous Monk on Mar 03, 2000 at 19:41 UTC
    If you've got enough access to create the symlink mikfire mentions, you could also install your re-usable code into the /usr/local/lib/perl5/site_perl directory. Isn't that what it's for? In any case, that would probably eliminate the security implications of creating a symlink to a more "accessible" place. -Brian (mrgarygnu)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://4674]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2021-12-04 04:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (30 votes). Check out past polls.

    Notices?