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

#!/usr/bin/perl use strict; use Date::Calc qw(Delta_Days); my $origd1 = "01-02-2015"; my $origd2 = "05-06-2015"; my @a = split(/-|-/, $origd1, 3); my @b = split(/-|-/, $origd2, 3); my @c = join ( ',', reverse @a ); my @d = join ( ',', reverse @b ); print "@c\n"; print "@d\n"; my $difference = Delta_Days(@c, @d); print "Difference is $difference\n";
produces
2015,02,01 2015,06,05 Date::Calc::PP::Delta_Days(): Usage: Date::Calc::Delta_Days($year1,$mo +nth1,$day1,$year2,$month2,$day2) at ./date_test.pl line 17

why not even the easiest things will work without hassles? any hints?

Replies are listed 'Best First'.
Re: Date::Calc question
by Athanasius (Archbishop) on Jul 17, 2015 at 08:40 UTC

    Hello flynn7312,

    any hints?

    Yes: Tip #4 from the Basic debugging checklist:

    Dump arrays, hashes and arbitrarily complex data structures.

    But I actually prefer to use Data::Dump:

    18:25 >perl 1309_SoPW.pl 2015,02,01 2015,06,05 ["2015,02,01"] ["2015,06,05"] Date::Calc::PP::Delta_Days(): Usage: Date::Calc::Delta_Days($year1,$mo +nth1,$day1,$year2,$month2,$day2) at 1309_SoPW.pl line 33 18:27 >perl 1309_SoPW.pl

    The output from print "@c\n"; looks like a list of 3 elements, but the corresponding output from Data::Dump shows that it’s actually a single element, the string "2015,02,01".

    As FreeBeerReekingMonk says, the problem is the call to join which turns the list into a string. Just remove the join, and the script works as expected.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      thank you very much, it worked.
      ./date_test.pl 2015 02 01 2015 06 05 Difference is 124
Re: Date::Calc question
by FreeBeerReekingMonk (Deacon) on Jul 17, 2015 at 08:23 UTC
    my @c = reverse @a; my @d = reverse @b;

    The arrays get automatically flattened afterwards in Delta_Days(@c, @d)

    Edit: A little more explaining:
    The join used would put a single string, with comma's in it, into the first position of an array. Thus, creating arrays with one element, so Delta_Days() would only receive 2 parameters, and those two do not expand to a number as expected, but to zero, thus Delta_Days("2015,02,01","2015,06,05") which is the equivalent of doing Delta_Days(0,0) because the function needs integers. By keeping the array as an array, the argument lists get flattened automatically for you.

    If you still want the debug printing with comma's you can:

    print join(",", @c) . "\n";

      so im stuck again... i can not find a function in Date::Calc, which can check for date "order", eg. one date (from) is before another (to).

        Take a look at the module documentation, Delta_Days returns the difference in dates, if the $difference (in your example) is negative then date 2 occurred this number of days before date 1.

        Update: Fixed typo.