Perl Programming/Keywords/length

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Previous: le Keywords Next: link

The length keyword[edit | edit source]

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 | edit source]


Examples[edit | edit source]

  use 5.10.0;
  use Encode;

  my $name = "Perl…";

  say $name;
  say length $name;
  say length(encode_utf8($name));

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

Previous: le Keywords Next: link