【问题标题】:What's the point of blessed in Perl?Perl 中的祝福有什么意义?
【发布时间】:2011-10-29 10:47:30
【问题描述】:
[root@ ~]$ perl -e "print 1 if blessed $a;"
1
[root@ ~]$ perl -e "print 1 if blessed $c;"
1
[root@ ~]$ perl -e "print 1 if blessed $cee;"
1

好像一直是true,版本是5.8.8

更新

我不是以root 运行的,为了隐私,我更改了它:)

【问题讨论】:

标签: perl


【解决方案1】:

你的意思是blessed 来自Scalar::Util 吗?你可能想先加载函数:

perl -MScalar::Util=blessed -e "print 1 if blessed $a;"

否则您的blessed 只是裸字(字符串),这显然是正确的。

【讨论】:

  • blessed $a; 怎么会被解释为裸词?
  • Bareword 不作为字符串,因为print test 不输出test
  • @asker - print test 不是很好的例子,因为它被解析为 print test $_(使用 test 作为文件句柄)。试试print STDOUT test。但无论如何,在你的情况下,blessed 不是裸词,因为周围的上下文(当没有定义祝福时,它被解释为$a->blessed
  • @asker,shell 为$a 插入一个空字符串,所以代码就是print 1 if blessed ,这与关闭严格时的print 1 if "blessed" 相同。
  • @bvr,它不会被解释为$a->blessed。这将导致运行时错误。请参阅我给提问者的消息。
【解决方案2】:

正如已经指出的,您需要在使用该方法之前加载模块。另外,如果您使用perl -we 而不是perl -e,您可能不会问这个问题。

对于我来说,使用perl -we,我收到以下警告:

Can't call method "blessed" without a package or object reference at -e line 1.

【讨论】:

    【解决方案3】:

    blessed 在 Perl 中不是关键字。您在 shell 命令中使用了双引号,因此变量($a$c 等)来自您的 shell 环境,它们不是 Perl 变量。 由于这些环境变量可能为空,因此您实际上是在执行脚本

    print 1 if blessed ;
    

    当这样使用时,blessed 只是一个 bareword 字符串,并且总是计算为真。你所做的和跑步没有太大区别

    $ perl -e 'print 1 if foo'
    

    【讨论】:

    • 糟糕,我没想到。由于引号,我认为 OP 在 Windows 上。好点子。
    • 或者更清楚一点,就像运行perl -e "print 1 if 'blessed'"
    猜你喜欢
    • 1970-01-01
    • 2010-09-28
    • 2014-01-19
    • 2014-07-29
    • 2011-05-04
    • 2014-01-17
    • 2011-07-26
    • 2015-11-25
    • 2011-11-14
    相关资源
    最近更新 更多