Wednesday, February 24, 2010

Learning Perl: List all environment variables

Listing all the environment variables using the built-in %ENV hash and simple for loop:

my @env_names = keys %ENV;

my $n;
my $v;

for ($k=0 ; $k<=$#env_names ; $k++)
{
    $n = $env_names[$k];
    $v = $ENV{"$n"};
    print "\n $n=$v";
}

A much shorter version using for each:

my @env_names = keys %ENV;
my $v;
foreach (@env_names)
{

    $v = $ENV{"$_"};
    print "\n $_=$v";
}

Putting it all in one line

print "\n $_=" , $ENV{"$_"} foreach (keys %ENV);

However, DOS still attractive … just use the SET command to get same result of the above :)

Thursday, February 04, 2010