This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: Réf. : Re: PATCH proposal for ext/Safe/safe2.t
[perl5.git] / lib / getopts.pl
index 852aae8..4a50b8f 100644 (file)
@@ -1,5 +1,13 @@
 ;# getopts.pl - a better getopt.pl
-
+#
+# This library is no longer being maintained, and is included for backward
+# compatibility with Perl 4 programs which may require it.
+#
+# In particular, this should not be used as an example of modern Perl
+# programming techniques.
+#
+# Suggested alternatives: Getopt::Long  or  Getopt::Std
+#
 ;# Usage:
 ;#      do Getopts('a:bc');  # -a takes arg. -b & -c not. Sets opt_* as a
 ;#                           #  side effect.
@@ -8,41 +16,50 @@ sub Getopts {
     local($argumentative) = @_;
     local(@args,$_,$first,$rest);
     local($errs) = 0;
+    local($[) = 0;
 
     @args = split( / */, $argumentative );
     while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
-       ($first,$rest) = ($1,$2);
-       $pos = index($argumentative,$first);
-       if($pos >= 0) {
-           if($pos < $#args && $args[$pos+1] eq ':') {
-               shift(@ARGV);
-               if($rest eq '') {
-                   ++$errs unless @ARGV;
-                   $rest = shift(@ARGV);
-               }
-               ${"opt_$first"} = $rest;
-           }
-           else {
-               ${"opt_$first"} = 1;
-               if($rest eq '') {
-                   shift(@ARGV);
+               ($first,$rest) = ($1,$2);
+               $pos = index($argumentative,$first);
+               if($pos >= $[) {
+                       if($args[$pos+1] eq ':') {
+                               shift(@ARGV);
+                               if($rest eq '') {
+                                       ++$errs unless(@ARGV);
+                                       $rest = shift(@ARGV);
+                               }
+                               eval "
+                               push(\@opt_$first, \$rest);
+                               if(\$opt_$first eq '') {
+                                       \$opt_$first = \$rest;
+                               }
+                               else {
+                                       \$opt_$first .= ' ' . \$rest;
+                               }
+                               ";
+                       }
+                       else {
+                               eval "\$opt_$first = 1";
+                               if($rest eq '') {
+                                       shift(@ARGV);
+                               }
+                               else {
+                                       $ARGV[0] = "-$rest";
+                               }
+                       }
                }
                else {
-                   $ARGV[0] = "-$rest";
+                       print STDERR "Unknown option: $first\n";
+                       ++$errs;
+                       if($rest ne '') {
+                               $ARGV[0] = "-$rest";
+                       }
+                       else {
+                               shift(@ARGV);
+                       }
                }
-           }
-       }
-       else {
-           print STDERR "Unknown option: $first\n";
-           ++$errs;
-           if($rest ne '') {
-               $ARGV[0] = "-$rest";
-           }
-           else {
-               shift(@ARGV);
-           }
        }
-    }
     $errs == 0;
 }