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

Hi everyone,
Is there any way to push a variable to @INC array in perl. I've noted if the full path is given then it accepts but a variable is given then it ignores,
Ex:
push @INC, "/home/user_x/custom_libs"; # working my $LIB_PATH = "/home/user_x/custom_libs"; push @INC, $LIB_PATH; # not working
Thanks,

Replies are listed 'Best First'.
Re: Push local lib paths to @INC array
by chromatic (Archbishop) on Dec 25, 2008 at 17:32 UTC

    How do you know it's "not working"? Print the contents of @INC before and after the push, and you'll see that it is (or you have a broken Perl installation).

    Note, however, that if you want to modify @INC before loading a module with use, you need to put the modifications in a BEGIN block -- and that includes setting the value of the variable.

      Ya, Thanks,
      It is working fine inside a BEGIN block (both variable assignment and push statement)
Re: Push local lib paths to @INC array
by jwkrahn (Abbot) on Dec 25, 2008 at 17:32 UTC

    Use the lib pragma to add paths to @INC.

Re: Push local lib paths to @INC array
by vlsimpson (Beadle) on Dec 25, 2008 at 18:04 UTC

    I use 'use lib' (sorry) in combination with 'FindBin' with a Perl/Tk app I'm working on.,

    use FindBin; use lib $FindBin::Bin . '/lib';

    FindBin documentation

Re: Push local lib paths to @INC array
by gube (Parson) on Dec 26, 2008 at 21:22 UTC
    If your application has many modules and many scripts lets say 100, instead adding these lines in each and every file and model class, you can export simply in command prompt while running the script. In linux terminal export PERL5LIB=/home/gubendran/dashboard/trunk/lib/ This will work till you close the terminal. This avoid adding code in each and every script and you can change the library path anytime.