| Previous | Table of Contents | Next |
#!/usr/bin/perl
$num = 10;
while ($num > 0) {
if ($num == 0) { print \n; }
else { print $num, ; }
$num++;
}
for ($i = -3; $i < 3; $i++) {
print $i ;
$i++;
}
$num = 20;
while ($num < 30) {
$num++;
next if ($num % 3) == 0;
last if $num == 19;
if (($num % 2) == 0) { $num += 3; } else { $num -= 3; }
print $num;
}
Difficulty: Easy
Pretend youre collaborating with your boss on a Perl program. The program should print out every multiple of 5 from 0 to 100. You know that you could easily do this in one line with a for loop.
for ($i = 0; $i <= 100; $i += 5) { print $i, ; }
Unfortunately, your boss didnt read this book, so his loop is a bit flawed. He gives you the following.
#!/usr/bin/perl
$num = 0;
while ($num >= 0) {
# <-----------------------your code goes here.
print $num ;
$num++;
}
Your boss prides himself on his programming skill. Dont offend him by changing his code. Instead, save your job by judiciously using next and last to make the program work.
The President said that the President will sign any bill that arrives on the Presidents desk.
The President said that shell sign any bill that arrives on her desk.
Pronouns such as hers shorten and simplify our sentences, relying upon listeners to put the right value in its place. Wouldnt it be nice if programming languages could do that? Unfortunately, most programming languages cant: If a function expects two arguments, you cant just say, Heres the first argument; use the obvious choice for the second.
Perl has something not found in many other programming languages: the default variable $_ (Figure 1-11).
Figure 1-11 $_
Some functions and operators use $_ in place of an argument. If so, you can omit that argument and Perl will use the contents of $_ instead. That might sound horrible; after all, wont that lead to surprising behavior sometimes? Not often, and statements with fewer arguments are easier to read. Youll find $_ quite handy..
Anyway, lets see what $_ can do. Consider the following foreach loop.
foreach $movie (@movies) { print $movie }
Thanks to $_, you can eliminate the somewhat redundant variable $movie.
foreach (@movies) { print }
Huh? you ask? Whered $movie go? And how does print() know what to print?
Heres how it works. When a foreach loop has no iteration variable (the $movie in foreach $movie (@movies) ), it uses $_ instead. And if print() is given nothing to print, it uses the contents of $_. So you dont need $movie at all.
Several of the functions youve seen use $_.
chomp;
is the same as
chomp($_);
and
print;
is the same as
print($_);
and
int;
is the same as
int($_);
Whenever a while loop is given <> as its condition, it stores each line of user input in $_. Thats demonstrated in parrot (Listing 1-18), which echoes everything the user types.
Listing 1-18 parrot: Evaluating <> inside a while loop
#!/usr/bin/perl -w
while (<>) { # Stuff whatever the user types into $_
print; # ...and print it!
}
Look ma, no variables!
% parrot
RESULT: This is line one.
This is line one.
Hello?
Hello?
Polly wanna cracker?
Polly wanna cracker?
<CTRL-D>
[CTRL]-[D] terminates standard input, so the while loop exits. (Use [CTRL]-[Z] followed by a [RETURN] on MS-DOS computers.)
You can also assign values to $_, just like any other variable.
$_ = Poughkeepsie; print;
prints Poughkeepsie.
| Previous | Table of Contents | Next |