ZetCode

Perl 字符串

最后修改于 2023 年 8 月 24 日

Perl 字符串教程展示了如何在 Perl 中处理字符串。 Perl 字符串 II 是本教程的第二部分。

Perl 字符串是字符序列。字符串可以单引号或双引号定义。区别在于双引号内的变量会被插值,特殊转义序列会被求值。

此外,Perl 还包含 qqq 操作符来定义字符串。

Perl 包含许多内置函数来处理字符串,例如 lengthuclcsubstr。此外,还有第三方模块用于处理字符串;例如 String::Util

Perl 被广泛认为是支持正则表达式领先的语言。使用正则表达式,我们可以执行高级文本操作。

Perl 字符串简单示例

单引号和双引号都可以用来创建字符串字面量。

simple.pl
#!/usr/bin/perl

use 5.30.0;
use warnings;

say 'falcon';
say "owl";

say "-----------------------";

say 'Perl language';
print "Python language\n";

say "-----------------------";

say 'a)\t\tChapter I';
say "a)\t\tChapter I";

用单引号定义的字符串不会求值转义序列。

say 'falcon';
say "owl";

在 Perl 中,单引号和双引号都可以用来定义字符串。

say 'a)\t\tChapter I';
say "a)\t\tChapter I";

\t 是制表符的转义序列;它不会在单引号 ('') 对内求值。

$ ./simple.pl
falcon
owl
-----------------------
Perl language
Python language
-----------------------
a)\t\tChapter I
a)		Chapter I

Perl 字符串使用引号

如果我们想显示引号,例如在直接引语中,该怎么办?在 Perl 中,这基本上有两种方法。

quotes.pl
#!/usr/bin/perl

use 5.30.0;
use warnings;

say "There are many stars";
say "He said, \"Which one is your favourite?\"";

say 'There are many stars';
say 'He said, "Which one is your favourite?"';

我们使用 (\) 字符来转义额外的引号。通常双引号字符用于分隔字符串字面量。然而,当转义时,它的原始含义被抑制。它显示为普通字符,可以在字符串字面量中使用。在引号内使用引号的第二种方法是混合使用单引号和双引号。

$ ./quotes.pl
There are many stars
He said, "Which one is your favourite?"
There are many stars
He said, "Which one is your favourite?"

Perl 字符串长度

字符串的大小由 length 函数确定。

string_size.pl
#!/usr/bin/perl

use 5.30.0;
use warnings;

my $msg = 'an old falcon';
say length $msg;

该示例打印字符串的大小。

$ ./string_size.pl 
13

Perl 字符串比较

在 Perl 中,字符串使用 eq 操作符进行比较。

comparison.pl
#!/usr/bin/perl

use 5.30.0;
use warnings;

my $w1 = 'falcon';
my $w2 = 'Falcon';

if ($w1 eq $w2) {

    say 'the strings are equal';
} else {

    say 'the strings are not equal';
}

该示例比较了两个字符串。

$ ./comparison.pl 
the strings are not equal

Perl 字符串重复

x 操作符重复给定的字符串。

comparison.pl
#!/usr/bin/perl

use 5.30.0;
use warnings;

say 'falcon ' x 4;

该示例将指定的字符串重复四次。

$ ./repeat.pl 
falcon falcon falcon falcon 

Perl 字符串特殊字符

在字符串中,某些字符有特殊用途。例如,$ 字符表示一个变量,该变量在双引号字符串中被求值。要使用这些字符的原意,我们使用 \ 字符来转义它们;

specials.pl
#!/usr/bin/perl

use 5.30.0;
use warnings;

say "The special character \\";
say 'The special character \'';
say "The special character \"";
say "The special character \$";

该示例打印了四个特殊字符。

$ ./specials.pl 
The special character \
The special character '
The special character "
The special character $

Perl 字符串转义序列

转义序列是特殊字符,在双引号字符串字面量中使用时具有特定含义。

\n 开始新的一行,\t 插入一个制表符,\r 返回到字符串的开头,\U 将后面的字符大写,\L 将后面的字符小写。

escapes.pl
#!/usr/bin/perl

use 5.30.0;
use warnings;

say "\tfalcon";
say "falcon\nfalcon\nfancon";
say "falcon\rhawk";
say "\Ufalcon";
say "\LSUN";

该示例演示了五个转义序列。

$ ./escapes.pl 
falcon
falcon
falcon
fancon
hawkon
FALCON
sun

Perl 连接字符串

Perl 使用 . 字符来连接字符串。

concat.pl
#!/usr/bin/perl

use 5.30.0;
use warnings;

say 'Perl ' . ' programming ' . 'language';

该示例使用点字符连接了三个字符串。

在 Perl 中还有其他连接字符串的方法。

concat2.pl
#!/usr/bin/perl

use 5.30.0;
use warnings;

my $w1 = 'an';
my $w2 = 'old';
my $w3 = 'hawk';

say $w1 . ' ' . $w2 . ' ' . $w3;
say "$w1 $w2 $w3";

say join ' ', $w1, $w2, $w3;

my $res = sprintf "%s %s %s", $w1, $w2, $w3;
say $res;

除了点字符,该示例还使用了 joinsprintf 函数来连接字符串。

say join ' ', $w1, $w2, $w3;

join 函数将三个字符串与指定的空格字符连接成一个字符串。

my $res = sprintf "%s %s %s", $w1, $w2, $w3;

sprintf 函数返回一个格式化的字符串。 %s 占位符会被给定变量的内容替换。

$ ./concat2.pl 
an old hawk
an old hawk
an old hawk
an old hawk

Perl q 和 qq 字符串操作符

qqq 是定义字符串的便捷操作符。

qops.pl
#!/usr/bin/perl

use 5.30.0;
use warnings;

my $s1 = q/\tThere is a \Ufalcon\E in the sky./;
my $s2 = qq/\tThere is a \Ufalcon\E in the sky./;

say $s1;
say $s2;

使用 q 我们创建单引号字符串;使用 qq 我们创建双引号字符串。

$ ./qops.pl 
\tThere is a \Ufalcon\E in the sky.
     There is a FALCON in the sky.

Perl qw 字符串操作符

qw 是创建单引号字符串列表的便捷操作符。

qw_oper.pl
#!/usr/bin/perl

use 5.30.0;
use warnings;

my @words = qw/sky cloud cup forest falcon/;

foreach (@words) {

    say $_;
}

该示例创建了一个单词列表;列表的元素在 foreach 循环中打印到控制台。

my @words = qw/sky cloud cup forest falcon/;

qw 是一个非常方便的操作符;我们不必指定引号字符和逗号。

$ ./qw_oper.pl 
sky
cloud
cup
forest
falcon

Perl 字符串插值

字符串插值是双引号字符串内的变量插值。

interpolation.pl
#!/usr/bin/perl

use 5.30.0;
use warnings;

my $name = 'Jane';
my $age = 17;

say "$name is $age years old.";

my @words = ('sky', 'blue', 'cup', 'road');
say "@words";

$" = '-';
say "@words";

在示例中,我们插值了两个标量和一个数组。

my $name = 'Jane';
my $age = 17;

say "$name is $age years old.";

在双引号字符串内部,$name$age 变量会被它们的值替换。

$" = '-';
say "@words";

$" 是一个特殊的列表分隔符变量;它设置了列表中元素的 the separator character。

$ ./interpolation.pl 
Jane is 17 years old.
sky blue cup road
sky-blue-cup-road

Perl 字符串 baby cart 操作符

"baby cart" 操作符 @{[]} 允许我们在字符串中求值表达式。

baby_cart.pl
#!/usr/bin/perl

use 5.30.0;
use warnings;
use Time::Piece;

