Perl

Perl Environment Variables

Two types of variables can be used in shell scripting languages: one is the shell variables and the other is the environment variables. The environment variables are worked as the global variables for the scripting languages. The environment-related data that is used to execute the script are stored in the environment variables. These variables are stored based on the key-value pair. The name of the variable is defined by the key that stores the value of the variable. The value of any environment variable can be set or accessed by the Perl script. The methods of accessing and modifying the environment variables using the Perl script are shown in this tutorial.

Uses of the Environment Variables in the Perl Script

The methods of accessing the particular or all environment variables and resetting any environment variable are shown in this part of the tutorial.

Example 1: Print the Particular Environment Variables

The environment variables are stored in the %ENV array that works like the associative array. The variable name is the key of the %ENV array and the value is stored in that particular key. Create a Perl file with the following script that prints three environment variables. The current login username is stored in the $ENV{‘USER’} variable. The current shell value is stored in the $ENV{‘SHELL’} variable. The current working directory is stored in the $ENV{‘PWD’} variable. These three variables are accessed and printed in the script.

#!/local/bin/perl
 
use strict;
use warnings;
use 5.34.0;
 
#Print the values of three environment variables
say "Username is " . $ENV{'USER'};
say "Current shell is " . $ENV{'SHELL'};
say "Current working directory is " . $ENV{'PWD'};

 

Output:

The following similar output appears after executing the script:

Example 2: Print the First Five Environment Variables

Create a Perl file with the following script to read the first five values of the %ENV array that contains all environment variables. Here, the “foreach” loop is used to read all environment variables based on the sorted key values. The “$counter” variable is used in the script to terminate the loop after printing the five environment variables. The “$counter” variable is incremented in each iteration of the loop and when the value of “$counter” is 5, the “last” statement is executed to exit from the loop.

#!/local/bin/perl

use strict;
use warnings;
use 5.34.0;

#Initialize the counter
my $counter = 0;
#Iterate the loop to read all environment variables
foreach (sort keys %ENV) {
   #Read the name of the environment variable
   my $key = $_;
   #Read the value of the environment variable
   my $value = $ENV{$key};
   #Print the variable name with the value
   say "$key => $value";
   #Increment the counter
   $counter++;
   #Check the counter value
   if ($counter >= 5)
   {
      #Terminate from the loop
      last;
   }
}

 

Output:

The following similar output appears after executing the script:

Example 3: Reset the Environment Variable

In the previous examples, the predefined values of the environment variables are accessed individually and using a loop. However, the value of any environment variable can be changed using the Perl script. Create a Perl file with the following script that prints the location of the HOME, changes the location to “/temp/files”, and prints the location of the HOME again after the update.

#!/local/bin/perl
 
use strict;
use warnings;
use 5.34.0;
 
 
#Print the value of the particular environment variable
say "The current value of the HOME is " . $ENV{'HOME'};
 
#Reset the environment variable
$ENV{'HOME'} = '/temp/files';
 
#Print the value of the particular environment variable after the update
say "The current value of the HOME is " . $ENV{'HOME'};

 

Output:

The following similar output appears after executing the script:

Example 4: Print the Environment Variables with Formatting

Create a Perl file with the following script that prints the environment variables with formatting using the printf() function. Here, the “foreach” loop is used to read all environment variables like in the previous example. The key-value pair of the environment variable is printed by adding the colon(:) between the key and value, and keeping a space of 15 characters for the key.

#!/local/bin/perl

use strict;
use warnings;
use 5.34.0;

#Iterate the loop to read all environment variables
foreach my $k (sort keys %ENV) {
   #Print the formatted output of the environment variables
   printf("%15s : %s\n" , $k, $ENV{$k});
}

 

Output:

The following similar output appears after executing the script. Some portions at the beginning of the output are shown here:

Conclusion

The environment variables are used for different purposes and it is sometimes required to change the current value of the particular environment variable for any programming purpose. The methods to get and set the values of the environment variables using the Perl script are shown in the examples of this tutorial to help the Perl users.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.