hurriedly% 2015-09-19 07:35 采纳率: 100%
浏览 218
已采纳

在 perl 中 m'~ m 语法是什么意思?

So I understand that perl has much unusual syntax, but I came across a code snippet at work that other day that has left me confused. Could someone please explain to me what it means:

<<'m'=~m>>
print $a unless $b;
return;
m
;

It looks like HEREDOC syntax, but not in any form I've ever seen.

转载于:https://stackoverflow.com/questions/32665616/what-does-the-m-m-syntax-mean-in-perl

  • 写回答

2条回答 默认 最新

  • Lotus@ 2015-09-19 07:44
    关注

    This secret syntax is known as the ornate double-bladed sword, and is typically denoted as:

    <<m=~m>>
    
    Comments here
    
    m
    ;
    

    It is a hack for multi-line comments, making use of the heredoc syntax and match operator (=~) with > as delimiter.

    Note, as slashes, /, aren't being used as the delimiter for matching, the 'm' operator is required. For example $my_var =~ /test/ is equivalent to $my_var =~ m>test>, whereas $my_var =~ >test> would be invalid.

    Pay attention to the fact that the first m character in your snippet is enclosed by single quotes, meaning the $a and $b variables won't be interpolated. Had these quotes been omitted (as per my provided code example) perl would automatically add double quotes to the end marker and any subsequent variables would be interpolated. This would cause problems if $a and $b are no longer defined and you're using use warnings;.

    To make the ornate double-blade example above easier to understand (but still not recommended), we could write:

    <<"END" =~ //
    
    Comments here
    
    END
    ;
    

    Note that the =~ // is redundant, so this is equivalent to:

    <<"END";
    
    Comments here
    
    END
    

    Which is just standard heredoc syntax in void context.

    I would not recommend using this in production code as having a string in void context can cause problems. Plus this code is a head scratcher for even the most experienced perl programmers and is pointlessly difficult to read and maintain! If you want to create proper multi-line comments then I would suggest sticking to pod as explained here How do I enter a multi-line comment in Perl?

    Hope this clears it up.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口