Tuna has asked for the wisdom of the Perl Monks concerning the following question:

While working on a solution for Does a file really exist?, I came across some bizarre behavior, that's got me tearing my hair out. Basically, the code below is taken directly from perldoc Date::Calc.
#!/usr/bin/perl -w #find-missing-files.pl use strict; use Date::Calc qw( Delta_Days Add_Delta_Days ); my (@start, @stop); my $from = $ARGV[0]; my $to = $ARGV[1]; $from =~ s/-/,/g; $to =~ s/-/,/g; #@start = $from; print "@start\n"; #@stop = $to; print "@stop\n"; @start = (1999,05,27); @stop = (1999,06,01); my $j = Delta_Days(@start,@stop); for ( my $i = 0; $i <= $j; $i++ ) { my @date = Add_Delta_Days(@start,$i); printf("%4d-%02d-%02d\n", @date); }
As is,
./find-missing-files.pl
will produce the following output:
1999-05-27 1999-05-28 1999-05-29 1999-05-30 1999-05-31 1999-06-01
But, uncomment:
#@start = $from; print "@start\n"; #@stop = $to; print "@stop\n";
comment this:
@start = (1999,05,27); @stop = (1999,06,01);
and run:
./find-missing-files.pl 1999-05-27 1999-06-01
it returns:
1999,05,27 1999,06,01 Usage: Date::Calc::Delta_Days(year1, month1, day1, year2, month2, day2 +) at ./test.pl line 19.
Can anyone explain what's going on here?

Replies are listed 'Best First'.
(zdog) Re: Weird Date::Calc behavior; Bug?
by zdog (Priest) on Apr 19, 2001 at 08:51 UTC
    You cannot assign a scalar to an array that way. You should instead split the scalar up like so:

    #!/usr/bin/perl -w #find-missing-files.pl use strict; use Date::Calc qw ( Delta_Days Add_Delta_Days ); my (@start, @stop); @start = split (/-/, $ARGV[0]); print "@start\n"; @stop = split (/-/, $ARGV[1]); print "@stop\n"; for ( my $i = 0; $i <= Delta_Days(@start, @stop); $i++ ) { my @date = Add_Delta_Days (@start, $i); printf ("%4d-%02d-%02d\n", @date); }
    Update: You can also do something like:

    $from =~ s/-/,/g; $to =~ s/-/,/g; @start = eval ($from); @stop = eval ($to);

    Zenon Zabinski | zdog | zdog7@hotmail.com

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Weird Date::Calc behavior; Bug?
by nardo (Friar) on Apr 19, 2001 at 08:45 UTC
    You're commenting out @start = (1999,05,27); (which assigns 3 elements to @start) and replacing it with @start = $from; (which assigns 1 element to @start). You later call Delta_Days(@start,@stop); which works when both @start and @stop contain 3 elements each, but doesn't work when both @start and @stop contain only 1 element each.

    The solution is to call it with 6 arguments in the form Delta_Days(year1, month1, day1, year2, month2, day2), the fact that the two arguments are scalars which happen to have commas in them doesn't change the fact that they are only two arguments.