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 | [reply] |
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 | [reply] [d/l] |
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 ) | [reply] |
|
|
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
| [reply] |
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 ) | [reply] |
|
|
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;
| [reply] |
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. | [reply] |
|
|
Thanks. This works.
But each user needs to set PERLLIB variables.
Also, in CGI script, you still need to set @INC or PERLLIB.
Qiang
| [reply] |
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 | [reply] |
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) | [reply] |
Re: how to include a directory in @INC?
by mrgarygnu (Initiate) on Mar 03, 2000 at 21:23 UTC
|
I agree that it's easier to create the symlink. Botom line:
If the modules / libraries are being used by one user, the
environment solution seems best to me.
If the modules / libraries are to be used by everyone on the
system, it would be good to create a symlink from your
site_perl directory.
If the modules / libraries are to be used by everyone, and
you're worrying about security, it would be good to move your
modules / libraries to the site_perl directory. | [reply] |