in reply to How do I report an error back to the user of my object?
The usual way of indicating a failure in Perl is to die, which functions as an exception that can be caught downstream, e.g.
which could be invoked as my $obj = eval{coolobject->new} or die "$@";sub new{ my $class = shift; my $self; $self->{name} = 'new'; bless ($self); if ($self->loaddata()){ return $self; }else{ die "Error loading data!"; } }
If you absolutely want to pass an error and return an undef, you could set $@ manually, but this is probably poor form.
If you are rolling your own objects for fun/education, I'd recommend you look at perltoot. If you are rolling objects for deployment, I'd look at (and use) some prior art like Moose, Mouse...
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
|
|---|