=head1 NAME
facts.pl - return the factorial of a number.
=head1 SYNOPSIS
perl facts.p
=head1 DESCRIPTION
Sample perl program with added features to show the steps
of the Perl course for Perl for Bioinformatics I.
=head1 TODO
I often use this style of documentation to document the project as I go.
o - to do
. - in progress
t - in test
x - done
x create documentation
x create a text factorial subroutine
x create a test of the factorial subroutine
o add a simple sub fact( ) to return the factorial number
o add a test of the simple sub fact( )
=head1 NOTES
Note that values over 52 will go to infinity.
=head1 BUGS
Note that any entry besides "-t" on the command line will try to be opened as a file.
=head1 AUTHOR
Modifications by David Scott from Perl for Bioinformatics I class.
=head1 FUNCTIONS
Perl Functions follow ...
=over 4
=cut
# Libraries to use
use SelfLoader;
# quick test of factorial boundaries
test() if grep(/-t/, @ARGV);
# Factorial Sub Routine with test illustrated
print "Enter the number of factorials to calculate> ";
my $input = <>;
chomp($input);
print factorial($input);
# Successful exit, tell the Operating System the exit is okay
exit(0);
=item my $text = factorial($number);
Return the text of calculating a factorial number.
=cut
sub factorial {
my ($number) = @_;
use constant { LOWER_LIMIT => 1 };
my $previous = 1;
my $result = "";
while ( $number > LOWER_LIMIT ) {
$result .= "$number * $previous\n";
$previous = $number * $previous;
$number--;
}
if( $number == 0 ) {
$previous = 1;
}
$result .= "The result is $previous\n";
return $result;
}
=back
=cut
__END__
sub test {
use Test::More tests => 4;
my $expected = "The result is 1\n";
my $result = factorial(-1);
is($result, $expected, "Test of factorial(-1)");
$result = factorial(0);
is($result, $expected, "Test of factorial(0)");
$result = factorial(1);
is($result, $expected, "Test of factorial(1)");
$result = factorial(2);
$expected =~ s/1$/2/;
$expected =~ s/^/2 * 1\n/;
is($result, $expected, "Test of factorial(2)");
# successful exit of test
exit(0);
}