From sean at blanton.com Thu Apr 28 14:00:36 2011 From: sean at blanton.com (Sean Blanton) Date: Thu, 28 Apr 2011 16:00:36 -0500 Subject: [WindyCity-pm] undef and Moose types Message-ID: When you have a constructor and it fails, It's not common practice to return a "null" object. Perl modules usually return undef. However, in Moose, in a Moose role that I want to do this: has ssh => ( is => 'rw', isa => 'Net::SSH2', default => \&ssh_builder, lazy => 1, ); --- To use this within a class, I do this: my $ssh = $self->ssh; $log->warn("couldn't connect") unless defined $ssh; #-- this line for illustration --- So, I want to handle any errors such as when \&ssh_builder fails and returns 'undef', but what happens is that it crashes before the error handling code because 'undef' is not of type 'Net::SSH2'. So what I do to fix it is take out the 'isa => 'Net::SSH2',' from the definition of the 'ssh' attribute. Now I lose the advantage of having the type check in my development for the 99.99% of the time when there are no errors. It seems to me that 'undef' should be considered to be a member of every class, or better yet have an option to make it so. Is there another way of getting the advantages of the type checking and also handling exceptions like this? Thanks, Sean -------------- next part -------------- An HTML attachment was scrubbed... URL: