Is there a way to load Bash environment variables ( defined by sourcing ~/.bashrc from a Bash shell ) from within a Perl script. Specifically, I am interested in reloading the PERL5LIB environment variable in the case the caller of the Perl script is not a child of a bash process1. In that case, $ENV{PERL5LIB} will not exist, which will prevent me from using any modules I have installed in my home directory.
A first attempt could be to rerun my myself under bash with the -l option. This will load ~/.bash_profile, and I have set that file up to load ~/.bashrc.
#! /usr/bin/env perl
use strict;
use warnings;
if ( !exists $ENV{PERL5LIB} ) {
exec 'bash', '-lc', $0, @ARGV;
}
else {
# Main program starts here..
}
But this could enter an infinite loop for the (unexpected) case that PERL5LIB is not defined in ~/.bashrc.
Footnotes:
[1] This specific case occured for me when trying to write a native host to be called from the google-chrome process using the Google Chrome Extension API.
@INCwhen you start up, rather than require the user to manage this (and any possible conflict between your libraries and those they actually want for their own needs).use libon your home directory instead of relying on the environment variable?bash; it's the caller's responsibility to make surePERL5LIBhas an appropriate value when calling the script.google-chrome, and I agree that it is strange that it does not setupPERL5LIBfor its child processes.PERL5LIBis probably something that should be defined once from.bash_profileand inherited, rather than setting every time an interactive shell is started. I don't know ifgoogle-chromeis being launched from a process that descends from an appropriate login shell in your case, though.