| Previous | Table of Contents | Next |
In madlibs, every <> was followed by a chomp(). Thats because when you press [ENTER], you append a newline to $num and $noun and $verb. The chomp() function removes the newline from the end of a string.
If you hadnt chomped your variables, the output would have looked like this:
10 lord s a-leap ing
You could also have used chop() instead of chomp(). The chop() function is simpleit indiscriminately lops off the last character of a string. chomp() is a bit more selectiveit removes the last character only if its a line separator. (Thats usually a newline, but you can define it to be anything you want with the $/ variable, described in Chapter 6, Session 4.)
$string = This has a newline:\n; chomp $string;
(Now $string is This has a newline.)
$string = This has a newline:\n; chop $string;
($string is also This has a newline.)
$string = This does not; chomp $string;
($string remains intact: This does not.)
$string = This does not; chop $string;
($string is now This does no.)
chop() returns the character lopped off (if any). Thats why its critical NOT to do this:
$string = This has a newline:\n; $string = chop $string;
because that sets $string to a solitary \n, annihilating everything else.
chomp() returns the number of characters that were lopped off. That might be 0, 1, or even more (but only if youve changed $/).
Because chomp() and chop() are functions, you can surround their arguments with parentheses if you want: chomp($num) is the same as chomp $num.
You can think of operators as functions that expect arguments in strange places, instead of in the comma-separated list required by normal functions such as print() or chomp() (Figure 1-3). Youve seen a couple of operators already: the = operator, which expects arguments on its left and right
Figure 1-3 Operators
$x = 7
just like the arithmetic operators
+ - * /
which behave as you might expect.
8 * 2
is 16, and
5 / 3
is 1.666666666666667.
This statement employs two operators, = and +:
$x = 3 + 4
and sets $x to 7.
Youve also seen the string concatenation operator: ., which glues two strings together. The madlibs program uses the . operator to turn lord into lords.
$noun = $noun . s;
To make our leap into a-leaping, madlibs uses . twice.
$verb = a- . $verb . ing;
Another operator is x, which expects a string (or an array; more about those in Session 5) on its left and a number on its right and duplicates the string (or the array) that many times.
$word = ba x 4;
sets $word to babababa.
8x8
yields 88888888.
$a = (xy x 2 . z) x 3; print $a;
prints xyxyzxyxyzxyxyz.
Of course, not all operators take two arguments:
-7
might look like a simple number. Well, it is. But its also the unary operator - applied to the argument 7.
#!/usr/bin/perl $word = xyzzy\n; chop $word; chop $word; $word = $word . ies; print $word;
pie in the sky pie in the sky
$food = pie; print $food in the sky; print $food in the sky;
$food = pie; print $food in the sky\n; print $food in the sky\n;
$food = pie; print $food in the sky; print $food in the sky;
$food = pie; print $food in the sky\n; print $food in the sky\n;
Difficulty: Easy
Write a program that asks the user for a first name and then asks the user for a last name. The program should then print the first and last names together on the same line.
| Previous | Table of Contents | Next |