| Previous | Table of Contents | Next |
$_ isnt the only default variable. (@_ is another onean array used to pass arguments to subroutinesbut that wont 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. (Thats not quite the same as Cs 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 youve seen so far has had any arguments, so heres 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
(Thats okayPerl 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 thats 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.)
@movies = (Sleeper, Poltergeist, Top Gun);
foreach (@movies) {
$_ .= s;
print; print ;
}
#!/usr/bin/perl
@movies = shift;
while (<>) {
chomp;
push(@movies, $_);
}
#!/usr/bin/perl
@movies = (Casablanca, Star Wars, E.T., Home Alone);
while (<>) {
foreach (@movies) {
chop;
}
print;
}
| Previous | Table of Contents | Next |