home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!gatech!darwin.sura.net!uvaarpa!mmdf
- From: Alan Stebbens <aks%anywhere@hub.ucsb.edu>
- Subject: getopts.pl bug fix
- Message-ID: <1993Jan8.062100.20495@uvaarpa.Virginia.EDU>
- Sender: mmdf@uvaarpa.Virginia.EDU (Mail System)
- Reply-To: aks%anywhere@hub.ucsb.edu
- Organization: The Internet
- Date: Fri, 8 Jan 1993 06:21:00 GMT
- Lines: 55
-
- In debugging a new script, I've found a couple of bugs in "getopts.pl"
- (typically installed as /usr/local/lib/perl/getopts.pl).
-
- First, if an option requires an argument and @ARGV is empty, its
- variable will get set to "shift @ARGV", which is "undef"; not a good
- thing to *set* a variable with.
-
- Second, if you give two consecutive options, both of which want
- arguments, the second option is used as the value for the first. Eg:
- '-x -y' results in $opt_x eq '-y', not the expected result of setting
- both $opt_x and $opt_y.
-
- The diffs are followed by the code, for those without the original. If
- this is old news to some of you, thanks for your forbearance.
-
- -- Alan Stebbens, UCSB, Santa Barbara <aks@hub.ucsb.edu>
- "If all you have is a hammer, then everything looks like a nail."
-
- ============================= cut here ===================================
- diff -c -d getopts.pl.~1~ getopts.pl
- *** getopts.pl.~1~ Sun Jan 26 00:23:12 1992
- --- getopts.pl Thu Jan 7 21:56:31 1993
- ***************
- *** 3,8 ****
- --- 3,14 ----
- ;# Usage:
- ;# do Getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a
- ;# # side effect.
- + ;# Last Edited:
- + ;#
- + ;# Thu Jan 7 21:56:31 1993 by Alan Stebbens (aks at anywhere.ucsb.edu)
- + ;# Fixed assignment of $opt_N when @ARGV is empty. Also, fixed
- + ;# case of '-x -y' and '-x' wants an arg.
- + ;#
-
- sub Getopts {
- local($argumentative) = @_;
- ***************
- *** 18,24 ****
- if($args[$pos+1] eq ':') {
- shift(@ARGV);
- if($rest eq '') {
- ! $rest = shift(@ARGV);
- }
- eval "\$opt_$first = \$rest;";
- }
- --- 24,30 ----
- if($args[$pos+1] eq ':') {
- shift(@ARGV);
- if($rest eq '') {
- ! $rest = (@ARGV && $ARGV[0] !~ /^-/ && shift(@ARGV)) || 1;
- }
- eval "\$opt_$first = \$rest;";
- }
- ============================= cut here ===================================
-