Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Moose: return null object

by bart (Canon)
on Apr 18, 2011 at 15:27 UTC ( [id://899960]=note: print w/replies, xml ) Need Help??


in reply to Moose: return null object

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://899960]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-18 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found