Hello g_speran,

interesting question: I was sure Getopt::Long was consuming the @ARGV array so you had the possibility to check if something unkonow is still there.. but is too late for this when it arrives the moment and "Processing sub first" is already fired.

Then I read about die("!FINISH") special behaviour, but is not useful.. again it fires too late.. so you must move quick to intercept the unkonown option.

A BEGIN block does the trick (but I moved the help sub on top, so it is parsed before the BEGIN block, so it can be called: you must move other subs to give them a chance to be called):

use warnings; use strict; use Getopt::Long; sub help { print "Available options are: \n"; print "\t--first\n"; print "\t--help\n"; } BEGIN{ # no warnings; #strangely this is not able to suppress the warning + "Undefined subroutine &main::first called at.." GetOptions( 'first' => \&first, 'help' => \&help, ) or &help and die "Invalid options passed to $0\n" } sub first { print "Processing sub first\n"; } __END__ perl getoptcheck.pl --year --first Unknown option: year Undefined subroutine &main::first called at ..perl5.26.64bit/perl/lib/ +Getopt/Long.pm line 606. Available options are: --first --second --help Invalid options passed to getoptcheck.pl BEGIN failed--compilation aborted at getoptcheck.pl line 20.

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Getopt - Validate arguments before processing -- BEGIN by Discipulus
in thread Getopt - Validate arguments before processing by g_speran

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.