< Perl Programming < Keywords
The
![](../../../I/Crystal_Clear_action_apply.png.webp)
The length
keyword
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
length EXPRESSION
Examples
![](../../../I/Crystal_Clear_action_apply.png.webp)
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.
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.