Perl的here文档机制是从UNIX shell中的here文档机制派生而来的。

和在shell中一样,Perl中的here文档也是面向行的引用表单,要求提供<<运算符,其后跟随一个初始的终止字符串。

<<之后可以不出现空格。

如果终止字符串没有加上引号或双引号,则允许执行变量表达式。

如果终止字符串上加了单引号,则不执行变量表达式。用户应当把文本的第一行内容插入到第一个和最后一个终止字符串之间。

最后一个终止字符串必须位于该行上,且周围不允许出现空白字符。

与UNIX shell不同的是,Perl不允许在here文档中执行命令替换。

另一方面,如果将终止字符串包含在反引号中的话,Perl也允许在here文档中执行该命令。

#!/bin/perl $price=100;

1 print <<EOF; # No quotes around terminator EOF are same # as double quotes

2 The price of $price is right. # Variables are expanded

3 EOF

4 print <<'FINIS';

5 The price of $price is right. # The variable is not expanded # if terminator is enclosed in single quotes

6 FINIS

7 print << x 4; # Prints the line 4 times

8 Christmas is coming! # Blank line is necessary here as terminating string

9 print <<'END'; # If terminator is in backquotes, # will execute UNIX commands

10 echo hi there

11 echo -n "The time is "

12 date

13 END

(Output)

2 The price of 100 is right.

5 The price of $price is right.

8 Christmas is coming!

Christmas is coming!

Christmas is coming!

Christmas is coming!

10 hi there The time is Fri Nov 3 17:03:46 PST 2000

【责任编辑:云霞 TEL:(010)68476606】
回书目   上一节   下一

分类:

技术点:

相关文章: