Perl length 函数
最后修改于 2025 年 4 月 4 日
Perl 的 length
函数返回字符串中的字符数。它是字符串操作和验证的基本操作。
length
对每个字符平等计数,包括空格和特殊字符。对于 Unicode 字符串,它计算逻辑字符,而不是字节。该函数始终返回一个整数值。
基本的 length 用法
使用 length
最简单的方法是作用于标量变量。
basic.pl
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $text = "Hello World"; my $len = length($text); print "The string '$text' has $len characters\n";
我们演示了 length
计算简单字符串的大小。该函数返回引号之间所有字符的计数。
$ ./basic.pl The string 'Hello World' has 11 characters
空字符串长度
length
对空字符串和 undef 值返回 0。
empty.pl
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $empty = ""; my $undef_var; print "Empty string length: ", length($empty), "\n"; print "Undef length: ", length($undef_var), "\n";
此脚本显示了 length
在空值和未定义值下的行为。Perl 在字符串上下文中将 undef 视为空字符串。
$ ./empty.pl Empty string length: 0 Undef length: 0
空格处理
length
计算所有空格字符,包括换行符。
whitespace.pl
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $text = " Hello\n\tWorld "; my $len = length($text); print "String: '$text'\n"; print "Length: $len (includes spaces, tabs, newlines)\n";
该示例演示了空格、制表符和换行符都计入长度。这对于输入验证很重要。
$ ./whitespace.pl String: ' Hello World ' Length: 14 (includes spaces, tabs, newlines)
多字节字符
对于 Unicode 字符串,length
计算字符,而不是字节。
unicode.pl
#!/usr/bin/perl use strict; use warnings; use v5.34.0; use utf8; my $text = "こんにちは"; # Japanese "hello" my $len = length($text); print "String: $text\n"; print "Character count: $len\n"; print "Byte length: ", length($text.bytes), "\n";
这表明 length
正确地计算了 Unicode 字符。每个日文字符都算作一个,尽管它们使用了多个字节。
$ ./unicode.pl String: こんにちは Character count: 5 Byte length: 15
数组上下文
length
在数组上下文中返回第一个元素。的长度
array.pl
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my @words = ("apple", "banana", "cherry"); my $len = length(@words); print "Array length: ", scalar(@words), "\n"; print "First element length: $len\n";
当与数组一起使用时,length
只检查第一个元素。要获取数组大小,请使用 scalar
或数组的标量上下文。
$ ./array.pl Array length: 3 First element length: 5
输入验证
length
通常用于验证用户输入的长度。
validation.pl
#!/usr/bin/perl use strict; 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"; }
此脚本检查输入长度是否在指定的范围内。请注意 chomp
用于从计数中排除换行符。
$ ./validation.pl Enter password (8-16 chars): secret Too short! (6 chars)
字符串截断
length
在将字符串截断到最大长度时很有帮助。
truncate.pl
#!/usr/bin/perl use strict; 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";
我们使用 length
来检查是否需要截断,然后使用 substr
来缩短字符串。省略号会增加长度。
$ ./truncate.pl Truncated: The quick brown fox... Length: 23
最佳实践
- 始终先 chomp: 在长度检查之前删除换行符。
- 使用 Unicode::GCString: 用于 Unicode 中的字素簇。
- 尽早验证: 收到输入后立即检查长度。
- 考虑 bytes::length: 当需要字节计数时。
来源
本教程通过实际示例介绍了 Perl 的 length
函数,演示了它在常见场景中的用法。
作者
列出 所有 Perl 教程。