Perl Programming/Keywords/length

Previous: le Keywords Next: link

The length keyword

edit

length is a function that returns how long the following expression is in terms of characters. If no EXPRESSION is given, it returns the length of $_. If the expression is undefined, undef is returned.

As length returns the number of logical characters, to find out the number of bytes in UTF-8, one has to enter an expression (see below).

Syntax

edit
  length EXPRESSION

Examples

edit
 
  use 5.10.0;
  use Encode;

  my $name = "Perl…";

  say $name;
  say length $name;
  say length(encode_utf8($name));
Perl…
7
10

The result is the string $name, and the length of it both as logical characters & as UTF-8.

Previous: le Keywords Next: link