Perl语言中的“此处”文档

   Perl 中提供类似于Unix Shell中的“此处文档”功能的操作符“<<”.
   具体使用的例子如下:
1:
   print <<" EOF"; #可以为任意字符串(包括空),也无需大写,为了便于查看最好采用大写字符。
   Hello World.
    EOF #注意该值要顶行书写,否则报错。 


2:
   print << x 10;   #打印行("Hello World.")10次
   Hello World.
   
   print << "" x 10;   #实现上面内容的比较好的方法,相当于使用"空"字符串进行标识
   The Camels are coming! Hurrah! Hurrah!


3:执行多条命令
   $a = << `EOC`;   # 执行多条命令
   echo hi there
   echo lo there
    EOC
   上述操作等价于 $a = qx/echo ni there;echo ni there/;
   
4: 堆叠使用
   print <<" dromedary", <<" camelid";
      I said bactrian.
dromedary

      She said llama.

camelid



5: 函数调用中使用
   &function(<<" THIS",23,<<' THAT');
   Here's a line
   ro two.
THIS
   And here's another.
THAT


sub function {
  my ($first,$second,$three) = @_;
  print "First = $first";
  print "Second = $second";
  print "Three = $three";
}


如果你的此处文档在你的其他代码里是缩进的,你就得手工从每行删除开头的空白: 
($quote = <<'QUOTE') =~ s/^\s+//gm; 
     The Road goes ever on and on, down from the door where it began.
QUOTE 


你甚至还可以用类似下面的方法用一个此处文档的行填充一个数组: 
@sauces = <<End_Lines =~ m/(\S.*\S)/g;
   normal tomato
   spicy tomato
   green chile
   pesto
   white wine

End_Lines


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值