1

Getopt::Stdperl スクリプトで @ARGV の使用を代わりに使用して変換しようとしています。substr エラーが発生しており、これを理解するための助けが必要です。

エラー:

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)=@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);
}
4

2 に答える 2

2

「getopt、getopts - スイッチ クラスタリングで単一文字スイッチを処理する」

単一文字の切り替えのみが許可され$opt{fd}$opt{ld}未定義であるため。

Getopt::Longは、あなたが望むことを行います。

use strict;
use warnings;
use Getopt::Long;

my $fd;
my $ld;

my $result = GetOptions(
    'fd=s' => \$fd,
    'ld=s' => \$ld,
);

die unless $result;

print "fd: $fd\n";
print "ld: $ld\n";
于 2011-08-14T21:21:07.240 に答える