Previous Table of Contents Next


@ARGV

$_ isn’t the only default variable. (@_ is another one—an array used to pass arguments to subroutines—but that won’t be discussed until Chapter 4.) Another useful variable, @ARGV, contains an array of all the command-line arguments provided to your program (Figure 1-12).


Figure 1-12  @ARGV

The first command-line argument can be found in $ARGV[0], the second command-line argument can be found in $ARGV[1], and so on. (That’s not quite the same as C’s argv, which stores the program name in argv[0]. Perl stores the filename in __FILE__, the process name in $0, and the name of the Perl binary in $^X. More about those in later chapters.)

Suppose program is a Perl program called with three command-line arguments:

% program 5.6 index 0

Then @ARGV will be equal to (5.6, ‘index’, 0).

None of the programs you’ve seen so far has had any arguments, so here’s cube (Listing 1-19), which raises its command-line argument to the third power.

Listing 1-19 cube: Using @ARGV

#!/usr/bin/perl -w
$num = $ARGV[0];      # Set $num equal to the first command-line argument
print $num ** 3;      # Print its cube.

% cube 5

($ARGV[0] is the first element of @ARGV, which is 5.)

125
        % cube -7
RESULT: -343

        % cube 28.98
RESULT: 24338.574792

        % cube

(Whoops! Forgot the command-line argument!)

0

(That’s okay—Perl assumes a 0.)

Some functions use @ARGV as a default variable instead of $_. shift() is an example.

shift;

removes and returns the leftmost argument of @ARGV. (You get analogous behavior with pop(), but that’s not as useful.)

Listing 1-20 shows another way to write cube.

Listing 1-20 cube2: Using shift() to extract program arguments

#!/usr/bin/perl -w
$num = shift;
print $num ** 3;

An even shorter way is as handled in cube3, shown in Listing 1-21.

Listing 1-21 cube3: Using shift() to extract program arguments

#!/usr/bin/perl -w
print shift() ** 3;

Of course, @ARGV can hold more than one command-line argument. Listing 1-22 shows a program called expo that exponentiates two command-line arguments. expo expects the base and the exponent on the command line, in that order.

Listing 1-22 expo: Accessing the first two command-line arguments

#!/usr/bin/perl -w

# These next two lines are equivalent to ($base, $exponent) = @ARGV

$base     = shift;     # Remove the leftmost argument, shortening @ARGV.
$exponent = shift;     # Remove the (now-)leftmost argument
                       #   Now @ARGV should be empty.

print $base ** $exponent;
% expo 5 3

(Raises 5 to the 3rd power.)

RESULT: 125

        % expo 2 0.5

(Computes the square root of 2.)

RESULT: 1.414213562373095
        % expo 1.00001 100000

(Computes the 100000th power of 1.00001.)

RESULT: 2.718268237192297
        % expo -1 .5

(Attempts to compute the square root of -1.)

RESULT: NaN

(Meaning “not a number.” Your computer may print something slightly different.)

Quiz 7

1.  Default variables are used by all of
a.  int(), print(), *=.
b.  shift(), last, chop().
c.  shift(), int(), foreach
d.  **, eq, next.
2.  What’s printed by the following code?
@movies = (‘Sleeper’, ‘Poltergeist’, ‘Top Gun’);
foreach (@movies) {
    $_ .= ‘s’;
    print;  print ‘ ’;
}
a.  Sleeper Poltergeist Top Guns
b.  Sleepers Poltergeists Tops Guns
c.  Sleepers Poltergeists Top Guns
d.  s s s s
3.  Describe the behavior of the following program.
#!/usr/bin/perl
@movies = shift;
while (<>) {
    chomp;
    push(@movies, $_);
}
a.
1.  It places the first command-line argument in @movies.
2.  Whenever the user types something, it places the command-line argument in @movies.
b.  It takes the user input and adds it to the end of @movies. It repeats.
c.
1.  It places the first command-line argument in @movies.
2.  It takes the user input and adds it to the beginning of @movies.
d.
1.  It places the first command-line argument in @movies.
2.  It takes the user input and adds it to the end of @movies. It repeats.
4.  What will the following program print? (Beware! This one is tricky.)
#!/usr/bin/perl
@movies = (‘Casablanca’, ‘Star Wars’, ‘E.T.’, ‘Home Alone’);
while (<>) {
  foreach (@movies) {
     chop;
  }
  print;
}
a.  Whatever the user types will be printed.
b.  CasablancStar WarE.THome Alon
c.  CasablancaStar WarsE.T.Home Alone
d.  Home Alon


Previous Table of Contents Next