Perl chop Function
last modified April 4, 2025
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.
#!/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.
#!/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.
#!/usr/bin/perl
use strict;
use warnings;
use v5.34.0;
my @words = ("apple", "banana", "cherry");
print "Before: @words\n";
my @removed = chop @words;
print "After: @words\n";
print "Removed chars: @removed\n";
When applied to an array, chop processes each element. It
returns an array of removed characters.
$ ./array.pl Before: apple banana cherry After: appl banan cherr Removed chars: e a y
Chop vs Chomp
chop differs from chomp in its unconditional
removal of the last character.
#!/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); print "'$copy1' (removed '$removed1')\n"; print "Using chomp:\n"; my $copy2 = $text1; my $removed2 = chomp($copy2); print "'$copy2' (removed $removed2 characters)\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.
$ ./compare.pl Using chop: 'Hello' (removed ' ') Using chomp: 'Hello' (removed 1 characters) Chop on string without newline: 'Hell' (removed 'o')
String Reversal with Chop
chop can help implement simple string reversal.
#!/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.
#!/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 and processes each character from the end of each line.
We use chomp first to handle the newline properly.
Chop in List Context
chop can be used creatively in list operations.
#!/usr/bin/perl
use strict;
use warnings;
use v5.34.0;
my @words = ("apple", "banana", "cherry");
my @last_chars = map { chop; $_ } @words;
print "Modified words: @words\n";
print "Last characters: @last_chars\n";
We use map with chop to collect last characters
while modifying the original array. The chop in void context
still modifies the value.
$ ./list.pl Modified words: appl banan cherr Last characters: e a y
Best Practices
- Understand the difference: Know when to use chop vs chomp.
- Check string length: Avoid chopping empty strings.
- Combine with chomp: Often need both for input processing.
- Document usage: Chop's unconditional nature can be surprising.
- Consider alternatives: substr may be clearer for some cases.
Source
This tutorial covered Perl's chop function with practical
examples demonstrating its usage in various scenarios.
Author
List all Perl tutorials.