Checking command line switches

R

Roger

#Greetings Perl folks, I have writen the code below, and it works just like I
# want. However, as a beginner I was concerned the code was not very
# 'perl-ish' so if anyone can comment on the working code below, I'd
# be appreciative and have a nice day.

# ########################################################################## #
# Check the user input if any. If restart usage is: progname -r mm/dd/yyyy #
# ########################################################################## #
if (!@ARGV == 0) {
if ($ARGV[0] eq '-r')
{
($month, $day, $year) = split /\//, $ARGV[1];
$month = $month +0; # these strip off
$day = $day + 0; # any leading zeros
if ($month > 12 || $month < 1 || $day > 31
|| $day < 1 || $year > 2035 || $year < 2000)
{
print "\n Invalid date ".$ARGV[1]; exit; }
else {$now_date = $ARGV[1];
$file_date = substr($ARGV[1] ,0,5); $file_date =~ s/\///g;}
}
elsif ( $ARGV[0] ne '-r') {print "\n Invalid switch ".$ARGV[0]; exit;}
}
 
R

Richard Morse

Roger said:
#Greetings Perl folks, I have writen the code below, and it works just like I
# want. However, as a beginner I was concerned the code was not very
# 'perl-ish' so if anyone can comment on the working code below, I'd
# be appreciative and have a nice day.

# ########################################################################## #
# Check the user input if any. If restart usage is: progname -r mm/dd/yyyy #
# ########################################################################## #
if (!@ARGV == 0) {
if ($ARGV[0] eq '-r')
{
($month, $day, $year) = split /\//, $ARGV[1];
$month = $month +0; # these strip off
$day = $day + 0; # any leading zeros
if ($month > 12 || $month < 1 || $day > 31
|| $day < 1 || $year > 2035 || $year < 2000)
{
print "\n Invalid date ".$ARGV[1]; exit; }
else {$now_date = $ARGV[1];
$file_date = substr($ARGV[1] ,0,5); $file_date =~ s/\///g;}
}
elsif ( $ARGV[0] ne '-r') {print "\n Invalid switch ".$ARGV[0]; exit;}
}

You can save yourself some trouble by looking at Getopt::Std and
Getopt::Long, which will handle some of the logic you need.

Also, I don't think you need to strip any leading zero explicitly --
using the values in the comparison will trigger the translation
automatically.

There are a number of modules on CPAN dealing with dates. These would
presumably also handle things like making sure that February doesn't end
up with 31 days...

Ricky
 
A

Anno Siegel

Roger said:
#Greetings Perl folks, I have writen the code below, and it works just like I
# want. However, as a beginner I was concerned the code was not very
# 'perl-ish' so if anyone can comment on the working code below, I'd
# be appreciative and have a nice day.
# ########################################################################## #
# Check the user input if any. If restart usage is: progname -r mm/dd/yyyy #
# ########################################################################## #

I'm missing "use strict; use warnings;" here. Switch them on, especially
when you want to crate quality code. You have do declare your variables
now. Since "$now_date" is apparently used later in the program, declare
it here:

my $now_date;
if (!@ARGV == 0) {

Equivalent: "if ( @ARGV ) {"
if ($ARGV[0] eq '-r')
{
($month, $day, $year) = split /\//, $ARGV[1];

Variable declarations and a better-written regex:

my ($month, $day, $year) = split m{/}, $ARGV[1];
$month = $month +0; # these strip off
$day = $day + 0; # any leading zeros

No need to strip leading zeroes, Perl copes with them.
if ($month > 12 || $month < 1 || $day > 31
|| $day < 1 || $year > 2035 || $year < 2000)
{
$file_date = substr($ARGV[1] ,0,5); $file_date =~ s/\///g;}
}

No message when the date is invalid? I think the user should be told.
elsif ( $ARGV[0] ne '-r') {print "\n Invalid switch ".$ARGV[0]; exit;}

For several reasons, the combo "print ...; exit;" is almost always
better written as "die ...", or even "croak ..." from the Carp module.

It has been pointed out that the Getopt::* and Time::* modules can
basically do all this. If I had to do without them, I'd write
the above somewhat like this:

use strict; use warnings;
my $now_date;
if ( @ARGV ) {
my $opt = shift;
if ( $opt eq '-r' ) {
my ( $month, $day, $year) = split m{/}, my $date = shift;
die "Bad month '$month' in '$date'" unless range( $month, 1, 12);
die "Bad day '$day' in '$date'" unless range( $day, 1, 31);
die "Bad year '$year' in '$date'" unless range( $year, 2000, 2035);
$now_date = $date;
} else {
die "Invalid switch '$opt'";
}
}

sub range { $_[ 1] <= $_[ 0] and $_[ 0] <= $_[ 2] }

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top