Getopt eats up the values in @ARGV so it is alway empty

Just to be nitpicky -- not quite. Getopt eats the options in @ARGV. Anything else will remain. Consider the following:

#!/usr/bin/perl use Getopt::Long; GetOptions( "i" => \$options{i}, "o=s" => \$options{o}, "p=s" => \$options{p}, "h" => \$options{h} ); foreach $arg (@ARGV) { print "$arg is still in ARGV\n"; } foreach $opt (keys %options) { print "$opt is $options{$opt}\n" if $options{$opt}; }
and note the output:
nkuvu$ perl try.pl --i h --o=foo h is still in ARGV o is foo i is 1

I have used this to check some command line inputs. Specifically, for most of my scripts anything specified on the command line should be an option. Anything after that is an error. So I'd use it sort of like:

GetOptions( #whatever ); die "Gah! (with appropriate error message here)\n" if (@ARGV);

In reply to Re^2: problem with Getopt::Long by Nkuvu
in thread problem with Getopt::Long by syedtoah

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.