Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: chaining method calls

by grantm (Parson)
on Jun 12, 2003 at 10:07 UTC ( #265321=note: print w/ replies, xml ) Need Help??


in reply to chaining method calls

Colour me unconvinced too.

I do find the idea of a set-property-method returning a reference to the object itself unintuitive and confusing.

I've seen this suggested as a way to initialise a bunch of properties at object creation time. A far more common and readable approach is to pass the constructor a series of name => value pairs. That doesn't preclude the constructor calling relevant mutator methods to store those values if necessary.

Also, as a general style for accessors, I prefer the common Perl idiom of a getter/setter method which always returns the property value. Rather than having separate methods for get and set. Users of certain other languages may sneer at this, but that's probably just sour grapes :-)

Having said all that, I have found it useful sometimes for a method to return the object reference. Just recently I had an object wrapping a DBI statement handle. I needed a method that caused the object to fetch and store the data internally. It just seemed natural for this method to return a reference to the original object so I could do this:

my $recordset = $self->db_query("select * from sometable")->prefetch;

instead of:

my $recordset = $self->db_query("select * from sometable"); $recordset->prefetch;

The difference (at least in my mind) was that although the prefetch method was mutating the object, it wasn't a simple property setter and it would not otherwise have returned a value (it would raise an exception on error).


Comment on Re: chaining method calls
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (16)
As of 2014-08-27 18:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (248 votes), past polls