Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Mostly it makes it difficult to catch errors if those methods fail.

But not any more difficult that catching an error in *any* chaining call. The OP had no problem with:
my $label_formatting = $button->child->font_style;
but if there's an error in the child method, it'll be as hard to catch as in:
$window->title('foo')->border(20);

And if your methods just throw exceptions on errors, you can just wrap it all in an eval:

eval {$window->title('foo')->border(20)}; if ($@) {...do something...}
If that isn't good enough for you, because you need fine grained control on catching errors, you can always write:
eval {$window->title('foo')}; if ($@) {....} else {eval {$window->border(20)} if ($@) {....}}
The point is that mutators returning $self allow for either style. Chaining for those who want it, and fine control for those who want that.

Returning the argument passed in is IMO not very useful. The OP mentioned "that's how = does it - it allows you to write $foo = $bar = $baz;. That's true, but the OO equivalent would be:

$obj->foo($obj->bar($baz))
which, IMO, gets unwieldy very quickly and isn't very readable either. I also have seldomly any use for that.

In reply to Re^4: Mutator chaining considered harmful by Anonymous Monk
in thread Mutator chaining considered harmful by Aristotle

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found