CLOSE

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

  1. #!/usr/bin/perl
  2.  
  3. # pairs.pl - save a list of numeric pairs to a file
  4. #
  5. # Write to the file "squares.dat" a list. Each line
  6. # will contain a number (i) and the square of that number.
  7. # The starting and ending number will be specified as user
  8. # input.
  9.  
  10. use warnings;
  11. use strict;
  12.  
  13. my $out_filename = 'squares.dat';
  14.  
  15. my ($start, $end);
  16.  
  17. print "Enter starting value: ";
  18. $start = <>;
  19.  
  20. print "Enter ending value: ";
  21. $end = <>;
  22.  
  23. # check that $start <= $end
  24. unless ($start <= $end)
  25. {
  26. die "start value ($start) must come before end ($end)\n";
  27. }
  28.  
  29. # open $out_filename for writing
  30. open OUT, ">$out_filename"
  31. or die "open($out_filename) $!";
  32.  
  33. # make a loop ($i) from $start to $end
  34. # write $i and $i*$i to the file
  35. foreach my $i ($start..$end)
  36. {
  37. printf OUT "%3d %3d\n", $i, ($i * $i);
  38. }
  39.  
  40. # close the file
  41. close OUT;
  42.  

CLOSE