blink has asked for the wisdom of the Perl Monks concerning the following question:

I want to be able to do the following:

myprog.pl -h <host> -d DD/MM/YYYY ] HH:MM:SS -e DD/MM/YYYY HH:MM:SS

This code will only capture DD/MM/YYYY, while ignoring HH:MM:SS as well as the entire argument to -e

#!/usr/local/bin/perl -w use strict; use Getopt::Std; getopts('c:d:e:h:'); our ($opt_c, $opt_d, $opt_e, $opt_h); # check our switch values if ($opt_h) { &help; } if ($opt_c) { my $client = $opt_c; } #else { &help; } print "d = $opt_d\ne = $opt_e\n"; &help unless ( ($opt_d, $opt_e) =~ m/(\d{2})\/(\d{2})\/(\d{4})(.*?)$/ + ); sub help { print <<HELP; usage: $0 -c <client> -d <begin date> [time] -e <end date> [time] Date must be in the format "DD/MM/YYYY", i.e., 09/04/2002. Time is optional. If it was included in the email you received from se +curity, then enter it. Time must be entered as "HH:MM:SS". example: $0 -c eman -d 09/01/2002 04:00:00 -e 09/04/2002 23:59:59 HELP exit 1; }

Running this code snippet produces the following output:

Useless use of a variable in void context at ./tape-pull.pl line 18. Use of uninitialized value in concatenation (.) or string at ./tape-pu +ll.pl line 16. d = 09/01/2002 e = Use of uninitialized value in pattern match (m//) at ./tape-pull.pl li +ne 18. usage: ./tape-pull.pl -c <client> -d <begin date> [time] -e <end date> + [time] Date must be in the format "DD/MM/YYYY", i.e., 09/04/2002. Time is optional. If it was included in the email you received from se +curity, then enter it. Time must be entered as "HH:MM:SS". example: ./tape-pull.pl -c eman -d 09/01/2002 04:00:00 -e 09/04/2002 2 +3:59:59
Does anyone have any ideas as to how to work around this?

Replies are listed 'Best First'.
Re: Using multiple args per switch with Getopts::Std
by kschwab (Vicar) on Aug 28, 2002 at 02:51 UTC
    &help unless (($opt_d, $opt_e) =~ /(\d{2})\/(\d{2})\/(\d{4})(.*?)$/);
    isn't doing what you think it is. Compare them separately...
      You may be right, but I don't think that that's where my problem lies. The print statement that prints $opt_d and $opt_e, and returns "DD/MM/YYYY", rather than "DD/MM/YYYY HH:MM:SS", is executed before hitting the regex.
Re: Using multiple args per switch with Getopts::Std
by tommyw (Hermit) on Aug 28, 2002 at 10:56 UTC

    Wrap the command line parameters in quotes (or otherwise escape the spaces), so they do not cause the text to be seen as multiple arguments:

    myprog.pl -h <host> -d "DD/MM/YYYY HH:MM:SS" -e "DD/MM/YYYY HH:MM:SS"

    --
    Tommy
    Too stupid to live.
    Too stubborn to die.