getopts() from Getopt::Std

I am using getopts() in a perl program. The synopsis is like this:

     program.pl [-r] -d [DD/MM/YY]

Now, typically $opt_d would get the date mentioned as DD/MM/YY. But I want a default action to take place if it is left out. So really it is optional. If DD/MM/YY is omitted, then today's date will be used.

The problem using getopts(rd:) is that it will complain that the date is empty because it feels that $opt_d must have an argument (or be defined?). And the problem using getopts(rd) is that $opt_d will always be 1 if used with the '-d' flag because it is Boolean. It never gets the actual string following the '-d' flag. How can I take care of this without reverting to @ARGV ?
PakfanAsked:
Who is Participating?
 
b2piConnect With a Mentor Commented:
perldoc Getopt::Std

the getopt routines (without the use Getopt::Std) is a holdover from perl4

You also might want to check out
perldoc Getopt::Long

for gnu style arguments
0
 
PakfanAuthor Commented:
Edited text of question
0
 
b2piCommented:
use strict;
use Getopt::Std;

my(%Options);
getopts('rd:', \%Options);

$Options{r} = 0 unless defined($Options{r});
if (exists($Options(d))) {
  if (defined($Options(d))) {
     $date = $Options(d);
  } else {
     $date = '010199';
  }
} else {
   die "You must specify a date  (-d is 010199)";
}

0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
PakfanAuthor Commented:
Assuming you meant $Options{d}, and not $Options(d), it still doesn't work as intended. Using that code, the program always dies. In other words   exists($Options{d})   is always false.
0
 
ozoCommented:
assuming -d is the last arg

getopts('rd');
$opt_d &&= shift || $today;

0
 
PakfanAuthor Commented:
Assuming you meant $Options{d}, and not $Options(d), it still doesn't work as intended. Using that code, the program always dies. In other words   exists($Options{d})   is always false.
0
 
PakfanAuthor Commented:
Assuming you meant $Options{d}, and not $Options(d), it still doesn't work as intended. Using that code, the program always dies. In other words   exists($Options{d})   is always false.
0
 
b2piCommented:
Excuse the types.  The following:

use strict;
use Getopt::Std;

my(%Options, $date, $r);
getopts('rd:',\%Options);

$Options{r} = 0 unless defined($Options{r});
if (exists($Options{d})) {
  if (defined($Options{d})) {
    $date = $Options{d};
  } else {
    $date = '010199';
  }
} else {
  die "You must specify a date (-d is 010199)";
}

print "R is ", $r ? "true" : "false", "\n";
print "Got date as $date\n";


gives:

# a.pl
You must specify a date (-d is 010199) at C:\perl\bin\a.pl line 16.

# a.pl -d
R is false
Got date as 010199

# a.pl -d 010199
R is false
Got date as 010199

# a.pl -d 010129
R is false
Got date as 010129


0
 
PakfanAuthor Commented:
Assuming you meant $Options{d}, and not $Options(d), it still doesn't work as intended. Using that code, the program always dies. In other words   exists($Options{d})   is always false.
0
 
b2piCommented:
I trust you meant to leave another comment.  Perhaps you rejected the last answer by mistake.  In any case, the code I submitted to you works on both unix (at least Solaris 2.5.1) and NT.
0
 
PakfanAuthor Commented:
sorry about the extraneous comments and rejections. this shouldn't be happening. I'm just reloading the page. Anyways, the program is still dying. I'm also running Solaris 2.5.1. Perl version is 5.003
0
 
b2piCommented:
Upgrade your perl.
0
 
PakfanAuthor Commented:
Indeed that was the problem. It works with 5.004 or greater. I'm curious how you knew about using getopts() in this manner:
      getopts('rd:',\%Options);
In the Second Edition of the Camel Book, it only describes syntax such as:
      getopts('rd:');
Where did you find out about using the hash as a second argument? You can also propose your answer now. I'll give the points and hopefully no unintentional rejections or comments.
0
All Courses

From novice to tech pro — start learning today.