Chomp can remove a line break from the end of a string. This is basically all the functions it can accomplish, as shown in the following example:
$ Text = "a line of text/N"; # It can also be input by <stdin>
Chomp ($ text); # Remove the line break (/N ).
It is very useful, basically every one of youProgramIt is used. As you will know, this is the best way to remove the line break at the end of the string. Based on a basic principle in Perl: Where variables need to be used, values can be used instead. We have a simpler method to use chomp. Perl first performs the value assignment operation and then uses this variable. Therefore, the most common method to use chomp is:
Chomp ($ text = <stdin>); # Read, but not line break
$ Text = <stdin>;
Chomp ($ text); # Same as above, but completed in two steps
From the first glance, the method of the first combination looks more complicated. If we take the preceding two steps as an operation and read one row and then chomp, We will write the two statement methods.
It looks natural. If you think of it as an operation and read a row 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 removed characters. This number is basically useless:
$ Food = <stdin>;
$ Betty = chomp $ food; # obtain the value 1.
As shown above, Chomp can be used or parentheses () are not used (). This is a general rule in Perl: parentheses can be omitted unless they are removed.
If there are two or more linefeeds at the end, Chomp removes only one. If there is no, nothing will be done. 0 is returned.