当我尝试和时,我得到以下回溯prove factorial
:
Cannot detect source of 'factorial'! at /usr/share/perl/5.18/TAP/Parser/IteratorFactory.pm line 263.
TAP::Parser::IteratorFactory::detect_source('TAP::Parser::IteratorFactory=HASH(0x2856b08)', 'TAP::Parser::Source=HASH(0x2856bc8)') called at /usr/share/perl/5.18/TAP/Parser/IteratorFactory.pm line 213
TAP::Parser::IteratorFactory::make_iterator('TAP::Parser::IteratorFactory=HASH(0x2856b08)', 'TAP::Parser::Source=HASH(0x2856bc8)') called at /usr/share/perl/5.18/TAP/Parser.pm line 469
TAP::Parser::_initialize('TAP::Parser=HASH(0x28569e8)', 'HASH(0x23d8cb8)') called at /usr/share/perl/5.18/TAP/Object.pm line 58
TAP::Object::new('TAP::Parser', 'HASH(0x23d8cb8)') called at /usr/share/perl/5.18/TAP/Object.pm line 133
TAP::Object::_construct('TAP::Harness=HASH(0x2131db8)', 'TAP::Parser', 'HASH(0x23d8cb8)') called at /usr/share/perl/5.18/TAP/Harness.pm line 779
TAP::Harness::make_parser('TAP::Harness=HASH(0x2131db8)', 'TAP::Parser::Scheduler::Job=HASH(0x28235d0)') called at /usr/share/perl/5.18/TAP/Harness.pm line 578
TAP::Harness::_aggregate_single('TAP::Harness=HASH(0x2131db8)', 'TAP::Parser::Aggregator=HASH(0x2740ae0)', 'TAP::Parser::Scheduler=HASH(0x2823570)') called at /usr/share/perl/5.18/TAP/Harness.pm line 670
TAP::Harness::aggregate_tests('TAP::Harness=HASH(0x2131db8)', 'TAP::Parser::Aggregator=HASH(0x2740ae0)', 'factorial') called at /usr/share/perl/5.18/TAP/Harness.pm line 485
TAP::Harness::__ANON__() called at /usr/share/perl/5.18/TAP/Harness.pm line 498
TAP::Harness::runtests('TAP::Harness=HASH(0x2131db8)', 'factorial') called at /usr/share/perl/5.18/App/Prove.pm line 554
App::Prove::_runtests('App::Prove=HASH(0x2120170)', 'HASH(0x2606428)', 'TAP::Harness', 'factorial') called at /usr/share/perl/5.18/App/Prove.pm line 512
App::Prove::run('App::Prove=HASH(0x2120170)') called at /usr/bin/prove line 11
这是我的factorial.pm
:
package factorial;
use Exporter qw(import);
BEGIN {
our @EXPORT_OK = qw(factorial);
}
sub factorial
{
my ($parm) = @_;
my $factorial = 1;
if ($parm == 0)
{
return $factorial;
}
foreach my $i (1..$parm) {
$factorial *= $i;
}
return $factorial;
}
1;
我的factorial.t
:
#!/usr/bin/perl -w
use Test::More;
use factorial 'factorial';
is(factorial(0), 1, "boundary case");
is(factorial(1), 1, "factorial(1)");
is(factorial(2), 2, "factorial(2)");
is(factorial(6), 720, "factorial(6)");
done_testing;
我通过运行这个程序验证了我的factorial
模块工作正常(或者,至少,正确地做一件事)。
#!/usr/bin/perl -w
use factorial 'factorial';
my $parm = 5;
print "Factorial($parm) = ", factorial($parm), "\n";