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

Dear Monks,

I am trying to compare two dates with no luck:

use strict; use warnings; use 5.010; use Date::Manip; use Date::Manip::Date; my $date1 = Date::Manip::Date->new; my $date2 = Date::Manip::Date->new; my $format = "%Y--%m--%d"; my $error1 = $date1->parse_format($format, "2010--03--30"); my $error2 = $date2->parse_format($format, "2010--03--31"); say $error1; say $error2; if ($error1 == 0 and $error2 == 0) { #then successfully parsed dates my $flag = Date_Cmp($date1, $date2); say $flag; } --output:-- 0 0 Use of uninitialized value $flag in say at 1perl.pl line 21.

I expected $flag to be an integer less than 0, 0, or greater than 0. What am I doing wrong?

Replies are listed 'Best First'.
Re: Date::Manip can't get Date_Cmp to work
by SBECK (Chaplain) on Apr 01, 2010 at 11:15 UTC

    You've already gotten the answer to your question, but I'd like to point out one thing.

    You will get better performance if you do:

    my $date1 = Date::Manip::Date->new; my $date2 = $date1->new_date();

    If you're working with lots of dates, there will be cached data that allow you to achieve much better performance, and by creating the 2nd (and other) dates from the first, the cached data is shared, so they all benefit from it.

      Ok. Thanks.
Re: Date::Manip can't get Date_Cmp to work
by ikegami (Patriarch) on Apr 01, 2010 at 04:03 UTC

    You're using Date::Manip::Date objects as inputs to a Date::Manip function. You want

    my $flag = $date1->cmp($date2);

      You're using Date::Manip::Date objects as inputs to a Date::Manip function

      Ah. I thought they were some kind of fungible generic date objects.

      You want

      my $flag = $date1->cmp($date2);

      Oops. I didn't see that function in Date::Manip::Date. Yep, that worked.

      Thanks

        Date::Manip doesn't use objects at all.

        use strict; use warnings; use Data::Dumper; use Date::Manip; use Date::Manip::Date; { my $date = ParseDate("today"); print(Dumper($date)); } { my $date = Date::Manip::Date->new(); $date->parse_format("%Y--%m--%d", "2010--03--30") and die; local $Data::Dumper::Maxdepth = 1; print(Dumper($date)); }
        $VAR1 = '2010040100:00:00'; $VAR1 = bless( { 'err' => '', 'args' => 'ARRAY(0x20170a4)', 'data' => 'HASH(0x218b084)', 'objs' => 'HASH(0x239064)' }, 'Date::Manip::Date' );