The CPAN Book - Chapter 2
Just after Sawyer X gave a quick review on my chapter 2, on Modern OO programming, I am making the second chapter PDF file available on the book website.
Before leaving you reading the document, I would like to make it clear that i really want to make this chapter this way, presenting first Moo, then Mouse, and finally Moose. I know that the order is not cronological. It is just the order of presenting, each time, a more complex module. I didn't know Moo, Mouse or Moose. I had to learn them all from scratch, and I did it in this order. And I liked it.
Other than that, please, comment and suggest any correction you find relevant.
Alberto,
Looks good so far, I have a few comments.
To start with I don't think it is fair to say use of Moose was originally discouraged due to slowness. Moose has always had a large compile time hit, which caused it to be slow at startup, but it was always fast at runtime (proportional with the amount of features you used). This is an important distinction to make, because in persistent environments Moose startup has rarely been viewed as a problem. Only in CGI scripts (which I question being included in a book on Modern Perl, but that is another argument entirely) and in command line applications, has Moose startup been a problem.
Next, in your Moo inheritance example, I think you missed the "use Moo;" at the top. This is required since Moo is what exports the 'extends' and 'has' keywords.
Additionally, in the Moo role application example, I think you want to put the "with 'Dumper::Role';" inside the Person class definition. I do not understand how it would work with the code example you are showing.
In the Moo method modifier section, I think it is unwise to suggest you can change the values of @_ in a 'before' or 'after' method modifier. In order to do this, you must alter @_ itself, which is ill advised. I would leave that out entirely. If you need to change the contents of @_ it is recommended that you use 'around' instead.
Next, in the Mouse section, you mention that you can store values in 'default' but that you "should be careful about references". Mouse should (because Moose does) complain loudly if you attempt to store anything other then a CODE reference or a non-reference value (string or number) in 'default'. I suggest that you either explain why this is, or you remove that entirely, what you have there will only serve to confuse.
In the "Inheritance and Roles" section you say "Note that when redefining an attribute in a subclass you need to prepend a plus sign to the name of the attribute begin redefined". This is not true, you can simply redefine the entire attribute if you want, the "+" is only useful when you want to use the superclass version of the attribute as a "base" for your new attribute.
Later in that chapter, you also say "As a final note, Mouse supports more than one role at a time. Just take care about the order you use them. It might be relevant." Actually, that is not exactly true and somewhat misleading. The order of roles should not be relevant, that is the whole point of roles. Only when you use method modifiers in roles does the order become relevant, but in all other cases Roles will combine in an order agnostic way.
And lastly, the Moose section seems a little sparse. I think a discussion on attribute traits would be a good way to illustrate where Moose provides something more over Mouse and Moo. It is a very powerful feature and is used in a lot of MooseX:: extensions. Probably the best, most digestable, example would be MooseX::Clone.
All in all, I think this is a good start. I think a few larger examples would be helpful, as well as showing things like MooseX::NonMoose which allow Moose to play well with traditional Perl OO.
- Stevan
Hello, Stevan.
Thanks for the long comments. Note that some remarks I do in my text, like the order of the roles to be relevant, were copied from Moose manual page, or the change of @_ on Moo was copied from Moo manual page, etc. If they are not correct, probably a good idea is to start by fixing them there :)
Hope to get a new version soon taking care of your comments. Thanks :)
Alberto,
RE: copying from the Moo manual page.
You should know better then to listen to what mst says without first checking with someone more sane ;)
- Stevan
Stevan, I think I took in consideration all your comments (just the @_ comment was to footnote, instead of just removing it).
Also, didn't write about traits yet. First need to learn about them. :P and reflect if I think they are easy to explain in less than a page :)
Thank you, again :)