rohit2007 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Friends, I am writing a programme which can check the values of different environment variables and at the same time compare with a standard value if the value does not match with standard value it should give a warning. below you can see a small part of the code which is checking the value of TEMP(environment variable)..I would like to test more than one value at a time.please help me how can i do it. Thanks for your efforts. cheers:)
#!/usr/bin/perl $a = 'TEMP'; $x = $ENV{$a}; if ($x =~ m#\QC:\DOCUME~1\z002n88f\LOCALS~1\Temp\E#) {print "cool\n"; } else { print "$x\n" }
Rohit HI Guys, Thank you all very much for your help!!I really appreciate active participation of all of you guys. With your help and with some surfing I could write this piece of code(please find the code below). This code has some defined environment variables(ENV_VAR) and it checks it with standard $ENV. I wanted to ask is there a possibality to add more values (parameters) to my defined "ENV_VAR"; which gives more detail about the defined ENV_VAR. For eg i say
ClientVersion => 2.24.103; value(attached to clientVersion which tells + that 2.24.103 of ClientVersion is value) COMPUTERNAME => DEB20ZYC; value(attached to COMPUTERNAME which tells t +hat DEB20ZYC is value ) CommonProgramFiles => C:\Program Files\Common Files; path(attached to +CommonProgramFiles which tells that C:\Program Files\Common Files is +path ) APPDATA => C:\Documents and Settings\prr2n88f\Appication Data; path(at +tached to CommonProgramFiles which tells that C:\Documents and Setti +ngs\prr2n88f\Appication Data is path )
In this way is it possible to have two/three values for each of the keys: APPDATA :- 1st value (2.24.103) ; 2ed value (value) CLEARCASE_PRIMARY_GROUP ClientVersion CommonProgramFiles:- 1st value (C:\Program Files\Common Files); 2ed value (path) COMPUTERNAME:- 1st value (DEB20ZYC) ; 2ed value (value)
#!/usr/bin/perl use strict; use warnings; use win32::ole; my (%ENV_VAR) = ( 'ALLUSERSPROFILE' => 'C:\Documents and Settings\All +Users', 'APPDATA' => 'C:\Documents and Settings\prr2n88f\Appication Data', 'CLEARCASE_PRIMARY_GROUP' => 'wp003\05083_ccuser_g', 'ClientVersion' => '2.24.103', 'CommonProgramFiles' => 'C:\Program Files\Common Files', 'COMPUTERNAME' => 'CAL20ZYC'); my $key = keys %ENV_VAR; my $value = values %ENV_VAR; while (($key, $value) =each %ENV_VAR) { print "$key => $value\n"; } if (exists $ENV_VAR{'ClientVersion'}){ print "this Environment variable is present\n";} else { print "This Environment variable is not present\n" } if ($ENV_VAR{ClientVersion} = $ENV{ClientVersion}) { print "$ENV_VAR{ClientVersion}\n" } else { print "Something is going wrong!!" }
Thanking you for your imputs. have a good day!!

Replies are listed 'Best First'.
Re: Environment variable
by toolic (Bishop) on Mar 12, 2010 at 14:38 UTC
    Create a hash of your expected variable names and value regexes. Then loop through your hash looking for matches:
    use strict; use warnings; my %expects = ( PROMPT => 5, FOO => 6 ); for my $evar (keys %expects) { if (exists $ENV{$evar}) { warn "No match for $evar\n" unless ($ENV{$evar} =~ /\Q$expects +{$evar}\E/); } else { warn "No $evar in ENV\n"; } }

    On a side note, please update your post to add code tags around your code. Read Writeup Formatting Tips.

Re: Environment variable
by cdarke (Prior) on Mar 12, 2010 at 14:49 UTC
    Do you mean something like this?
    use warnings; use strict; my %Defaults = (TEMP => q(\QC:\DOCUME~1\z002n88f\LOCALS~1\Temp\E), LOGONSERVER => q(\\MOLLY2), NUMBER_OF_PROCESSORS => 2, OS => q(Windows_NT)); while (my ($key, $value) = each %Defaults) { if (exists $ENV{$key} and $ENV{$key} eq $value) { print "$key is cool\n"; } else { print "$key is $value\n" } }
Re: Environment variable
by lostjimmy (Chaplain) on Mar 12, 2010 at 13:54 UTC
    What, exactly, is your question? If you want to compare more env variables, just add more comparisons.

    To make it easy, you can put your environment variables and their expected values in a hash, then make the comparisons in some kind of a loop.

      I want to write a programme in perl which can compare values of different ENVIRONMENT VARIABLES(env var) with the default variables and it should give a warning when the value of environment variable is not same as my default value. I have a small peice of programme which can compare the value of an environment variable. lets say i have in total 20 env var, but i m intrested in 10 env var(a1, a2, a3,.....,a10) their default values are (b1, b2, b3,......, b10). Now in Perl we have environment variables defined in $ENV so i want to take these 10 ENV VAR (a1,....,a10)compare their values from default and then tell if they match or not. for sure i can write this same thing ten times and get the work done but i m looking for an easier alternative(as done in the previous post). Thank you for your help!!
        I believe the simplest method would be to put the defaults in an array and the ENV-vars as well, then you could use grep or array::compare or something like that to find out differences, like
        my @defaults = (b1,..,b10);
        my @envs = (a1,..,a10);
        print "errors" if grep { $envs[$_] !~ $defaults[$_] } 0..9; this is untested and maybe not the most elegant, but I must confess I have not entirely understood your problem...
        Hope it helps...
Re: Environment variable
by apl (Monsignor) on Mar 12, 2010 at 14:59 UTC
    You need to read Data Type: Hash. After that, create a hash keyed on the environment variable name, having the value you expect the environment variable to have. Loop and test.
Re: Environment variable
by ssandv (Hermit) on Mar 12, 2010 at 17:34 UTC

    There are simple directions for formatting posts right below the input box. You should follow them. In particular, your code should be wrapped in <code></code> tags.