Notes on porting to p5-mop
Here are a few things I observed while doing the port, which I'm glad to say, was a very easy and worthwhile thing to do (if at least to help me think about improving my own use of Perl:)
- Porting wasn't hard, especially coming from Moo*: when I started writing my module, I had just read the new perlootut from perldoc, and decided that my new OO modules from now on would be made using Moo. If p5-mop gets into core, I'll probably decide later to switch to it fully, for stuff that target perl v5.20+
- Learning p5-mop isn't hard: the docs are still mostly unwritten (and I feel this will be fixed soon,) but the tests seem solid enough to start getting an idea of how p5-mop works. Furthermore, the fine people in #p5-mop can help clear up stuff along the way. Indeed, part of the reason for this exercise is also to get some stuff down to writing, like this post, and contribute it later to the official documentation.
No need forPut a
package main;line in a one-class module;
if you have a simple top-level class name like this module, you can get away with not adding arecent versions of p5-mop now qualify classes as packages. Edit: doy on #p5-mop suggests putting a package line nevertheless, to ensure that the class doesn't end up being appended to some other namespace when the module is
package MyModule;line and rely on the
class MyModuleinstead, as it seems
used. Thanks for clarifying!
- There's no
BUILDARGS, but you can override
new(): this seems to run contrary to Moose::Manual::BestPractices, but then again, p5-mop isn't Moo*.
- Attributes are private to a class; overriding default values for attributes in subclasses, a la
has '+foo'doesn't work as you might expect. As Stevan and others said on IRC, prefer to have attributes with the
buildertrait, or if your attribute is a non-reference/simple scalar, just override
newin your subclass, calling
$class->next::methodwith the new defaults.
metawhen you need it; the "m" in "mop" is something I haven't really had much time using (especially since I worked with Moo first, which did not have any metaprogramming facilities at all,) so I feel I want to read up more on other modules that make use of it extensively.
- Twigils! This is just freshly added to p5-mop-redux from Perl 6 (just as p5-mop-redux itself is looking to be like Perl 6 classes.) The initial implementation doesn't seem to support @!array or similar yet, just sticking to simple $!scalar variables, but it looks promising.
- It is all a work in progress; both my own module and p5-mop are still early in their implementation, which is probably a contributing factor for my ease of doing the port. Furthermore, porting showed me some things along the way that I may probably change in my module later, like handling subclassing issues and the like.
In the end, I actually got a fewer lines of code (as the old Moo code forced me to have quick lexical copies of accessors in order to stick to its encapsulation.) Granted, it does look less than the classical plain Perl 5 or Moo* class, but it does look like a good start for a MOP system that could be included in future Perl core.