私はちょうど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よく書かれた質問 –