Perl是否有Python的多行字符串的等价物?

bgibtngc  于 2023-02-05  发布在  Perl
关注(0)|答案(5)|浏览(190)

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

foo = """line1
line2
line3"""

在Perl中是否有类似的东西?

kuarbcqp

kuarbcqp1#

正常报价:

# 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标记。*
vfh0ocws

vfh0ocws2#

Perl在语法上没有明显的垂直空格,因此您可以

$foo = "line1
line2
line3
";

它相当于

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

kuuvgm7e3#

是的,这里的医生。

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

wb1gzix04#

简单示例

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

btqmn9zl

btqmn9zl5#

是的,您有2个选项:

  1. heredocs请注意,heredocs中的每个数据都是插值的:
my $data =<<END 

your data 

END

2.qq()参见示例:

print qq(
 HTML

 $your text

 BODY

 HTML
);

相关问题