http://www.perlmonks.org?node_id=997116


in reply to Re^2: Range in Getopt
in thread Range in Getopt

That is because while the d option's value is required, the d option itself is not, check the definedness of $_debug afterwards:

use Getopt::Long; my $_debug; GetOptions('d=i' => sub { $_debug = $_[1]; ($_debug > 0 && $_debug <= 4) or die "Invalid Debug Level ($_d +ebug)" } ) or exit 1; die "Debug Level is required" unless defined $_debug; warn "Debug Level is $_debug\n";

or set a default value before GetOptions:

use Getopt::Long; my $_debug = 0; GetOptions('d=i' => sub { $_debug = $_[1]; ($_debug > 0 && $_debug <= 4) or die "Invalid Debug Level ($_d +ebug)" } ) or exit 1; warn "Debug Level is $_debug\n";