Original command does not work when used in a Perl script
Consider the below code:
#!/usr/bin/perl
use strict;
use warnings;
$value="export PI = 3.14";
open(IN,">>/root/.bashrc");
print IN $value;
close(IN);
`source /root/.bashrc`;
I want to create an env variable using a Perl script. I have given 777 rights to the root folder as well as the .bashrc file. The $ value in the script is appended to the .bashrc, but when I use "env" or "printenv" to display the env variables, I cannot see the one added to the .bashrc.I think the source command is not working because when i source .bashrc file from CL display it in env list.Please help me or suggest another way.Thanks in advance.
source to share
The reverse steps start the subshell process in which the source code will be executed. And then the output is hooked and your perl script won't execute. Have you tried $ ENV {PI} = 3.14 ;? This will change the environment of the running perl script and any subsequent subprocesses created.
use strict ;
use warnings ;
$ENV{TEST} = 'Yo.' ;
my $subshelled = qx!echo \$TEST! ;
chomp $subshelled ;
printf "Subshelled result: '%s'\n" , $subshelled ;
my $subshellenv = qx!env! ;
printf "Env:\n%s\n" , $subshellenv ;
will print
Subshelled result: 'Yo.'
Env:
...
TEST=Yo.
source to share