Solved

getopts() from Getopt::Std

Posted on 1998-11-04
13
1,139 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now