Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^4: How Large Does Your Project Have To Be to Justify Using Moose? (modular)

by stvn (Monsignor)
on Oct 23, 2011 at 18:05 UTC ( #933225=note: print w/ replies, xml ) Need Help??


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


Comment on Re^4: How Large Does Your Project Have To Be to Justify Using Moose? (modular)
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-10-31 04:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (214 votes), past polls