Flylib.com

Books Software

 
 
 

The chomp Operator

2.9 The chomp Operator

The first time you read about the chomp operator, it seems terribly overspecialized. It works on a variable. The variable has to hold a string. And if the string ends in a newline character, chomp can get rid of the newline. That's (nearly) all it does. For example:

$text = "a line of text\n"; # Or the same thing from <STDIN>
chomp($text); # Gets rid of the newline character

But it turns out to be so useful, you'll put it into nearly every program you write. As you see, it's the best way to remove a trailing newline from a string in a variable. In fact, there's an easier way to use chomp , because of a simple rule: any time that you need a variable in Perl, you can use an assignment instead. First, Perl does the assignment. Then it uses the variable in whatever way you requested . So the most common use of chomp looks like this:

chomp($text = <STDIN>); # Read the text, without the newline character
 
$text = <STDIN>; # Do the same thing...
chomp($text); # ...but in two steps

At first glance, the combined chomp may not seem to be the easy way, especially if it seems more complex! If you think of it as two operations—read a line, then chomp it—then it's more natural to write it as two statements. But if you think of it as one operation—read just the text, not the newline—it's more natural to write the one statement. And since most other Perl programmers are going to write it that way, you may as well get used to it now.

chomp is actually a function. As a function, it has a return value, which is the number of characters removed. This number is hardly ever useful:

$food = <STDIN>;
$betty = chomp $food; # gets the value 1 - but we knew that!

As you see, you may write chomp with or without the parentheses. This is another general rule in Perl: except in cases where it changes the meaning to remove them, parentheses are always optional.

If a line ends with two or more newlines, [27] chomp removes only one. If there's no newline, it does nothing, and returns zero.

[27] This situation can't arise if we're reading a line at a time, but it certainly can when we have set the input separator ( $/ ) to something other than newline, or use the read function, or perhaps have glued some strings together ourselves .

If you work with older Perl programs, you may run across the chop operator. It's similar, but removes any trailing character, not just a trailing newline. Since that could accidentally turn pebbles into pebble , it's usually not what you want.

 

2.10 The while Control Structure

Like most algorithmic programming languages, Perl has a number of looping structures. [28] The while loop repeats a block of code as long as a condition is true:

[28] Every programmer eventually creates an infinite loop by accident . If your program keeps running and running, though, you can generally stop it in the same way you'd stop any other program on your system. Often, typing Control-C will stop a runaway program; check with your system's documentation to be sure.

$count = 0;
while ($count < 10) {
 $count += 1;
 print "count is now $count\n"; # Gives values from 1 to 10
}

As always in Perl, the truth value here works like the truth value in the if test. Also like the if control structure, the block curly braces are required. The conditional expression is evaluated before the first iteration, so the loop may be skipped completely, if the condition is initially false.