Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set an environment variable in Perl?

How do I set an environment variable in Perl?

I want to set $HOME to a different directory than the default.

like image 334
user7305 Avatar asked Nov 17 '09 10:11

user7305


People also ask

How do I check if an environment variable is set in Perl?

But in the specific case of an environment variable, you don't need the exists test anyway: environment variables are always strings, so they are never undef unless they haven't been set — making exists equivalent to defined for them. So you can just write if(defined $ENV{'VARIABLE_NAME'}) or if(defined $debug) .

How do I export a variable in Perl script?

6.3. For a module to export one or more identifiers into a caller's namespace, it must: use the Exporter module, which is part of the standard Perl distribution, declare the module to inherit Exporter's capabilities, by setting the variable @ISA (see Section 7.3. 1) to equal ('Exporter'), and.

What are the three categories of Perl variables?

Perl has three main variable types: scalars, arrays, and hashes. A scalar represents a single value: my $animal = "camel"; my $answer = 42; Scalar values can be strings, integers or floating point numbers, and Perl will automatically convert between them as required.


1 Answers

You can do it like this:

$ENV{HOME} = 'something different';

But please note that this will only have an effect within the rest of your script. When your script exits, the calling shell will not see any changes.

As perldoc -v %ENV says:

%ENV The hash %ENV contains your current environment. Setting a value in "ENV" changes the environment for any child processes you subsequently "fork()" off.

like image 147
innaM Avatar answered Oct 27 '22 07:10

innaM