I am trying to set environment variable using Perl. It should export this variable outside (in current shell) not just in the script.
I tried:
`setenv X1 /p/fsd`
system("setenv X1 /p/fsd") == 0 or die "failed:$?"
system command "setenv X1 /p/fsd" failed: -1
$ENV{"X1"} = "/p/fsd";
Nothing seems to be working.
If it matters i am using TCSH.
For example, to determine the setting of your "PATH" environment variable, you can write a line of Perl code like this: $path = $ENV{'PATH'}; As you may remember, "%ENV" is a special hash in Perl that contains the value of all your environment variables.
Perl variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.
DESCRIPTION. Perl maintains environment variables in a special hash named %ENV . For when this access method is inconvenient, the Perl module Env allows environment variables to be treated as scalar or array variables.
$_ - The default input and pattern-searching space. @_ - Within a subroutine the array @_ contains the parameters passed to that subroutine. $" - When an array or an array slice is interpolated into a double-quoted string or a similar context such as /.../ , its elements are separated by this value.
$ENV{"X1"} = "/p/fsd";
is the right way.
Test in perlconsole
:
Perl> $ENV{X1} = "/p/fsd";
/p/fsd
Perl> system('echo $X1');
/p/fsd
0
NOTE
echo $X1
to prevent perl
to interpolate it itself.Equally important to know how to set an env variable is how to delete it:
delete $ENV{"X1"};
This will delete the environment variable for the perl process and any children processes. But the environment variable will still be set for the parent process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With