Solved

getopts() from Getopt::Std

Posted on 1998-11-04
13
1,144 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Sort Question 4 136
Transforming a Soap message to a simple xml message! 10 146
Excel to CSV conversion with specific columns 5 83
Perl Frameworks 1 93
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 …
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…

832 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