【问题标题】:perl bless with fat commaperl 用胖逗号祝福
【发布时间】:2014-07-29 04:39:27
【问题描述】:

我知道 perl bless 可以接受 1 或 2 个 arg,如 perlbless 中所述。但是,我不明白下面代码中的胖逗号是做什么的?和bless \$value,$class;一样吗?

# Construct the tie.
sub TIESCALAR { 
    my $class = shift;
    my $value = shift || 0;

    bless \$value => $class;
}

【问题讨论】:

    标签: perl comma difference bless


    【解决方案1】:

    胖逗号只是comma 的另一种写法。你可以看到这个:

    perl -MO=Deparse -e 'bless \$value => $class'
    bless \$value, $class;
    -e syntax OK
    

    here 也可以找到有趣的讨论。

    【讨论】:

    • 这并不完全正确,尽管在这种情况下它是正确的。与普通的不同,如果需要,胖逗号自动引用它的 LHS 裸字操作数。注意这两个调用的输出有多么不同:sub show_pair { my($this, $that) = @_; print "$this is $that.\n" } show_pair(time, time); show_pair(time => time); 产生 1402245177 is 1402245177. time is 1402245177. 还有更多奇特的情况。
    • @tchrist 感谢您的澄清和一个很好的例子。
    猜你喜欢
    • 1970-01-01
    • 2011-05-04
    • 2014-01-19
    • 2014-01-17
    • 2011-10-29
    • 2011-07-26
    • 2010-09-28
    • 2011-11-14
    • 2011-03-28
    相关资源
    最近更新 更多