in reply to use lib $lib_dir not working

Pull up your code from run time to compile time by using a BEGIN block:

BEGIN { ... } use lib ...;

Replies are listed 'Best First'.
Re^2: use lib $lib_dir not working
by karelb (Novice) on Apr 27, 2011 at 09:57 UTC
    Thanks for the suggestion and I already tried something like that. But it reports a problem with the splice line: "Modification of non-creatable array value attempted, subscript -2". I guess this is because $0 is not defined inside the BEGIN block.
      No; guess again.

      The problem is with @dirs. Did you assign it in the BEGIN block? If you declare it with "my", make sure you do that in outside the BEGIN block. the actual use line should also be outside (after) that block. And is $0 actually the full path? I think perhaps your @dirs array is just too short for the splice. You might have to use rel2abs to make it so.

      Anyway, I would do it another way, and put the code in a do block. Untested:

      use File::Spec::Functions qw(rel2abs) use lib do { my @dirs = split('/', rel2abs($0)); splice(@dirs,-2); my $install_dir = join('/',@dirs); $install_dir."/lib" };