Read the following story and see if the errors and troubleshooting that you’ve done match up. If so, the tip at the end might work for you.

The story

You want to run a Perl script, or a whole package which has Perl components. You have made sure that the files are executable and have run them, only to be confronted with an error that looks something like this:

Can't locate Random::ModuleName.pm in @INC (you may need to install the Random::ModuleName module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at ./WonderfulScriptName.pl line 3.
BEGIN failed--compilation aborted at ./WonderfulScriptName.pl line 3.

You realise that the module Random::ModuleName isn’t installed, and you follow the instructions from a guide such as this one to get them installed. The command and output on a Mac looks something like this:

FantasticUsername@MacBookPro:~$ cpanm Random::ModuleName
!
! Can't write to /Library/Perl/5.30 and /usr/local/bin: Installing modules to /Users/FantasticUsername/perl5
! To turn off this warning, you have to do one of the following:
!   - run me as a root or with --sudo option (to install to /Library/Perl/5.30 and /usr/local/bin)
!   - Configure local::lib in your existing shell to set PERL_MM_OPT etc.
!   - Install local::lib by running the following commands
!
!         cpanm --local-lib=~/perl5 local::lib && eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib)
!

Your terminal reads like it has succeeded, so you try the script again, but it won’t run.

You read online that the @INC is a list of directories that Perl looks in to find installed modules, and you read a guide such as this one which goes into great detail about how adding the installation directory can help. The warning when you installed the module was that the module was installed to /Users/FantasticUsername/perl5, so you add lines such as the following to your ~/.bashrc or ~/.bash_profile file:

PERL5LIB=/Users/FantasticUsername/perl5
export PERL5LIB

This still doesn’t work, and you want to throw your keyboard.

The tip

On both Mac and Linux, specifying a library location doesn’t mean that cpanm or cpan will install the actual modules there. Generally, they will store the modules in a subdirectory of the listed location. Go into this directory and find the location of a directory which matches the first word of your module name (ie: for the module Random::ModuleName, you’re looking for Random). Use pwd to get your current directory when you can see it. It should look something like one of these:

/home/FantasticUsername/perl5/lib/perl5
/Users/FantasticUsername/perl5/lib/perl5

This is the real location of the Perl 5 library that needs to be assigned to the $PERL5LIB variable.

PERL5LIB=/Users/FantasticUsername/perl5/lib/perl5
export PERL5LIB

If you have multiple installation locations for Perl modules, you may not want to delete the other location(s) of libraries. In this case, you can add your new installation location to the front of a colon-separated-list of library locations like so:

PERL5LIB=/Users/FantasticUsername/perl5/lib/perl5:${PERL5LIB}
export PERL5LIB