chomp

chompVARIABLEchompLISTchomp
This function (normally) deletes a trailing newline from the
end of a string contained in a variable. This is a slightly safer
version of chop (described next) in
that it has no effect on a string that doesn’t end in a newline. More
specifically, it deletes the terminating string corresponding to the
current value of $/, and not just any
last character.
Unlike chop, chomp returns the number of characters
deleted. If $/ is "" (in paragraph mode), chomp removes all trailing newlines from the
selected string (or strings, if chomping a
LIST). When in slurp mode ($/ = undef) or fixed-length record mode
($/ is a reference to an integer),
chomp does nothing. You cannot
chomp a literal, only a variable.
Chomping a hash chomps only the
values, not the keys.
For example:
while (<PASSWD>) {
chomp; # avoid \n on last field
@array = split /:/;
...
}I/O layers are allowed to override the value of the $/ variable and mark how strings should be
chomped. This has the advantage that
an I/O layer can recognize more than one variety of line terminator
(like the Unicode paragraph and line separators), but still safely
chomp whatever terminates the current
line.
The chomp function is not currently smart enough to handle Unicode linebreak sequences, whose regex metacharacter ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access