Please add chaining mutators
leonerd opened this issue · comments
It's a common form to want to return
a newly-constructed object with all the values set into it already, by having mutator methods that return the invocant, so you can easily add more. In such a style you could write:
return HTTP::Response->new( 200 )
->set_content_type( "text/plain" )
->set_content( "Hello, world" );
As it currently stands, the existing mutator methods like ->header
, ->content
, etc.. do not return the invocant, thus such a style is impossible and one must use a temporary:
my $resp = HTTP::Response->new( 200 );
$resp->content_type( "text/plain" );
$resp->content( "Hello, world" );
return $resp;
Note you can also set all these things at once through the constructor -- e.g. $r = HTTP::Response->new( $code, $msg, $header, $content )
I fully agree on a approach that @leonerd describes, a very common way to do things in other languages. Bu I would strongly disagree with set_*
mutators, because of 'inmutability'. I'd rather would go for with_*
that would create a clone, with the new setting.