How can I respond to the -help flag with Getopt :: Std?

I want my script to print a help message when it starts with a command line option --help. Based on the Getopt::Std documentation , this user should do the trick:

#!/usr/bin/env perl
use strict;
use warnings;
use 5.014;
use Getopt::Std;

sub HELP_MESSAGE {
    say "HELP MESSAGE";
}

But he does not print anything. I also tried to add this, out of curiosity:

for (@ARGV) {
    HELP_MESSAGE() if /--help/;
}

It really works, but it seems pretty messy. I know that using a flag -hwill be pretty simple, but I would like to have both.

+4
source share
1 answer

Getopt :: Std documentation says

- , getopts() --help --version. main::HELP_MESSAGE() / main::VERSION_MESSAGE() , ;...

, :

#!/usr/bin/env perl

use strict;
use warnings;
use 5.014;
use Getopt::Std;

$Getopt::Std::STANDARD_HELP_VERSION = 1;
our $VERSION = 0.1;

getopts('');       # <<< You forgot this line, and `getopt()` DOES NOT work

sub HELP_MESSAGE {
    say "HELP MESSAGE";
}

:

$ ./t00.pl --help
./t00.pl version 0.1 calling Getopt::Std::getopts (version 1.07),
running under Perl version 5.16.3.
HELP MESSAGE
+4