Solved

getopts() from Getopt::Std

Posted on 1998-11-04
13
1,142 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl script 4 97
instmodsh - listing perl modules 2 73
Perl Script to read file contents, make changes, and build new file 7 90
Call Shell Script from Perl Script 6 75
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

18 Experts available now in Live!

Get 1:1 Help Now