http://www.perlmonks.org?node_id=933225


in reply to Re^3: How Large Does Your Project Have To Be to Justify Using Moose? (modular)
in thread How Large Does Your Project Have To Be to Justify Using Moose?

Even if you don't have Moose generate accessors, then it generates a constructor that still exposes the attributes.

By default, yes it does. But it is easily remedied.

has 'foo' => ( is => 'bare', # no accessors init_arg => undef, # no longer accessible via constructor );

And if you don't generate accessors then you can't make use of many roles.

Not sure what that even means because roles and accessor generation have nothing to do with one another. Care to clarify this?

I'm starting to realize that there are people (I can't tell how many / how common yet) that can't even really conceive of designing a class without jumping straight to building a list of attributes, even after you explain to them that they should design the interface before designing the attributes (which are part of the implementation). Moose certainly encourages that mindset.

Again, I really fail to see how Moose encourages this. Just because Moose makes it easy to build attributes, that in no way forces not think about the interface first. Moose is just a tool to help you write your classes, it is still your responsibility to actually design the classes.

# One reasonable constructor interface for a square: Square->new( center => [ $x, $y ], width => $w );
But I'd likely implement the square as a list of 4 x/y coordinates. And I'm not sure how I want to store the coordinates (as pairs or as parallel arrays or ?). I don't even think too much about how I'm going to implement the square's data when I'm defining the interface for the square.

So, again, Moose doesn't prevent you from doing this.

package Square; use Moose; has 'top_left' => ( init_arg => undef, accessor => '_top_left' ); has 'top_right' => ( init_arg => undef, accessor => '_top_right' ); has 'bottom_left' => ( init_arg => undef, accessor => '_bottom_left' ) +; has 'bottom_right' => ( init_arg => undef, accessor => '_bottom_right' + ); sub BUILD { my ($self, $params) = @_; my $center = $params->{center}; my $width = $params->{width}; $self->_construct_coordinates_from_center_and_width( $center, $wid +th ); }
What this buys you over doing it by hand is basically automated (private) accessor construction and proper constructor initialization for any subclasses (via BUILD). It is also pretty concise and (with the exception of init_arg => undef which is a little odd) is pretty easy to understand (once you know Moose of course).

-stvn