CLOSE

A possible solution for the signstrict.pl program is as follows:

  1. #!/usr/bin/perl
  2.  
  3. # signstrict.pl - revised sign checker which requires numeric input
  4.  
  5. use warnings;
  6. use strict;
  7.  
  8. my $number;
  9.  
  10. print "enter a number: ";
  11.  
  12. # Use chomp() to eliminate whitespace at beginning and end of string.
  13. # This will take off the newline
  14. chomp($number = <>);
  15.  
  16. # match 0 or 1 sign characters ( - or + )
  17. # match one of 4 patterns:
  18. # integer (one or more digits, no decimal point)
  19. # decimal number with digits before and after decimal point
  20. # decimal number which starts with a decimal point, followed by digits
  21. # decimal number which starts with digits, ending in a decimal point
  22. unless ($number =~ /^[-+]?(\d+|\d+\.\d+|\.\d+|\d+\.)$/)
  23. {
  24. die "input ($number) is not a number\n";
  25. }
  26.  
  27. if ($number < 0)
  28. {
  29. print "Your number is negative\n";
  30. }
  31. elsif ($number > 0)
  32. {
  33. print "Your number is positive\n";
  34. }
  35. else
  36. {
  37. print "Your number is zero\n";
  38. }
  39.  

CLOSE