ZetCode

Perl length Function

last modified October 8, 2026 Perl 5.44

The Perl length function returns the number of characters in a string. It's a fundamental operation for string manipulation and validation.

length counts each character equally, including whitespace and special characters. For Unicode strings, it counts characters (code points), not bytes. For a defined value it returns an integer; for undef it returns undef.

Basic length Usage

The simplest way to use length is on a scalar variable.

basic.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my $text = "Hello there";
my $len = length($text);

print "The string '$text' has $len characters\n";

We demonstrate length calculating the size of a simple string. The function returns the count of all characters between the quotes.

$ ./basic.pl
The string 'Hello there' has 11 characters

Empty String Length

length returns 0 for empty strings and undef for undefined values.

empty.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my $empty = "";
my $undef_var;

print "Empty string length: ", length($empty), "\n";
print "Undef length: ", defined(length($undef_var)) ? length($undef_var) : "undef", "\n";

This script shows length behavior with empty and undefined values. Calling length on undef returns undef, not 0, so the script checks it with defined before printing.

$ ./empty.pl
Empty string length: 0
Undef length: undef

Whitespace Handling

length counts all whitespace characters including newlines.

whitespace.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my $text = "  Hello\n\tthere  ";
my $len = length($text);

print "String: '$text'\n";
print "Length: $len (includes spaces, tabs, newlines)\n";

The example demonstrates that spaces, tabs, and newlines all contribute to the length count. This is important for input validation.

$ ./whitespace.pl
String: '  Hello
	there  '
Length: 16 (includes spaces, tabs, newlines)

Multibyte Characters

For Unicode strings, length counts characters, not bytes.

unicode.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;
use utf8;
use bytes ();

binmode STDOUT, ':utf8';

my $text = "こんにちは";  # Japanese "hello"
my $len = length($text);

print "String: $text\n";
print "Character count: $len\n";
print "Byte length: ", bytes::length($text), "\n";

This shows length correctly counting Unicode characters. Each Japanese character is counted as one, despite using multiple bytes.

$ ./unicode.pl
String: こんにちは
Character count: 5
Byte length: 15

Array Context

length does not measure the elements of an array.

array.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my @words = ("apple", "banana", "cherry");
my $len = length($words[0]);

print "Array length: ", scalar(@words), "\n";
print "First element length: $len\n";

length expects a scalar. Passing an array such as @words evaluates it in scalar context, so length(@words) returns the length of the element count (1 here) and Perl warns about it. To get the size of the array, use scalar(@words); to measure an element, index it as $words[0].

$ ./array.pl
Array length: 3
First element length: 5

Input Validation

length is commonly used for validating user input length.

validation.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

print "Enter password (8-16 chars): ";
my $password = <STDIN>;
chomp $password;

my $len = length($password);

if ($len < 8) {
    print "Too short! ($len chars)\n";
} elsif ($len > 16) {
    print "Too long! ($len chars)\n";
} else {
    print "Valid password length: $len\n";
}

This script checks if input length falls within specified bounds. Note the chomp to exclude the newline from the count.

$ ./validation.pl
Enter password (8-16 chars): secret
Too short! (6 chars)

String Truncation

length helps when truncating strings to maximum lengths.

truncate.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my $text = "The quick brown fox jumps over the lazy dog";
my $max_len = 20;

if (length($text) > $max_len) {
    $text = substr($text, 0, $max_len) . "...";
}

print "Truncated: $text\n";
print "Length: ", length($text), "\n";

We use length to check if truncation is needed, then substr to shorten the string. The ellipsis adds to the length.

$ ./truncate.pl
Truncated: The quick brown fox ...
Length: 23

Best practices

These recommendations summarize how to use the length function safely and clearly.

Source

Perl length Documentation

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

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Perl tutorials.