ZetCode

Perl chop Function

last modified October 8, 2026

The Perl chop function removes the last character from a string. It modifies the original string and returns the character that was removed.

Unlike chomp, chop removes any last character unconditionally. It's useful when you need to process strings character by character.

Basic chop Usage

The simplest way to use chop is on a single variable.

basic.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $text = "Hello";
print "Before: '$text'\n";

my $removed = chop($text);
print "After: '$text'\n";
print "Removed character: '$removed'\n";

We demonstrate chop removing the last character from a string. The function modifies the original variable and returns the removed character.

$ ./basic.pl
Before: 'Hello'
After: 'Hell'
Removed character: 'o'

Processing User Input

chop can be used to process user input character by character.

input.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

print "Enter a word: ";
my $word = <STDIN>;
chomp $word;  # First remove newline

while (length $word > 0) {
    my $char = chop $word;
    print "Removed: '$char', Remaining: '$word'\n";
}

This script reads user input and processes it character by character from the end. Note we use chomp first to remove the newline.

$ ./input.pl
Enter a word: Perl
Removed: 'l', Remaining: 'Per'
Removed: 'r', Remaining: 'Pe'
Removed: 'e', Remaining: 'P'
Removed: 'P', Remaining: ''

Chop on Arrays

chop can process entire arrays, modifying each element.

array.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my @words = ("apple", "banana", "cherry");
print "Before: @words\n";

my $last = chop @words;
print "After: @words\n";
print "Returned character: '$last'\n";

When applied to an array, chop removes the last character of each element. However, it returns only one value: the character removed from the last element. To get the character removed from every element, see the map example below.

$ ./array.pl
Before: apple banana cherry
After: appl banan cherr
Returned character: 'y'

Chop vs Chomp

chop differs from chomp in its unconditional removal of the last character.

compare.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $text1 = "Hello\n";
my $text2 = "Hello";

print "Using chop:\n";
my $copy1 = $text1;
my $removed1 = chop($copy1);
(my $shown1 = $removed1) =~ s/\n/\\n/;
print "'$copy1' (removed '$shown1')\n";

print "Using chomp:\n";
my $copy2 = $text1;
my $removed2 = chomp($copy2);
print "'$copy2' (removed $removed2 character(s))\n";

print "Chop on string without newline:\n";
my $copy3 = $text2;
my $removed3 = chop($copy3);
print "'$copy3' (removed '$removed3')\n";

chop always removes the last character, while chomp only removes the input record separator if present. The newline is shown as \n in the output so that it is visible.

$ ./compare.pl
Using chop:
'Hello' (removed '\n')
Using chomp:
'Hello' (removed 1 character(s))
Chop on string without newline:
'Hell' (removed 'o')

String Reversal with Chop

chop can help implement simple string reversal.

reverse.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $text = "Perl";
my $reversed = '';

while (length $text > 0) {
    $reversed .= chop $text;
}

print "Reversed: $reversed\n";

This script builds a reversed string by repeatedly chopping the original. Note Perl has better ways to reverse strings, but this demonstrates chop.

$ ./reverse.pl
Reversed: lreP

Processing File Content

chop can be used when processing file content character by character.

file.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

open(my $fh, '<', 'data.txt') or die "Can't open file: $!";

while (my $line = <$fh>) {
    chomp $line;  # Remove newline first
    while (length $line > 0) {
        my $char = chop $line;
        print "Processing: '$char'\n";
    }
    print "--- End of line ---\n";
}

close($fh);

This script reads a file named data.txt and processes each character from the end of each line. We use chomp first to remove the newline. If data.txt contains the two lines ab and cd, the output is:

$ ./file.pl
Processing: 'b'
Processing: 'a'
--- End of line ---
Processing: 'd'
Processing: 'c'
--- End of line ---

Chop in List Context

chop can be used creatively in list operations.

list.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my @words = ("apple", "banana", "cherry");
my @removed = map { chop } @words;

print "Modified words: @words\n";
print "Removed characters: @removed\n";

We use map with chop to collect the character removed from each element. The $_ variable is aliased to each element, so chop modifies the original array as well.

$ ./list.pl
Modified words: appl banan cherr
Removed characters: e a y

Best Practices

Source

Perl chop Documentation

This tutorial covered Perl's chop function with practical examples demonstrating its usage in various scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Perl tutorials.