zapp_prefect has asked for the wisdom of the Perl Monks concerning the following question:

I try to use Getopt::Long to get options, and test it:
use Getopt::Long; my ( $timeout, $show_help ) = 30; unless ( GetOptions("timeout|t=i" => \$timeout, "help|h|?" => \$show_h +elp) && $show_help ) { error_info(); }
I means, if GetOptions can get the right options and find --help option ,it should show the help info output by error_info(). or if GetOptions failed to get the right options, some errors happend, it also should show the help info. but when I execute the script likes below:
my.pl --help
there's no output, so why?

Replies are listed 'Best First'.
Re: why this not work? -- return value of GetOptions
by Discipulus (Canon) on Oct 10, 2016 at 07:26 UTC
    The return value of GetOptions is useful: it tells you if some command line arguments processing went wrong. Then it's up to you to do checks and decide if is worth to continue with the program.

    Your description but that not work it is not so useful to me to understand what you want to achieve.

    Constructs like unless (A && B) are potentially confusing: your call to error_info happens only if GetOptions returns true and also $show_help is defined. It does not seems too logical to me.

    After I read The Dynamic Duo --or-- Holy Getopt::Long, Pod::UsageMan! i usually start with something like:

    use Getopt::Long; use Pod::Usage; unless ( GetOptions ( "color=s"=>\$par_color, "help" => \$par_help, ) ) {pod2usage(-verbose => 1)} if (defined $par_help){pod2usage(-verbose => 1)}

    pod2usage implicitly exit the program. And is a sane behaviour: exit if arguments are not what you expected and exit if user requested help to be shown.

    After this you need to manually validate your given arguments, but this is another story..

    See also GetOpt Organization

    HtH

    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.
      Sorry, I means, if GetOptions can get the right options and find the --help option ,it should show the help info output by error_info(), or if GetOptions failed to get the right options, some errors happend, it also should show the help info. but when I execute the script likes below:
      my.pl --help
      there's no output, so why?

        When I run your code:

        use Getopt::Long; my ( $timeout, $show_help ) = 30; unless ( GetOptions("timeout|t=i" => \$timeout, "help|h|?" => \$show_h +elp) && $show_help ) { error_info(); }

        I get the following output:

        Undefined subroutine &main::error_info called at tmp.pl line 5. </c>

        This is what I expect. If I call your script with --help like this:

        perl -w tmp.pl --help

        I get no output. This is also what I expect, because your && expression is not true ($show_help and GetOptions will be true, which never satisfies an unless statement).

        Maybe you want to use less clever code like the approach outlined in the documentation of Getopt::Long?

        use Getopt::Long; GetOptions( "timeout|t=i" => \my $timeout, "help|h|?" => \my $show_help ) or die "bad options."; $timeout ||= 30; if ( $show_help ) { print "Showing help\n"; error_info(); }
Re: why this not work?
by Anonymous Monk on Oct 10, 2016 at 06:23 UTC

    but that not work, why?

    it not work "because"

    also I've never found the return value of GetOptions to be reliable for any use, ignore it