开发者

Does Perl have the equivalent of Python's multiline strings?

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

foo = """line1
line2
line3"""

Is ther开发者_StackOverflowe something equivalent in Perl?


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.


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";


Yes, a here-doc.

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


Yes you have 2 options :

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

my $data =<

your data

END

2.qq() see for example :

print qq( HTML

$your text

BODY

HTML );


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