Monday, March 22, 2010

Setting environment variable using Perl Programs

Most of the time you may need to add the path of some directories where your code files are present or even your perl modules are there, and you want to use them.

Since %ENV is a special Hash in Perl, which contains values for all of your environment variables. So you can set any variables just like other hash variable in Perl.
So you can add following directories in your Path variable as:

$ENV{PATH} = 'C:\bin; C:\mymodule';

In this way it will replace your previous path to the one you are adding, but that will be till the program runs.

For Adding path in addition to existing path do it like following:

my $path = $ENV{PATH};
$path = $path . ';C:\bin; C:\mymodule';
$ENV{PATH} = $path;
print "\n\n $ENV{PATH}";


Similarly other variables can also be set.
You can check whether it's added or not by :

print $ENV{PATH};

No comments:

Post a Comment