Perl 中的“Here”文档


你可以非常方便地存储或打印多行文本。甚至可以在“here”文档内使用变量。下面是一个简单的语法,仔细检查,<<和标识符之间一定不能有空格。

标识符既可以是裸词,也可以是引号包围的文本,如下面的 EOF。如果标识符带有引号,则使用的引号类型将决定文档中的文本处理方式,就像在常规引号中一样。未加引号的标识符的作用类似于双引号。

示例

 实用演示

#!/usr/bin/perl
$a = 10;
$var = <<"EOF";
This is the syntax for here document and it will continue
until it encounters a EOF in the first line.
This is case of double quote so variable value will be
interpolated. For example value of a = $a
EOF
print "$var\n";
$var = <<'EOF';
This is case of single quote so variable value will be
interpolated. For example value of a = $a
EOF
print "$var\n";

输出

将产生以下结果 −

This is the syntax for here document and it will continue
until it encounters a EOF in the first line.
This is case of double quote so variable value will be
interpolated. For example value of a = 10

This is case of single quote so variable value will be
interpolated. For example value of a = $a

更新于: 28-Nov-2019

1K+ 浏览

开启你的 职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.