CIW Course Revision Site
Click here for domain name registration and web hosting

Perl - Environment Variables

In a Nutshell - CIW Course Section 2, Part B2, Chapter 9

 

What are Environment Variables?

The environment being referred to is the Operating System, therefore Environment Variables are variables appertaining to the operating system and all applications running under it.

The Perl scripting languages makes environment variables available in a special hash: %ENV

The environment variable names form the keys in the hash and the environment variable values make up the corresponding hash values.

foreach (sort keys(%ENV))
{
print("$_\t$ENV{$_}\n");
}

Environment Variable Script Output
Script Output

A sample of the output from the script can be seen above, compare this with what can be seen in the systems Environment Variable dialog:

Environment Variable System Dialog
Environment Variables System Dialog

Similarities may be seen in the two lists although I did note that the list from the Perl %ENV hash is considerably longer that the list in the system dialog. There will be other environment variables declared in the startup file config.sys if it exists.

Command Line Parameters

Any programming or scripting language worth it's salt will allow parameters to be passed with the program/script name when it is initiated. To facilitate this, Perl makes two special values available: @ARGV an array containing the parameters and $ARGV a scalar variable which holds the name of the current file when reading from the default filehandle.

When passing parameters to a Perl script the parameters must be space-delimited, not comma-delimited.

Design by Stephen

Certified Internet Webmaster

Page last Edited: 10 Nov 2011