in reply to Help required on Getopt::Long

pass_through configuration option and &GetOptionsFromArray has a solution -- complete with option requirement type checking -- for you ...

cat p.pl perl p.pl -h -p 1 ; \ perl p.pl -p q -h ; \ perl p.pl -p -h ; \ perl p.pl -p q ; \ perl p.pl -p ; \ perl p.pl -p 3 use strict; use warnings; # May or may not work wit previous version(s). use Getopt::Long 2.38; my ( $h ); Getopt::Long::Configure( 'pass_through' ); GetOptions( 'help' => \$h ); die "HELP" if $h; my ( $p ); Getopt::Long::Configure( 'nopass_through' ); Getopt::Long::GetOptionsFromArray ( \@ARGV , 'p=i' => \$p ) or die; printf "p: %s\n" , defined $p ? $p : 'undef'; __END__ HELP at p.pl line 10. HELP at p.pl line 10. HELP at p.pl line 10. Value "q" invalid for option p (number expected) Died at p.pl line 14. Option p requires an argument Died at p.pl line 14. p: 3

Replies are listed 'Best First'.
Re^2: Help required on Getopt::Long
by Anonymous Monk on May 17, 2012 at 01:56 UTC

    Come to think of it, Getopt::Long::GetOptionsFromArray( \@ARGV , ... ) can be replaced with GetOptions( ... ).