in reply to Re: why this not work? -- return value of GetOptions
in thread why this not work?

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?

Replies are listed 'Best First'.
Re^3: why this not work? -- return value of GetOptions
by Corion (Patriarch) on Oct 10, 2016 at 08:34 UTC

    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(); }
      Ah! Understand! Thanks! It's even not a real question, the logic confuse me. Sure, I should use the clear one. Please del this stupid question.