我剛剛花了一大堆時間來調試我追溯到wantarray()
的問題。我已經完成了這個測試案例。 (忽略$!
在這種情況下不會有任何有用的信息)。我想知道的是爲什麼wantarray
不認爲它被稱爲在列表環境在第二個例子:爲什麼在調用foo()||時,wantarray會在標量上下文中返回。死?
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
{
my ($one, $two) = foo();
is($one, 'a', 'just foo');
is($two, 'b', 'just foo');
}
{
my ($one, $two) = foo() || die $!;
is($one, 'a', '|| die');
is($two, 'b', '|| die');
}
done_testing();
sub foo {
return wantarray ? ('a', 'b') : 'bar';
}
這個測試的輸出是:
$ prove -v wantarray.pl
wantarray.pl ..
ok 1 - just foo
ok 2 - just foo
not ok 3 - || die
not ok 4 - || die
1..4
# Failed test '|| die'
# at wantarray.pl line 15.
# got: 'bar'
# expected: 'a'
# Failed test '|| die'
# at wantarray.pl line 16.
# got: undef
# expected: 'b'
# Looks like you failed 2 tests of 4.
Dubious, test returned 2 (wstat 512, 0x200)
Failed 2/4 subtests
Test Summary Report
-------------------
wantarray.pl (Wstat: 512 Tests: 4 Failed: 2)
Failed tests: 3-4
Non-zero exit status: 2
Files=1, Tests=4, 0 wallclock secs (0.03 usr 0.01 sys + 0.02 cusr 0.00 csys = 0.06 CPU)
Result: FAIL
+1寫得好的問題 –