Perl Variables and Data Structures

 < Day Day Up > 

Perl is a weakly typed language, meaning that it does not require that you declare a data type, such as a type of value (data) to be stored in a particular variable. C, for example, makes you declare that a particular variable is an integer, a character, a structure, or whatever the case may be. Perl variables are whatever type they need to be, and can change type when you need them to.

Perl Variable Types

There are three variable types in Perl: scalars, arrays, and hashes. In an attempt to make each data type visually distinct, a different character is used to signify each variable type.

Scalar variables are indicated with the $ character, as in $penguin. Scalars can be numerical or strings, and they can change type from one to the other as needed. If you treat a number like a string, it becomes a string. If you treat a string like a number, it is translated into a number if it makes sense to do so; otherwise, it probably evaluates as 0.

For example, the string "76trombones" will evaluate as the number 76 if used in a numerical calculation, but the string "polar bear" will evaluate to 0.

Perl arrays are indicated with the @ character, as in @fish. An array is a list of values that are referenced by index number, starting with the first element numbered 0, just as in C and awk. Each element in the array is a scalar value. Because scalar values are indicated with the $ character, a single element in an array is also indicated with a $ character.

For example, $fish[2] refers to the third element in the @fish array. This tends to throw some people off, but is similar to arrays in C in which a first array element is 0.

Hashes are indicated with the % character, as in %employee. A hash, which used to go by the cumbersome name associative array, is a list of name and value pairs. Individual elements in the hash are referenced by name rather than by index (unlike an array). Again, because the values are scalars, the $ character is used for individual elements.

For example, $employee{name} gives you one value from the hash. Two rather useful functions for dealing with hashes are keys and values. The keys function returns an array containing all the keys of the hash, and values returns an array of the values of the hash. Using this approach, the Perl program in Listing 29.2 displays all the values in your environment, much like typing the bash shell's env command.

Listing 29.2. Displaying the Contents of the env Hash
 #!/usr/bin/perl foreach $key (keys %ENV)  {     print "$key = $ENV{$key}\n"; } 

Special Variables

Perl has a wide variety of special variables. These usually look like punctuation such as $_, $!, and $] and are extremely useful for shorthand code. ($_ is the default variable, $! is the error message returned by the operating system, and $] is the Perl version number.)

$_ is perhaps the most useful of these, and you will see that variable used often in this chapter. $_ is the Perl default variable, which is used when no argument is specified. For example, the following two statements are equivalent:

 chomp; chomp($_); 

The following loops are equivalent:

 for $cow (@cattle) {         print "$cow says moo.\n"; } for (@cattle)        {         print "$_ says moo.\n"; } 

For a complete listing of the special variables, you should see the perlvar document that comes with your Perl distribution (such as in the perlvar manual page), or you can go online to http://theoryx5.uwinnipeg.ca/CPAN/perl/pod/perlvar.html.

     < Day Day Up > 


    Red Hat Fedora 4 Unleashed
    Red Hat Fedora 4 Unleashed
    ISBN: 0672327929
    EAN: 2147483647
    Year: 2006
    Pages: 361

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net