【问题标题】:undef value and printing itundef 值并打印它
【发布时间】:2013-10-02 18:04:35
【问题描述】:

这是我第一次使用 Perl。

在 Perl 线程中,如果上下文是标量,threads->exit() 子例程的返回值是 undef 值。

#!/usr/bin/perl
use threads;

$t=threads->create({"context"=>"scalar"},
                    sub { threads->exit();});
$re = $t->join();
print "##################\n";
print "$re\n\n";
print "##################\n";
print (undef) . "TES\n";
print "##################\n"

输出是:

##################


##################
##################

为什么在print "$re\n\n"; 中执行打印,但在print (undef) . "TES\n"; 中没有执行? 即使$reundef

我做了一个测试以确保 $re 是未定义的。

#!/usr/bin/perl
use threads;

$t=threads->create({"context"=>"scalar"},
                    sub { threads->exit();});
$re = $t->join();
print "##################\n";
print "$re\n\n" if ! defined $re;
print "##################\n";
print (undef) . "TES\n";
print "##################\n"

然后我得到相同的输出。

【问题讨论】:

    标签: perl undef


    【解决方案1】:
    print (undef) . "TES\n";
    

    相同
    (print (undef)) . "TES\n";
    

    所以你将print 的结果与字符串连接起来。

    你想要的是

    print ((undef) . "TES\n"); # or print undef() . "TES\n";
    

    【讨论】:

    • 或者只是print undef . "TES\n"; :)
    • @friedo Warning: Use of "undef" without parentheses is ambiguous..
    • @mpapec,使用undef()静音
    • @friedo, print undef() . "TES\n"; 会避免警告。
    • @ikegami 似乎两个例子都在使用括号
    猜你喜欢
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    相关资源
    最近更新 更多