Keep It Simple, Stupid | |
PerlMonks |
Re: Moose: return null objectby bart (Canon) |
on Apr 18, 2011 at 15:27 UTC ( [id://899960]=note: print w/replies, xml ) | Need Help?? |
It's a pretty standard practice in Perl OO (not necessarily using Moose) to return undef from the sub new if you can't build an object. After all, news is just a plain sub, in Perl, unlike in a lot of other languages. The sample code you posted here then would just work.
However, you might have a problem that it's not actually an object. Method calls and other attribute checking would all barf. So you might actually create a null object, by blessing it into some class and make sure that, when testing in boolean context, it returns false. I imagine you can use overload (with "bool") for that... In addition, you could have an AUTOLOAD sub which just does nothing, to avoid having to test if calling any method would croak.
In Section
Seekers of Perl Wisdom
|
|