Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I export the calling environment correctly into a subshell in Perl?

I am trying to use a Perl script to add certain directories in a tool(wpj) we have. The direct command would look something like

 $ wpj add path/to/desired/library makefile.wpj

I want to do a script to automate this since a lot of this has to be done. However, a very special set of environment variables among other things are setup to make 'wpj'. If I call wpj with backquotes from Perl, the call fails. You can see the code below

$command = "wpj add library path\\to\\file.wpj \\path\\to\\add";
print $command."\n";
$result = `$command`;
print "->".$result."\n";

For this I get

wpj: not found

However, the same call from the shell succeeds. Something is not correctly exported, could you please give me suggestions on how to export the calling environment into the subshell created by backquotes?

like image 614
Pedro Perez Avatar asked Dec 26 '22 21:12

Pedro Perez


2 Answers

Check the PATH and modify its content if needed:

use Env qw(@PATH);

# check the PATH:
print join("\n", @PATH);

# modify its content:
push @PATH, "/usr/bin/wpj";

The official manual for this module.

like image 61
Vidul Avatar answered Dec 31 '22 18:12

Vidul


The environment is inherited without you have to do anything. I suspect wpj is not an executable, but a shell alias. As such, you'll need to run the command in a shell that has that alias defined. This might require an interactive shell, depending on how you create your aliases.

system('bash', '-i', '-c', 'wpj add path/to/desired/library makefile.wpj');
like image 31
ikegami Avatar answered Dec 31 '22 17:12

ikegami