13 Part 11: Perl Variables

LinuxChix Perl Course Part 11: Perl Variables

1) Weird Names for Variables
2) Use English
3) The Special Variable "$_"
4) Exercises
5) Answer to Previous Exercise
6) Past Information
7) Credits
8) Licensing

-----------------------------------

1) Weird Names for Variables

Perl has lots of built-in variables with odd names. We've already seen
two of them: "$/" and "$!". But there are plenty more.

For example, "$<" and "$>" are the current and effective user ID,
respectively:

print "UID = $<\n";
print "Effective UID = $>\n";

The variables "$(" and "$)" represent your real and effective group IDs,
but they are more complicated.

Assigning to these variables will change the user or group under which
the process runs (within the limits of what your operating system allows
of course: trying to set "$<" to zero won't necessarily make you root).
But this could lead to problems with security and cross-platform
compatibility that are too complicated to go into here, so do a little
research before actually trying it.

The variables "$?" (result of executing a program) and "$$" (PID of
current process) have a similar meaning to their meaning in certain
shells, such as bash (the Bourne-Again Shell).

The variable "$," is easy to remember because its content is output
every time you see a comma in a print statement:

my($a,$b,$c) = ('foo', 'bar', 'baz');
print $a, $b, $c, "\n"; # Output is: "foobarbaz"
$, = '##';
print $a, $b, $c, "\n"; # Output is: "foo##bar##baz##";

For the full list of weird Perl variables, see "man perlvar". Don't
worry about memorising all of them; you'll learn the most important ones
simply through use.

All special Perl variables are global, which means that setting them
introduces potential side effects. We will see more about that next week.

-----------------------------------

2) Use English

No, this isn't an anglo-centric statement; it's a Perl command.

All special variables have names that are (arguably) more intuitive. For
example, "$," can also be called "$OUTPUT_FIELD_SEPARATOR" or
"$OFS". However, to use these names you must "use English;".

#!/usr/bin/perl -w
use strict;
use English;

$OUTPUT_FIELD_SEPARATOR = '|'; # Equivalent to: $, = '|';
print 'field 1', 'field 2';
print "\n";

However, due to an oversight in its implementation, "use English" makes
regular expression matches slower. So you are welcome to "use English"
right now if it helps you, but it's not recommended for writing Perl
libraries.

-----------------------------------

3) The Special Variable "$_"

Probably the most used variable in Perl is "$_", pronounced
"dollar-underscore" and called "$ARG" if you "use English". This
variable is passed as an understood parameter to many functions and
operators if an expected parameter is not explicitly passed. Examples:

chomp; # Equivalent to chomp($_);
print; # Equivalent to print $_;

"$_" is the understood subject of "tr///", "m//" and "s///":

s/foo/bar/; # Equivalent to $_ =~ s/foo/bar/;
if ( /baz/ ) { ... } # Equivalent to $_ ~= m/baz/

In addition, for any file handle "HANDLE", the following are equivalent:

while ( <HANDLE> ) { ... }
# ... is equivalent to ...
while ( defined( $_ = <HANDLE> ) ) { ... }

Caution: this trick only works if "<HANDLE>" is used as the sole
criterion of a while loop. So the following do NOT set "$_":

<HANDLE>; # NOT equivalent to $_ = <HANDLE>;
if ( <HANDLE> ) { ... } # NOT $_ = <HANDLE>

The classic example use of "$_" is a program like the following:

while ( <STDIN> ) { # Read line of input.
s/puppy/dog/g; # Change "puppy" to "dog".
s/kitten/cat/g; # Change "kitten" to "cat".
print; # Output the line, with changes.
}

Despite the "magic" nature of "$_", it can still be assigned to or from
just like any other variable. (This is handy for testing.)

$_ = "foo";
my $bar = $_;

We will see more uses of "$_" with time.

-----------------------------------

4) Exercise

One previous exercise was to write a program that converted the word
"dead" to "metabolically different". Here was my solution to this problem:

#!/usr/bin/perl -w
use strict;

while ( defined(my $line = <STDIN>) ) {
$line =~ s/\bdead\b/metabolically different/g;
print $line;
}

Modify this program (or your own if you prefer) so that it uses no
variables except "STDIN" and "$_", and try to avoid explicitly naming "$_".

-----------------------------------


5) Answer to Previous Exercise

Here is my "pidof" program.

#!/usr/bin/perl
use strict;

my $program = shift; # Program to search for.

unless ( defined($program) ) {
die "Please give the name of a program on the command-line.\n";
}

open PS, "ps -e |" or die "Couldn't execute 'ps': $!";
while ( my $line = <PS> ) {
chomp($line);
# PID TTY Time Program
if ( $line =~ /^\s*(\d+)\s+\S+\s+\S+\s+(\S+)\s*$/ ) {
print "$1\n" if $2 eq $program;
}
}
close PS;

-----------------------------------

6) Past Information

Part 1: Getting Started
http://linuxchix.org/pipermail/courses/2003-March/001147.html

Part 2: Scalar Data
http://linuxchix.org/pipermail/courses/2003-March/001153.html

Part 3: User Input
http://linuxchix.org/pipermail/courses/2003-April/001170.html

Part 4: Control Structures
http://linuxchix.org/pipermail/courses/2003-April/001184.html

Part 4.5, a review with a little new information at the end:
http://linuxchix.org/pipermail/courses/2003-July/001297.html

Part 5: The "tr///" Operator
http://linuxchix.org/pipermail/courses/2003-July/001302.html

Part 6: The "m//" Operator
http://linuxchix.org/pipermail/courses/2003-August/001305.html

Part 7: More About "m//"
http://linuxchix.org/pipermail/courses/2003-August/001322.html

Part 8: The "s///" Operator
http://linuxchix.org/pipermail/courses/2003-August/001330.html

Part 9: Simple File Access
http://linuxchix.org/pipermail/courses/2003-September/001340.html

Part 10:
http://linuxchix.org/pipermail/courses/2003-September/001344.html

-----------------------------------

7) Credits

Works cited:
a) "man perlvar"
b) Kirrily Robert, Paul Fenwick and Jacinta Richardson's "Intermediate
Perl",
which you can find (along with their "Introduction to Perl") at:
http://www.perltraining.com.au/notes.html

Thanks to Jacinta Richardson for fact checking.

-----------------------------------

8) Licensing

This course (i.e., all parts of it) is copyright 2003 by Alice Wood and
Dan Richter, and is released under the same license as Perl itself
(Artistic License or GPL, your choice). This is the license of choice to
make it easy for other people to integrate your Perl code/documentation
into their own projects. It is not generally used in projects unrelated
to Perl.