我想轉換使用@ARGV與使用Getopt::Std
而不是在我的Perl腳本。 我得到一些substr錯誤,需要一些幫助搞清楚這一點。幫助perl腳本轉換使用argv使用getopts
錯誤:
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
這裏是我的代碼。 (註釋代碼是工作的代碼中使用@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)[email protected];
# 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);
}
謝謝你的澄清。 – jdamae