Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Getopt - Validate arguments before processing -- BEGIN

by Discipulus (Canon)
on Jan 31, 2022 at 08:57 UTC ( [id://11140986]=note: print w/replies, xml ) Need Help??


in reply to Getopt - Validate arguments before processing

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.

Replies are listed 'Best First'.
Re^2: Getopt - Validate arguments before processing -- BEGIN
by Corion (Patriarch) on Jan 31, 2022 at 08:59 UTC

    The code in the BEGIN block runs before Perl has even seen your declaration of the subroutine sub first { .... To work around that, you need to move the BEGIN block below the subroutine declaration(s).

      yes thanks Corion, I know that.. but probably I have expressed it poorly :)

      > (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)

      I just wanted to suppress the warning but I was not able to find its category, and neither no warnings suppresses it

      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.

        It's not a warning. It's a fatal error which GetOptions apparently catches and converts to a warning. And Corion told you how to fix it.

Re^2: Getopt - Validate arguments before processing -- BEGIN
by soonix (Canon) on Jan 31, 2022 at 20:30 UTC
    as long as sub first is defined below the BEGIN block,
    even if the only argument is '--first' (i.e. no invalid options),
    you'll have
    Undefined subroutine &main::first called at .../Getopt/Long.pm line 60 +7. Available options are: --first --help Invalid options passed to ....pl BEGIN failed--compilation aborted at ....pl line 17.
    which means, as Corion already mentioned, you have to move the BEGIN block below the subroutines, which defeats the purpose.

    besides, from an esthetic viewpoint, it isn't nice, if a program reacts with a "compilation error" to a wrong invocation.

    I think I like haukex's variant "push @actions" best, and it would even allow for an optional --ignore-unknown-switches or --please-dont-die switch to suppress dying on invalid options ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11140986]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-20 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found