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

Hello! Sorry fro my English. :) I have the next program:
#!/usr/bin/perl use strict; use feature qw(say); use warnings; use utf8; use Getopt::Long; my ($helpCmdArgv, $configCmdArgv, $setupCmdArgv, $servertypeCmdArgv); my $cmdArgsResult; $cmdArgsResult = GetOptions ( "help" => \$helpCmdArgv, "config=s" => \$configCmdArgv, "setup" => \$setupCmdArgv, "servertype=s" => \$servertypeCmdArgv ); # Condition 1 if ( (@ARGV eq 0) or ($helpCmdArgv eq 1) ) { say scalar(@ARGV); &Help; }; # Condition 2 say scalar(@ARGV); ($configCmdArgv) ? say 'Custom config' : say 'Default config'; sub Help { print <<HELP; Avliable options: --config - Main config file --setup - Just install files. --servertype - Server type, where: |-> db - Database server; |-> app - Application server; HELP exit 1; };
I want run program with options --help and without options. In both cases i want that my programm show me HELP MESSAGE (see Condition 1). But i need aslo to set arguments like this  ./install.pl --config main.cfg --setup (see Condition 2). But this not work, because @ARGV always zero and "Condition 2" never to be. I read that GetOpt ignor (or replace) standart ARGV. You can suggest me check "--config" and "--setup" in the "Condition 1". But I would like to avoid it. How to check for "--config main.cfg" when the user can execute the program with an empty argument? Thanks.

Replies are listed 'Best First'.
Re: GetOpt::Long empty ARGV
by Discipulus (Canon) on Jan 11, 2016 at 12:13 UTC
    Hello Pazitiff,

    Getopt::Long consumes what it find in @ARGV is because of this that you'll find it empty and you never reach the second condition.

    If you want to set some default, do that before even calling the GetOpts routine: in the case of a switch is simple, but if you only want to print the help anyway do that with an explicit call for the Help sub and leave it away from @ARGV
    See also this thread about the use of the module. Remember can you can leave something in @ARGV using the double dash -- see also The Dynamic Duo --or-- Holy Getopt::Long, Pod::UsageMan!

    HtH
    L*
    UPDATE: your second 'condition' is not a condition! And also remove the exit 1 from the sub Help or the program will exit, as requested.
    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.
Re: GetOpt::Long empty ARGV
by VinsWorldcom (Prior) on Jan 11, 2016 at 15:59 UTC

    This is the template I use:

    use strict; use warnings; use Getopt::Long qw(:config no_ignore_case); use Pod::Usage; my %opt; my ( $opt_help, $opt_man ); GetOptions( # 'add+' => \$opt{add}, # 'flag!' => \$opt{flag}, # 'integer=i' => \$opt{integer}, # 'optional:s' => \$opt{optional}, # 'string=s' => \$opt{string}, 'help!' => \$opt_help, 'man!' => \$opt_man, ) or pod2usage( -verbose => 0 ); pod2usage( -verbose => 1 ) if defined $opt_help; pod2usage( -verbose => 2 ) if defined $opt_man; # Make sure at least one argument provided if ( !@ARGV ) { pod2usage( -verbose => 0, -message => "$0: one argument required\n +" ); } __END__ =head1 NAME [TITLE] - [TEXT] =head1 SYNOPSIS [PROGNAME ARGS] =head1 DESCRIPTION [TEXT DESCRIPTION] =head1 OPTIONS [OPT DESCRIPTION] --help Print Options and Arguments. --man Print complete man page. =head1 LICENSE This software is released under the same terms as Perl itself. If you don't know what that means visit L<http://perl.com/>. =cut
      Thanks guys. You helped me a lot!