This is my first time using Perl.

In Perl threads, the return value of the threads->exit() subroutine is undef value if the context is scalar.

#!/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"

The output is:

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


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

Why in print "$re\n\n"; the print is executed but not in print (undef) . "TES\n";? Even though the $re is undef.

and I made a test to ensure that $re is undefined of not.

#!/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"

and I go the same output.

有帮助吗?

解决方案

print (undef) . "TES\n";

is same as

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

so you're concatenating result of print with string.

What you want is

print ((undef) . "TES\n"); # or print undef() . "TES\n";
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top