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

While working with the DateTime module, I'm getting a very odd and frustrating error back from the Params::Validate module:
The 'locale' parameter (undef) to DateTime::new was an 'undef', which +is not one of the allowed types: scalar object at /usr/lib/perl5/site_perl/5.8.5/Params/ValidatePP.pm line 634 Params::Validate::__ANON__('The \'locale\' parameter (undef) to Da +teTime::new was an \'un...') called at /usr/lib/perl5/site_perl/5.8.5 +/Params/ValidatePP.pm line 485 Params::Validate::_validate_one_param('undef', 'HASH(0x1039fb0)', +'HASH(0xe04910)', 'The \'locale\' parameter (undef)') called at /usr/ +lib/perl5/site_perl/5.8.5/Params/ValidatePP.pm line 345 Params::Validate::validate('ARRAY(0xa87370)', 'HASH(0xe284a0)') ca +lled at /usr/lib64/perl5/site_perl/5.8.5/x86_64-linux-thread-multi/Da +teTime.pm line 171 DateTime::new('undef', 'year', 2015, 'month', 08, 'day', 24, 'hour +', 17, ...) called at MonitorClass.pm line 80 MonitorClass::MakeDateTime('MonitorClass=HASH(0xf03430)', 2015, 08 +, 24, 17, 36, 47, 144) called at MonitorClass.pm line 196

This is driving me nuts, as the parameters sent to DateTime->new() are correct, as can be seen in the final line of my error. I'm not even able to reproduce the issue consistently. For the vast majority of cases my code works great but every morning when I get into work I see that it's thrown the above a few times.

I'm going to try explicitly setting the locale parameter each time I call DateTime->new(); but I'd rather understand what I'm doing wrong here.

Here's my method that's invoking DateTime->new(); it's ridiculously simple:
sub MakeDateTime { my $self = shift; my ($Year, $Month, $Day, $Hour, $Minute, $Second, $MilliSecond) = @_ +; my $DateTime = DateTime->new( year => $Year, month => $Month, day => $Day, hour => $Hour, minute => $Minute, second => $Second, nanosecond => ($MilliSecond * 1000000), time_zone => 'America/New_York', locale => 'en_US', ); return $DateTime; }

Replies are listed 'Best First'.
Re: Strange Params::Validate Error
by stevieb (Canon) on Aug 26, 2015 at 14:03 UTC

    Using Data::Dumper, do this:

    sub MakeDateTime { my $self = shift; my ($Year, $Month, $Day, $Hour, $Minute, $Second, $MilliSecond) = @_ print Dumper \@_; ...

    It looks like there might be something unexpected at the front of the params array that may be confusing DateTime/Params::Validate, per this line in the error:

    DateTime::new('undef', 'year', 2015, 'month', 08, 'day', 24, 'hour', 1 +7, ...) called at MonitorClass.pm line 80

    DateTime may fail with your error if it can't figure out the locale, but your time_zone param is set properly so I don't see that being the issue. Also, please let us know which version of DateTime you're using.

    -stevieb

Re: Strange Params::Validate Error
by toolic (Bishop) on Aug 26, 2015 at 13:54 UTC
    Perl version 5.8.5 is very old. Perhaps you have old versions of the modules as well, and those old versions have bugs which have been fixed. Try to update.

      Try to update.

      On a newer Perl version it looks fine, even without setting locale parameter:
      % perl /tmp/scratch.pl  
      2015-08-24T17:36:47
      
      % perl -v
      
      This is perl 5, version 20, subversion 2 (v5.20.2) built for x86_64-linux-gnu-thread-multi
      ...
      
      % dpkg -l|grep libdatetime.*perl
      ...
      ii  libdatetime-perl                              2:1.20-1 
      ii  libdatetime-locale-perl                       1:0.46-1
      ...
      
      /tmp/scratch.pl contains:
      use warnings; use strict; use DateTime; # [faked method call] print MakeDateTime(undef, 2015, 8, 24, 17, 36, 47, 144); sub MakeDateTime { my $self = shift; my ($Year, $Month, $Day, $Hour, $Minute, $Second, $MilliSecond) = @_ +; my $DateTime = DateTime->new( year => $Year, month => $Month, day => $Day, hour => $Hour, minute => $Minute, second => $Second, nanosecond => ($MilliSecond * 1000000), time_zone => 'America/New_York', ); return $DateTime; }
Re: Strange Params::Validate Error
by anonymized user 468275 (Curate) on Aug 27, 2015 at 15:42 UTC
    en_US is the default, so I wonder if you need to set it anyway - perhaps an old glitch if you set to the default explicitly.

    One world, one people