Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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).

In reply to Re: chaining method calls by grantm
in thread chaining method calls by perrin

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (7)
    As of 2018-06-24 22:55 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.