Previous Table of Contents Next


The chomp() and chop() Functions

In madlibs, every <> was followed by a chomp(). That’s 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 hadn’t 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 simple—it indiscriminately lops off the last character of a string. chomp() is a bit more selective—it removes the last character only if it’s a line separator. (That’s 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). That’s why it’s 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 you’ve 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.

Operators

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). You’ve 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.

You’ve 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 it’s also the unary operator - applied to the argument 7.

Quiz 2

1.  Which statement is false?
a.  A scalar variable can be a string or a number.
b.  chop()lops off the last character of a string no matter what it is.
c.  Double quotes perform variable interpolation.
d.  print() requires quotes.
2.  What does the following program print?
#!/usr/bin/perl
$word = “xyzzy\n”;
chop $word;
chop $word;
$word = $word . ‘ies’;
print $word;
a.  ies
b.  xyzies
c.  xyzzies
d.  Nothing, because there’s an error in the program.
3.  Which pair of lines produces the following output?
pie in the sky
pie in the sky
a.  
$food = “pie”;
print ‘$food in the sky’;
print ‘$food in the sky’;
b.  
$food = ‘pie’;
print ‘$food in the sky\n’;
print ‘$food in the sky\n’;
c.  
$food = “pie”;
print “$food in the sky”;
print “$food in the sky”;
d.  
$food = ‘pie’;
print “$food in the sky\n”;
print “$food in the sky\n”;
4.  Which statement is a legal scalar assignment?
a.  Days14 = 0;
b.  $DAYS____14 = 0;
c.  $14Days = 0;
d.  $Days.14 = 0;

Exercise 2

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