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?

In reply to Using multiple args per switch with Getopts::Std by blink

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.