List,

I have a factorial script that calls a sub fact that does the
factorial and returns a value.  But I get this error when I execute
the script:

Use of uninitialized value in numeric gt (>) at fact.pl line 22.

Here is the script:

#!/usr/bin/perl

use warnings;
use strict;

fact();
my $num;

print "enter a number: \n";
chomp($num = <STDIN>);

my $x = fact($num);
print "the factorial is: $x\n";

sub fact {
        my $num = 0;
        $num = shift;
        my $res = 1;
        while ($num > 1) {
                $res *= $num;
                $num--;
        }
        return $res;
}


Here is an example of the output:

[ora...@localhost perlscripts]$ perl fact.pl
Use of uninitialized value in numeric gt (>) at fact.pl line 22.
enter a number:
4
the factorial is: 24
[ora...@localhost perlscripts]$ perl fact.pl
Use of uninitialized value in numeric gt (>) at fact.pl line 22.
enter a number:
3
the factorial is: 6
[ora...@localhost perlscripts]$ perl fact.pl
Use of uninitialized value in numeric gt (>) at fact.pl line 22.
enter a number:
2
the factorial is: 2
[ora...@localhost perlscripts]$ perl fact.pl
Use of uninitialized value in numeric gt (>) at fact.pl line 22.
enter a number:
1
the factorial is: 1
[ora...@localhost perlscripts]$ perl fact.pl
Use of uninitialized value in numeric gt (>) at fact.pl line 22.
enter a number:
0
the factorial is: 1
[ora...@localhost perlscripts]$ perl fact.pl
Use of uninitialized value in numeric gt (>) at fact.pl line 22.
enter a number:
7
the factorial is: 5040
[ora...@localhost perlscripts]$ perl fact.pl
Use of uninitialized value in numeric gt (>) at fact.pl line 22.
enter a number:
8
the factorial is: 40320
[ora...@localhost perlscripts]$ perl fact.pl
Use of uninitialized value in numeric gt (>) at fact.pl line 22.
enter a number:
9
the factorial is: 362880


You can see it works, but returns that error.  Why does it do this?

thanks.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to