Solved

getopts() from Getopt::Std

Posted on 1998-11-04
13
1,151 Views
Last Modified: 2008-02-26
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 ?
0
Comment
Question by:Pakfan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 

Author Comment

by:Pakfan
ID: 1205948
Edited text of question
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1205949
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
 

Author Comment

by:Pakfan
ID: 1205950
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 1205951
assuming -d is the last arg

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

0
 

Author Comment

by:Pakfan
ID: 1205952
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
 

Author Comment

by:Pakfan
ID: 1205953
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
 
LVL 5

Expert Comment

by:b2pi
ID: 1205954
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
 

Author Comment

by:Pakfan
ID: 1205955
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
 
LVL 5

Expert Comment

by:b2pi
ID: 1205956
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
 

Author Comment

by:Pakfan
ID: 1205957
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
 
LVL 5

Expert Comment

by:b2pi
ID: 1205958
Upgrade your perl.
0
 

Author Comment

by:Pakfan
ID: 1205959
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
 
LVL 5

Accepted Solution

by:
b2pi earned 70 total points
ID: 1205960
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

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question