Perl Scripts ·
Removing Newlines in Perl with chomp()
By Manu Mathew, Technical Lead Engineer · Published 5 December 2013 · Last updated 5 October 2026

Every line you read from a file, a pipe or the keyboard ends with a line break, so handling newlines in Perl is one of the first things most scripts do. chomp() is the tool for it.
Quick answer
Use chomp. It removes a trailing newline (strictly, whatever is in $/, the input record separator) from a string or from every element of an array, and returns how many characters it removed. It never removes anything else, so it is safe to call more than once.
How do I use chomp()?
use strict;
use warnings;
my $line = "hello\n";
my $removed = chomp $line; # returns the number of characters removed: 1
my $again = chomp $line; # nothing left to remove: 0
my @names = ("asha\n", "ben", "chitra\n");
chomp @names; # chomps every element
The usual place for it is straight after reading input: while (my $line = <$fh>) { chomp $line; ... }.
Why is there still a character left on lines from Windows files?
Windows text files end lines with \r\n. On Linux or macOS, chomp removes only the \n, so a \r stays at the end and breaks comparisons such as $line eq 'total'.
my $windows = "total\r\n";
chomp $windows;
print length($windows), "\n"; # 6: the carriage return is still there
$windows =~ s/\r\z//;
print length($windows), "\n"; # 5
To strip any line ending in one step, $line =~ s/\R\z//; also works (\R matches any line break).
How is chomp different from chop?
chop removes the last character whatever it is. chomp only removes a line ending. For cleaning input, chomp is almost always what you want; see my note on chop().
Frequently asked questions
Can I read a line and chomp it in one statement?
Yes: chomp(my $line = <STDIN>); reads a line, stores it and removes the newline. The parentheses matter, because chomp has to act on the variable, so the assignment goes inside them.
Does chomp remove trailing spaces?
No. chomp removes only the input record separator, which is normally a newline. To remove trailing spaces and tabs as well, use s/\s+\z//, which also removes the newline and any carriage return in one step.
How do I chomp every line of a file at once?
Read the lines into an array and chomp the array: chomp(my @lines = <$fh>);. That is fine for small files. For large files, read line by line with a while loop and chomp each line, so the whole file isn’t held in memory.
What happens to chomp if I change $/?
chomp follows it. In paragraph mode, with $/ set to an empty string, chomp removes every trailing newline, not just one. Keep such changes inside a block with local $/, so the rest of the program still reads normal lines.
Related notes: removing the last character with chop(); reading a file with while or foreach; Perl strings and regex; hiring a remote Perl developer.
Need help with Perl code?
If you have Perl code that needs fixing, extending or upgrading, I’m happy to take a look. I work remotely from Kozhikode, Kerala, with teams across India and elsewhere; Perl development and maintenance explains how I help. If you are deciding how to bring someone in, Hiring a remote Perl developer is a short checklist.
You can reach me through the contact section or email me at manu@manu.co.in. A few lines about the system and what you need is a good start.
An earlier version of this note reproduced parts of a perlmeme.org tutorial on chomp(). This version is rewritten in my own words, with new examples.
Sources: perlfunc: chomp; perlvar: $/; perlmeme.org (link removed: the site currently fails over HTTPS).