Something like this might do what you need:
GetOptions(\%param, 'file:s', 'name:s', 'any:s', 'other:s', 'params:s' +); foreach (qw(file name any other params)) { die "Option $_ requires an argument\n" if defined($param{$_}) && $pa +ram{$_} eq ''; }

Using the colon (e.g. 'file:s') in the GetOptions call instead of an equals sign makes a string parameter on the switch optional rather than required. As a result, when GetOptions sees '-file -name', it interprets this as 'switch -file with no optional string param, followed by switch -name with no optional string param', rather than as 'switch -file with string param -name'. Then the subsequent loop goes through and complains if one of those "optional" parameters in fact has an empty value.

Also, if you check the perldoc for Getopt::Long you'll find that you can define handler subroutines to be called when an individual switch is encountered, which would probably also let you trap the case that's bothering you. I haven't used that capability in a while and it seems like it would be more work, but perhaps I haven't addressed all the angles of your question.

Good luck!


In reply to Re: Re: Re: Getopts::Long -- changing its behavior by seattlejohn
in thread Getopts::Long -- changing its behavior by jonjacobmoon

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.