| Developer's Daily | Perl Education |
| front page | java | perl | unix | dev directory | web log |
Introduction
One of the great features of the Perl language is it's support of associative arrays. Unlike normal arrays, whose subscripts can only be integers, the subscripts of associative arrays are text strings. This may not sound like much yet, but with associative arrays (or hashes as they're now called) we can create fairly complex data structures with Perl.
Perl programs use a special associative array name "%ENV" to store the shell environment variables they inherit when they begin running. This article explores the proper way to access your shell variables from within a Perl program using the %ENV associative arrays.
A simple "Hello" program
It has become stylish when introducing a language to a new audience
to begin with a "Hello, world" program. Our approach to this normal
introduction will be a bit more personal:
|
#!/usr/bin/perl
|
| Listing 1: | This simple program prints a customized "Hello"
message when run from the Solaris command line, printing the word "Hello",
followed by the users login name.
|
A "Hello, world" program written with the use of an associative array is shown in Listing 1. If your login name is fred, and you ran the program shown in Listing 1 from the Solaris command line, the last line of the program would print the following output:
The variable "ENV" is actually a special associative array within Perl that holds the contents of your shell environment variables. You can access any shell environment variable in the same way we accessed the username in Listing 1. For instance, the following commands retrieve the desired PATH and PWD information from a user's environment:
Printing all of your environment variables
If you're interested in seeing the contents of all of your environment variables, you can easily print them all out with a foreach command:
The keys function gets all of the keys, or subscripts, out of the specified associative array. In this example, the keys of the associative array %ENV will be the name of your environment variables. In Listing 1, the key (or subscript) was 'LOGNAME', and the value of the array element $ENV{'LOGNAME'} was fred:
|
PERL STATEMENT
Key Value
|
| Listing 2: | This listing shows typical values of several environment variables that would be stored in the associative array %ENV on a Solaris 2.4 system. |
When the keys function is run within the foreach loop, it returns only the subscript names LOGNAME, HOME, SHELL, and EDITOR - it does not return the values of each element.
The sort function, which precedes the keys function, sorts the output of the keys command before that output is used by the foreach statement. Given the keys shown in Listing 2, the sorted output would leave the keys in the following order: EDITOR, HOME, LOGNAME, and SHELL.
The foreach statement creates a loop that can be read like this:
The $_ variable contains the default pattern space when working with Perl. Therefore, within the foreach loop, the variable $_ will be assigned the contents of the list of sorted keys, one element at a time. The first time through the loop, the print command
Conclusion
Having associative arrays available in Perl makes something like accessing
our shell environment variables very easy. Instead of having to deal
with many different separate variable names, we simply work with one array
which contains the contents of the
separate variables.
In the future we'll explain more about the magical powers of associative
arrays, and how they can be great tools for getting a lot of work done
with a minimal amount of coding.
Copyright © 1998 DevDaily Interactive, Inc.
All Rights Reserved.