Perl Scripts ·

Removing the Last Character in Perl with chop()

By Manu Mathew, Technical Lead Engineer · Published 5 December 2013 · Last updated 5 October 2026

Isometric illustration of a row of tiles with the last tile lifted off in blue

Trailing commas, stray separators and fixed-width codes all leave you needing to remove the last character in Perl. The built-in chop() does exactly that; this note shows when it is the right tool and when it isn’t.

Quick answer

chop $string removes the last character, whatever it is, and returns that character. If you only want to remove a trailing newline, use chomp() instead, because chop doesn’t check what it removes. To remove one specific trailing character, such as a comma, use a substitution like s/,\z// so nothing else is lost.

How do I use chop()?

use strict;
use warnings;

my $word = 'frogs';
my $last = chop $word;        # returns the removed character
print "$word $last\n";        # frog s

my $csv = 'a,b,c,';
chop $csv;                    # drop a trailing comma
print "$csv\n";               # a,b,c

What can go wrong?

chop always removes something, so running it on a string that has no newline loses a real character:

my $no_newline = "abc";
chop $no_newline;             # chop does not check what it removes
print "$no_newline\n";        # ab

When you only want to remove a specific trailing character, a substitution is safer: $csv =~ s/,\z//; removes the comma only if it is there.

Is there an alternative to chop()?

substr as an lvalue does the same job and makes the intent obvious to readers:

my $text = "abc";
substr($text, -1) = '';       # same effect as chop, without the return value
print "$text\n";              # ab

Frequently asked questions

Does chop work on arrays?

Yes. chop @lines removes the last character from every element and returns the last character it removed. For lines read from a file that is rarely what you want; chomp @lines removes only the newlines and leaves the text alone.

How do I remove the last few characters of a string?

Use substr with a negative length: substr($name, 0, -4) returns “invoice” from “invoice.txt”. On a string shorter than the amount you remove, it returns an empty string rather than failing, so check the length first if that matters.

What does chop do to an empty or undefined value?

On an empty string, chop returns an empty string and leaves the variable empty, without a warning. On an undefined variable, use warnings reports an uninitialised value, so check defined first when the value may be missing.

Is chop still used in modern Perl?

Rarely. Code that once used chop to remove a newline now uses chomp, and code that removes a known ending uses a substitution such as s/,\z//. chop still fits when you really do want to drop the last character, whatever it is.

Related notes: removing newlines with chomp(); Perl strings and regex; checking whether a Perl variable is a number; 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 chop(). This version is rewritten in my own words, with new examples.

Sources: perlfunc: chop; perlfunc: substr; perlmeme.org (link removed: the site currently fails over HTTPS).

Back to the blog