home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / perl / 7716 < prev    next >
Encoding:
Text File  |  1993-01-08  |  2.2 KB  |  67 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!gatech!darwin.sura.net!uvaarpa!mmdf
  3. From: Alan Stebbens <aks%anywhere@hub.ucsb.edu>
  4. Subject: getopts.pl bug fix
  5. Message-ID: <1993Jan8.062100.20495@uvaarpa.Virginia.EDU>
  6. Sender: mmdf@uvaarpa.Virginia.EDU (Mail System)
  7. Reply-To: aks%anywhere@hub.ucsb.edu
  8. Organization: The Internet
  9. Date: Fri, 8 Jan 1993 06:21:00 GMT
  10. Lines: 55
  11.  
  12. In debugging a new script, I've found a couple of bugs in "getopts.pl"
  13. (typically installed as /usr/local/lib/perl/getopts.pl).
  14.  
  15. First, if an option requires an argument and @ARGV is empty, its
  16. variable will get set to "shift @ARGV", which is "undef"; not a good
  17. thing to *set* a variable with.
  18.  
  19. Second, if you give two consecutive options, both of which want
  20. arguments, the second option is used as the value for the first.  Eg:
  21. '-x -y' results in $opt_x eq '-y', not the expected result of setting
  22. both $opt_x and $opt_y.
  23.  
  24. The diffs are followed by the code, for those without the original.  If
  25. this is old news to some of you, thanks for your forbearance.
  26.  
  27. -- Alan Stebbens, UCSB, Santa Barbara <aks@hub.ucsb.edu>
  28. "If all you have is a hammer, then everything looks like a nail."
  29.  
  30. ============================= cut here ===================================
  31. diff -c -d getopts.pl.~1~ getopts.pl
  32. *** getopts.pl.~1~    Sun Jan 26 00:23:12 1992
  33. --- getopts.pl    Thu Jan  7 21:56:31 1993
  34. ***************
  35. *** 3,8 ****
  36. --- 3,14 ----
  37.   ;# Usage:
  38.   ;#      do Getopts('a:bc');  # -a takes arg. -b & -c not. Sets opt_* as a
  39.   ;#                           #  side effect.
  40. + ;# Last Edited:
  41. + ;# 
  42. + ;# Thu Jan  7 21:56:31 1993 by Alan Stebbens (aks at anywhere.ucsb.edu)
  43. + ;#      Fixed assignment of $opt_N when @ARGV is empty.  Also, fixed
  44. + ;#      case of '-x -y' and '-x' wants an arg.
  45. + ;#
  46.   
  47.   sub Getopts {
  48.       local($argumentative) = @_;
  49. ***************
  50. *** 18,24 ****
  51.           if($args[$pos+1] eq ':') {
  52.           shift(@ARGV);
  53.           if($rest eq '') {
  54. !             $rest = shift(@ARGV);
  55.           }
  56.           eval "\$opt_$first = \$rest;";
  57.           }
  58. --- 24,30 ----
  59.           if($args[$pos+1] eq ':') {
  60.           shift(@ARGV);
  61.           if($rest eq '') {
  62. !             $rest = (@ARGV && $ARGV[0] !~ /^-/ && shift(@ARGV)) || 1;
  63.           }
  64.           eval "\$opt_$first = \$rest;";
  65.           }
  66. ============================= cut here ===================================
  67.