Introduction to string operation functions chomp and chop in Perl


Chomp and chop are characters used to remove the tail of string variables, but they have their own differences.

The chomp function works on a variable that contains a string. If there is a newline at the end of the string, chomp can remove it. This is basically all the functions it can perform, as shown in the following example:

Copy codeThe code is as follows:

$text=”alineoftext\n      “; # It can also be entered by < stdin >
chomp($text); # remove line breaks (\ n).

It’s very useful. It’s used in almost every program you use. As you will know, this is the best way to remove the newline at the end of the string. Based on a basic principle in Perl: where variables need to be used, assignment expressions can be used instead. We have a simpler way to use chomp. Perl first does the assignment operation, and then uses this variable. Therefore, the most common way to use chomp is:

Copy codeThe code is as follows:

chomp($text=<STDIN>); # read in without newline


#Same as above, but in two steps

At first sight, the first combination method looks more complex. If you take the above as a two-step operation, read a line and then write chomp, the method of writing two statements seems more natural. If you regard it as an operation and read in a line without line breaks, it is more appropriate to write a statement. Since most Perl programmers use the first method, you should also use it.


Chomp is a function. As a function, it has a return value, which is the number of characters removed. This number is basically useless:

Copy codeThe code is as follows:

$betty=chomp($food); # Get the value 1

As mentioned above, you can use or not use parentheses () when using chomp. This is another general rule in Perl: parentheses can be omitted unless their meaning changes when they are removed. If there are two or more line breaks at the end, chomp removes only one. If not, do nothing and return 0. This does not happen when reading in one line at a time, but it can happen by using the input separator ($/) (which is not a newline (\ n)), the read function, or combining some strings.


When writing Perl program code, you may sometimes use the chop() function to remove the < > input newline character “\ n”, and sometimes use the chomp() function. In fact, the above usage can achieve the desired effect, but they also have subtle differences.
The chop () function simply removes the last character.
Chomp() function, first judge whether the last character is “\ n”, and then remove it.

The chop function will cut off the last character of the string variable and return the cut character. The chop function will cut it short no matter what is in the string, while the chomp function is more selective. You cannot use the chop direct variable, only the chop variable.

Copy codeThe code is as follows:

chop LIST

If you select a list variable, each string in the list will be truncated:

Copy codeThe code is as follows:

@lines=`cat myfile`;
chop @lines;

In the most common case, chop can be represented by substr:

Copy codeThe code is as follows:

$last_char = chop($var);
$last_ char = substr($var, -1, 1, “”); # ditto

Let’s look at the following complete example:

Copy codeThe code is as follows:

$string1 = “This is test”;
$retval  = chop( $string1 );
print ” Choped String is : $string1\n”;
print ” Character removed : $retval\n”;


Copy codeThe code is as follows:

Choped String is : This is tes
Number of characters removed : t