Perl 是否具有与 Python 的多行字符串等效的功能?

发布于 2024-09-02 11:13:01 字数 119 浏览 2 评论 0原文

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

foo = """line1
line2
line3"""

Perl 中是否有等效的东西?

In Python you can have a multiline string like this using a docstring

foo = """line1
line2
line3"""

Is there something equivalent in Perl?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月棠 2024-09-09 11:13:01

普通引号:

# 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
};

更新:更正了此处文档标记。

Normal quotes:

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

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

Here-docs allow you to define any token as the end of a block of quoted text:

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

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

Regex style quote operators let you use pretty much any character as the delimiter--in the same way a regex allows you to change delimiters.

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

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

UPDATE: Corrected the here-doc tokens.

卖梦商人 2024-09-09 11:13:01

Perl 没有重要的语法垂直空格,因此您可以执行

$foo = "line1
line2
line3
";

相当于

$foo = "line1\nline2\nline3\n";

Perl doesn't have significant syntactical vertical whitespace, so you can just do

$foo = "line1
line2
line3
";

which is equivalent to

$foo = "line1\nline2\nline3\n";
樱花细雨 2024-09-09 11:13:01

是的,这里有一个文档。

$heredoc = <<END;
Some multiline
text and stuff
END

Yes, a here-doc.

$heredoc = <<END;
Some multiline
text and stuff
END
云柯 2024-09-09 11:13:01

是的,你有 2 个选择:

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

my $data =<<END 

your data 

END

2.qq() 请参阅示例:

print qq(
 HTML

 $your text

 BODY

 HTML
);

Yes you have 2 options :

1.heredocs please note that every data in heredocs are interpolated :

my $data =<<END 

your data 

END

2.qq() see for example :

print qq(
 HTML

 $your text

 BODY

 HTML
);
绿萝 2024-09-09 11:13:01

简单示例

#!/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

Quick example

#!/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;

...Result:

Dear $name,

this is a message I plan to send to you.

regards
  the Perl Maven

Reference: http://perlmaven.com/here-documents

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文