[Chicago-talk] undef and Moose types

Sean Blanton sean at blanton.com
Thu Apr 28 14:00:36 PDT 2011

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


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'

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?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/chicago-talk/attachments/20110428/d4ade820/attachment.html>

More information about the Chicago-talk mailing list