22

在 Python 中,您可以使用 docstring 获得这样的多行字符串

foo = """line1
line2
line3"""

Perl 中有没有等价的东西?

4

5 回答 5

40

正常报价:

# Non-interpolative
my $f = 'line1
line2
line3
';

# Interpolative
my $g = "line1
line2
line3
";

Here-docs 允许您将任何标记定义为引用文本块的结尾:

# Non-interpolative
my $h = <<'END_TXT';
line1
line2
line3
END_TXT

# Interpolative
my $h = <<"END_TXT";
line1
line2
line3
END_TXT

正则表达式风格的引号运算符让您几乎可以使用任何字符作为分隔符——就像正则表达式允许您更改分隔符一样。

# Non-interpolative
my $i = q/line1
line2
line3
/;

# Interpolative
my $i = qq{line1
line2
line3
};

更新:更正了 here-doc 标记。

于 2010-05-20T19:37:14.633 回答
35

Perl 没有重要的语法垂直空格,所以你可以这样做

$foo = "line1
line2
line3
";

这相当于

$foo = "line1\nline2\nline3\n";
于 2010-05-20T18:45:49.393 回答
17

是的,这里的文档。

$heredoc = <<END;
Some multiline
text and stuff
END
于 2010-05-20T18:40:08.670 回答
0

是的,您有 2 个选项:

1.heredocs 请注意,heredocs 中的每个数据都是插值的:

我的 $ 数据 =<

你的数据

结尾

2.qq() 例如看:

打印qq(HTML

$你的文字

身体

HTML );

于 2010-05-21T14:58:55.013 回答
0

快速示例

#!/usr/bin/perl
use strict;
use warnings;

my $name = 'Foo';

my $message = <<'END_MESSAGE';
Dear $name,

this is a message I plan to send to you.

regards
  the Perl Maven
END_MESSAGE

print $message;

...结果:

Dear $name,

this is a message I plan to send to you.

regards
  the Perl Maven

参考: http: //perlmaven.com/here-documents

于 2016-07-04T15:06:39.683 回答