Hey all,

Someone recently asked on the Catalyst list about an error that said

   Odd number of elements in anonymous hash at
   /Library/Perl/5.8.8/darwin-thread-multi-2level/Moose/Object.pm line 39.

I just happened to happen to get a similar error recently (actually from Catalyst and not Moose in my case), so I posted a response that included a possible (minor) enhancement to Moose. Re-posting here (at Dave R's request), in case anyone has any thing to weigh in about this proposed fix. If no one sees any potential problems with it, I'll pass the patch on to [email protected].

Original post below.

-Sir


   I notice that the relevant bit of Moose::Object (line 39 from the
   error you got) says:

         28 sub BUILDARGS {
         29     my $class = shift;
         30     if ( scalar @_ == 1 ) {
         31         unless ( defined $_[0] && ref $_[0] eq 'HASH' ) {
         32             Class::MOP::class_of($class)->throw_error(
         33                 "Single parameters to new() must be a HASH
       ref",
         34                 data => $_[0] );
         35         }
         36         return { %{ $_[0] } };
         37     }
         38     else {
       * 39         return {...@_};*
         40     }
         41 }

   Based on that, it looks like some object is trying to instantiate
   with an odd number of args, but not just one arg.  Something like

       my $obj = My::Awesome::Moose::Class->new(a=>1, b=>2, 'c');

   The check in line 30 checks if there was just one arg.  Probably, it
   should do something more like

       --- Object.old.pm    2010-10-13 11:03:48.000000000 -0400
       +++ Object.new.pm    2010-10-13 11:07:15.000000000 -0400
       @@ -35,9 +35,11 @@
                 }
                 return { %{ $_[0] } };
             }
       -    else {
       -        return {...@_};
       +    elsif (scalar @_ % 2) {
       +        warn "all args should be paired values (e.g. a=>1),
       not: @_";
             }
       +
       +    return {...@_};
         }

         sub BUILDALL {


_______________________________________________
List: [email protected]
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/

Reply via email to