Reputation: 625
I want to source file in perl script, first, I use the below method:
system("source env.bash");
but I got a error like:
Can't exec "source": No such file or directory at s-test.pl line 10.
I cannot fix the issue, so I use Shell::GetEnv:
use Shell::GetEnv;
$env = Shell::GetEnv->new('bash', 'source env.bash' );
$envs = $env->envs(%opt);
$env->import_envs(%opt);
but it seems doesn't work at all, can anyone help me to resolve it? Thanks so much.
Upvotes: 0
Views: 973
Reputation: 263497
system()
(at least on Unix-like systems) invokes the named command via /bin/sh
. Unlike bash, /bin/sh
doesn't have source
as a synonym for .
.
But even if you changed it to system(". env.bash")
, it wouldn't work. Presumably env.bash
sets environment variables (export FOO=BAR; ...
). Such settings apply only to the current process. It would invoke a shell, set the environment variables in that shell, and then have no effect on the invoking (perl) process.
I've never used Shell::GetEnv
before, but according to the documentation %opt
is used to pass options to the functions You haven't shown us what value %opt
has. Are you sure you need it? Perhaps you just want to do:
...
$envs = $env->envs();
$env->import_envs();
Upvotes: 1