Function length() in Perl

In the Perl programming language has a built-in function length(). This function returns the length of the string, which is passed argument to the function.

Here's an example:

▶ Run

print( length('Hello') );

This program will display the number 5. In line 'Hello' five characters.


my $length = length($str);

If the functions length() to pass a single argument, the function will operate on it.

If you do not pass any argument, the function will work with variable $_. Here is an example (the program displays the number 4):

▶ Run

$_ = 'asdf';

print( length() );

If the functions length() pass more than one argument, it will error:

Too many arguments for length at line 3, near "'a') "
Execution of aborted due to compilation errors.

Return value

Function length() returns either undefor a positive integer. The function returns undef only if it gave undef.

Here are three programs, each of them length() returns undef. They are all equivalent.

▶ Run

use Data::Dumper;

warn Dumper length(undef);
▶ Run

use Data::Dumper;

$_ = undef;

warn Dumper length();
▶ Run

use Data::Dumper;

warn Dumper length();


Function length() returns the number of characters in the string, not the number of bytes, but it is necessary that the row was with a flag that says that the string is Unicode. This flag automatically appears and strings are described in the code if the program there are use utf8;. Here is a program that returns the number 7 is the correct number characters in this string:

▶ Run

use utf8;

print( length('АБВГДЕЁ') );

But if the program no use utf8;, then the strings defined in the program considered as a sequence of bytes, and the function length() returns the number of bytes. Here is a program which uses the same string as in the previous example, but this program displays the number 14 (in line 7 letters, each letter is two bytes):

▶ Run

print( length('АБВГДЕЁ') );


Here is a program which shows some examples of function length():

▶ Run

use feature qw(say);

my $arr = [1, 2, 3];
my $hash = { a => 1 };
my $sub = sub { return 1 };

foreach my $var ('12', 12, 0xff, 0b1000, 'S', 'String', $arr, $hash, $sub ) {
    say $var . ' - ' . length($var);

The output from this program:

12 - 2
12 - 2
255 - 3
8 - 1
S - 1
String - 6
ARRAY(0x561a04e48578) - 21
HASH(0x561a04e60cf8) - 20
CODE(0x561a04e61370) - 20

Function length() returns the same value for line '12' and for the number 12 (number 2). Hexadecimal number 0xff is 255 in decimal notation and length() returns the number 3. Works the same and the number is written in binary notation.

Function length() cannot be used to count the number of elements in the array or hash. How to do this are described on the following pages:

For ARRAYREF and HASHREF function length() returns the number of characters in the strings 'ARRAY(0x561a04e48578)'.

Official documentation

Here is the output of the command perldoc -f length:

       length EXPR
       length  Returns the length in characters of the value of EXPR.  If EXPR
               is omitted, returns the length of $_.  If EXPR is undefined,
               returns "undef".

               This function cannot be used on an entire array or hash to find
               out how many elements these have.  For that, use "scalar
               @array" and "scalar keys %hash", respectively.

               Like all Perl character operations, length() normally deals in
               logical characters, not physical bytes.  For how many bytes a
               string encoded as UTF-8 would take up, use
               "length(Encode::encode_utf8(EXPR))" (you'll have to "use
               Encode" first).  See Encode and perlunicode.

Related topics

Other articles