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

Argh. This seems like a stupid question but I've consulted all my perl scriptures and can't figure it out. I want to set a variable to control the library search path. The variable will ultimately come from a config file but this simple test case illustrates the problem.

$BASEDIR="/export/home/nrc"; use lib "$BASEDIR/mylib";
This doesn't work. The value "/mylib" is added to @INC. If I use -w it warns about concatenating an uninitalized value in the "use" line.

It seems like this is a scoping problem but what scope is the use lib pragma looking for and how do I assign it?

Replies are listed 'Best First'.
Re: "use lib" with variable
by japhy (Canon) on Nov 03, 2001 at 00:40 UTC
    The problem is that use lib happens at compile, and the assignment to $BASEDIR doesn't happen until run-time. You could put $BASEDIR in a BEGIN block though:
    BEGIN { $BASEDIR = "/export/home/nrc" } use lib "$BASEDIR/mylib";

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Wow, thanks for the quick response. I am humbled by our wisdom. Works like a charm!