Hoeze
Hoeze

Reputation: 716

Perl: How to avoid adding current directory to module include path?

I'd like to override some perl modules from a perl tool installed via conda. Perl is v5.26.2 installed via conda-forge. However, I cannot overwrite any scripts which are located in the same directory as $SCRIPT because perl always prepends the location of the script to the module directories:

/opt/anaconda/envs/vep-99/share/ensembl-vep-99.2-0/modules
/opt/anaconda/envs/vep-99/share/ensembl-vep-99.2-0
/home/user/.local/lib/perl5/lib/perl5/x86_64-linux-thread-multi
/home/user/.local/lib/perl5/lib/perl5
/opt/anaconda/envs/vep-99/lib/site_perl/5.26.2/x86_64-linux-thread-multi
/opt/anaconda/envs/vep-99/lib/site_perl/5.26.2
/opt/anaconda/envs/vep-99/lib/5.26.2/x86_64-linux-thread-multi
/opt/anaconda/envs/vep-99/lib/5.26.2
.

I now did a very ugly hack which simply changes the working directory for perl:

#!/bin/bash                                                                                                                                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                                                                                                                             
SCRIPT="${SCRIPT:-vep}"
SCRIPT_DIR="$(dirname $(realpath $(which $SCRIPT)))"
 
export PERL5LIB="$PERL5LIB:$SCRIPT_DIR:$SCRIPT_DIR/modules"

cd /
 
# fake script location to be in the current working directory
cat $(which $SCRIPT) | perl $@

This results in the following @INC:

//modules
/
/home/user/.local/lib/perl5/lib/perl5/x86_64-linux-thread-multi
/home/user/.local/lib/perl5/lib/perl5
/opt/anaconda/envs/vep-99/share/ensembl-vep-99.2-0
/opt/anaconda/envs/vep-99/share/ensembl-vep-99.2-0/modules
/opt/anaconda/envs/vep-99/lib/site_perl/5.26.2/x86_64-linux-thread-multi
/opt/anaconda/envs/vep-99/lib/site_perl/5.26.2
/opt/anaconda/envs/vep-99/lib/5.26.2/x86_64-linux-thread-multi
/opt/anaconda/envs/vep-99/lib/5.26.2
.

This technically works, but there is still /, //modules and . in the include path. Is there a way to get rid of that?

Upvotes: 1

Views: 250

Answers (1)

ikegami
ikegami

Reputation: 385496

It's the following that's adding the first two directories:

export PERL5LIB="$PERL5LIB:$SCRIPT_DIR:$SCRIPT_DIR/modules"

Instead of adding cd /, just remove or comment out that line.


As for ., it's hardcoded up Perl 5.26 (released in 2017) and absent from then on. Removing it would require running something like the following:

# Remove relative paths from `@INC` (on unix systems).
BEGIN { @INC = grep m{^/}, @INC; }

But, since . is last in @INC, it doesn't hinder your attempts to override modules.

Upvotes: 1

Related Questions