I am trying to convert the use of @ARGV with using Getopt::Std instead in my perl script.
I am getting some substr errors and need some help figuring this out.
Errors:
Use of uninitialized value in substr at ./h.pl line 33.
Use of uninitialized value in substr at ./h.pl line 33.
substr outside of string at ./h.pl line 33.
Use of uninitialized value in substr at ./h.pl line 33.
substr outside of string at ./h.pl line 33.
The 'month' parameter (undef) to DateTime::new was an 'undef', which is not one of the allowed types: scalar
at /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi/DateTime.pm line 176
DateTime::new('undef', 'HASH(0xb6932d0)') called at ./h.pl line 33
Here is my code. (commented out code was working code using @ARGV)
use strict;
use warnings;
use Getopt::Std;
use DateTime;
# Getopt usage
my %opt;
getopts ('fd:ld:h', \%opt);
$opt{h} and &Usage;
my $first_date = $opt{fd};
my $last_date = $opt{ld};
#unless(@ARGV==2)
#{
# print "Usage: myperlscript first_date last_date\n";
# exit(1);
#}
#
#my ($first_date,$last_date)=@ARGV;
# Convert using Getopts
my $date=DateTime->new(
{
year=>substr($first_date,0,4),
month=>substr($first_date,4,2),
day=>substr($first_date,6,2)
});
while($date->ymd('') le $last_date)
{
print $date->ymd('') . "\n";
$date->add(days=>1);
}
“getopt, getopts – Process single-character switches with switch clustering”
As only single character switches are allowed
$opt{fd}and$opt{ld}are undef.Getopt::Long does what you want.