say "The time is @{[localtime->hms]}";
say "2 + 2 = @{[2 + 2]}";

使用 @{[]} 操作符,我们在字符串中求值当前时间和简单的算术运算。

$ ./baby_cart.pl 
The time is 15:03:14
2 + 2 = 4

Perl 字符串 Qoute::Code

使用 Qoute::Code 模块,我们可以求值表达式。

quote_code.pl
#!/usr/bin/perl

use 5.30.0;
use warnings;
use Quote::Code;

my $n = 4;

say qc"$n = {$n}";
say qc"$n * $n = {$n * $n}";

该示例求值了一个简单的算术表达式。

$ ./quote_code.pl 
$n = 4
$n * $n = 16

Perl 字符串回文

回文是指一个单词、数字、短语或其他字符序列,正着读和反着读都一样,例如 madam 或 racecar。有很多方法可以检查一个字符串是否是回文。以下示例是可能的解决方案之一。

内置的 reverse 函数在标量上下文中反转字符串的字符。

palindrome.pl
#!/usr/bin/perl

use 5.30.0;
use warnings;

my @words = qw/sky racecar tool kayak arc madam level/;

foreach (@words) {

    if ($_ eq reverse $_) {

        say "$_ is a palindrome";
    } else {

        say "$_ is not a palindrome";
    }
}

如果反转后的字符串等于原始字符串,则它是一个回文。

$ ./palindrome.pl
sky is not a palindrome
racecar is a palindrome
tool is not a palindrome
kayak is a palindrome
arc is not a palindrome
madam is a palindrome
level is a palindrome

Perl 字符串 uc 和 lc 函数

uc 函数返回字符串的大写版本,而 lc 函数返回字符串的小写版本。

此外,我们还可以使用 \U\L 转义序列。

upper_lower.pl
#!/usr/bin/perl

use 5.30.0;
use warnings;

my $msg = "And old falcon";

say uc $msg;
say lc $msg;

say '------------------';

say "\U$msg";
say "\L$msg";

该示例将一个字符串转换为大写和小写字母。

$ ./upper_lower.pl 
AND OLD FALCON
and old falcon
------------------
AND OLD FALCON
and old falcon

在下一个示例中,我们修改非 ASCII 字母的大小写。

upper_lower2.pl
#!/usr/bin/perl

use 5.30.0;
use utf8;
use warnings;
use open qw( :std :encoding(UTF-8) );

my $word = "Čerešňa";

say uc $word;
say lc $word;

say '------------------';

say "\U$word";
say "\L$word";

该示例更改了斯洛伐克语单词 'Čerešňa' 的大小写。

use utf8;

use utf8 pragma 告诉 Perl 解析器允许在当前词法范围内使用 UTF-8 字符。

use open qw( :std :encoding(UTF-8) );

open pragma 更改标准文件句柄的编码。这对于正确输出是必要的。

$ ./upper_lower2.pl 
ČEREŠŇA
čerešňa
------------------
ČEREŠŇA
čerešňa

Perl 字符串表情符号

表情符号是使用字符的表情的图示表示。

emojis.pl
#!/usr/bin/perl

use 5.30.0;
use utf8;
use warnings;
use open qw( :std :encoding(UTF-8) );

my $text = "🐄🦙🐘🐫🐑🦝🦍🐯";
my @emojis = split '', $text;

foreach (@emojis) {

    say;
}

say length $text;

use bytes;
say length $text;

该示例分割了一个由表情符号组成的字符串,然后将每个表情符号单独打印到控制台。

say length $text;

我们使用 length 获取字符串的字符长度。

use bytes;
say length $text;

我们获取字符串的字节长度。

$ ./emojis.pl 
🐄
🦙
🐘
🐫
🐑
🦝
🦍
🐯
8
32

在本文中,我们处理了 Perl 中的字符串数据类型。

访问 Perl 字符串 II 或查看 所有 Perl 教程 列表。