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

Please help ! urgent need to know:
what is the best practice on using :
use lib <path_to_libs>;
?
I have a live and beta website and wish to use the same code on both sites with as little changing as possible - I do not want to change the paths continually to point to the relevant website library (live or beta).
Therefore how can I achieve this ?
currently I am using:
use FindBin qw/$RealDir/; BEGIN {$RealDir .="libs";} use lib "$RealDir";

which I am not satisfied or happy with.
The live and beta websites reside on the same server - hence I cannot add the lib path into the @inc permanently (?or can I add both live and beta? I doubt.)

many thanks, oh wise ones,
johnc

Replies are listed 'Best First'.
Re: problem with paths to library files
by ferreira (Chaplain) on Jan 17, 2007 at 09:43 UTC

    Maybe you don't need use lib at all, if you are able to set environment variables visible to your live and beta websites (possibly customized for each of these). If that's the case, you can add some paths to @INC automatically by setting the PERL5LIB variable.

    Read more about it in the section "ENVIRONMENT" on perlrun.

Re: problem with paths to library files
by themage (Friar) on Jan 17, 2007 at 09:52 UTC
    hi j_c,

    In your case you have to options (that I know), depending on your environment .

    If your script is cgi your can use:
    BEGIN { my $path=$0; $path=~s{[^\/]+$}{libs/}; } use lib $path;
    On the other hand, if your site is using ModPerl, you can configure your libpath in the httpd.conf (or similar) using:
    <virtualhost *:80> # ... PerlSetVar libpath "/path/to/your/libs/"; </virtualhost>
    And then read that config with:
    BEGIN { my $path=$r->dir_config("libpath"); } use lib $path;
    Hope this help.

      BEGIN { my $path=$0; $path=~s{[^\/]+$}{libs/}; } use lib $path;

      The "minor" problem with this is that the lexical $path will already be out of scope in the use lib $path; statement (so the (empty) global $path is being used instead...)

      Moving the my $path; before the BEGIN block does work, though.

      (Honestly, I'm somewhat surprised it does (you might argue the BEGIN block is being executed before the lexical is brought into existence - but apparently this is not the case...). Would any of the more knowledgable Monks care to comment on whether this is a feature you can rely on?)

        I tried moving the
        my $path
        above the begin but it didn't work...
        however, we have succeeded with the following:
        use vars qw/$RealDir/; BEGIN{ $0 =~ m/(.*\/)/;#location of this file $RealDir = $1 . "libs"; }; use lib "$RealDir";

        which works a treat.
        thanks guys, excellent advice :)
        all the best
        johnc
Re: problem with paths to library files
by eff_i_g (Curate) on Jan 17, 2007 at 14:59 UTC
    Why not something like the following (i.e., you don't need the BEGIN block)?
    use FindBin; use lib "$FindBin::Bin/libs";