which take an argument don't care whether there is a space between the
switch and the argument.
+Note that, if your code is running under the recommended C<use strict
+'vars'> pragma, it may be helpful to declare these package variables
+via C<use vars> perhaps something like this:
+
+ use vars qw/ $opt_foo $opt_bar /;
+
For those of you who don't like additional variables being created, getopt()
and getopts() will also accept a hash reference as an optional second argument.
Hash keys will be x (where x is the switch name) with key values the value of
@ISA = qw(Exporter);
@EXPORT = qw(getopt getopts);
-
-# $RCSfile: getopt.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:23:58 $
+$VERSION = $VERSION = '1.01';
# Process single-character switches with switch clustering. Pass one argument
# which is a string containing all switches that take an argument. For each
sub getopt ($;$) {
local($argumentative, $hash) = @_;
local($_,$first,$rest);
- local $Exporter::ExportLevel;
+ local @EXPORT;
while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
($first,$rest) = ($1,$2);
$$hash{$first} = $rest;
}
else {
- eval "\$opt_$first = \$rest;";
+ ${"opt_$first"} = $rest;
push( @EXPORT, "\$opt_$first" );
}
}
$$hash{$first} = 1;
}
else {
- eval "\$opt_$first = 1;";
+ ${"opt_$first"} = 1;
push( @EXPORT, "\$opt_$first" );
}
if ($rest ne '') {
}
}
}
- $Exporter::ExportLevel++;
- import Getopt::Std;
+ unless (ref $hash) {
+ local $Exporter::ExportLevel = 1;
+ import Getopt::Std;
+ }
}
# Usage:
local($argumentative, $hash) = @_;
local(@args,$_,$first,$rest);
local($errs) = 0;
- local $Exporter::ExportLevel;
+ local @EXPORT;
@args = split( / */, $argumentative );
while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
$$hash{$first} = $rest;
}
else {
- eval "\$opt_$first = \$rest;";
+ ${"opt_$first"} = $rest;
push( @EXPORT, "\$opt_$first" );
}
}
$$hash{$first} = 1;
}
else {
- eval "\$opt_$first = 1";
+ ${"opt_$first"} = 1;
push( @EXPORT, "\$opt_$first" );
}
if($rest eq '') {
}
}
else {
- print STDERR "Unknown option: $first\n";
+ warn "Unknown option: $first\n";
++$errs;
if($rest ne '') {
$ARGV[0] = "-$rest";
}
}
}
- $Exporter::ExportLevel++;
- import Getopt::Std;
+ unless (ref $hash) {
+ local $Exporter::ExportLevel = 1;
+ import Getopt::Std;
+ }
$errs == 0;
}