|
NAMEMoos - Moo s{imple,peedy,ingle}SYNOPSISpackage Foos; use Moos; extends 'Boos'; with 'Cloos'; has this => (); has that => 42; has other => ( builder => 'build_other', lazy => 1, ); sub BUILD { my $self = shift; # build, build, build } sub BUILDARGS { my ($self, @args) = @_; # munge, munge, munge return {%munged_args}; } DESCRIPTIONMoos completes the M to Moose sequence of Perl OO modules.This one is pure Perl, single file and mostly Moose compatible (for what it does). Moos has no non-core dependencies, but certain features (roles, debugging functions, legacy Perl support) do require additional modules. If you steer away from those features, you don't need those additional modules. FEATURESHere's a quick list of the Moose compatible features that are supported by Moos.strict/warningsTurns on "strict" and "warnings" for you.Helpful exportsThe ever useful "blessed" (from Scalar::Util) and "confess" (from Carp) are exported to your namespace.extendsFor inheritance. "Moos::Object" is the default base class.package MyClass; extends 'MyBaseClass'; Supports multiple inheritance, by allowing multiple classes on a single invocation. withMoos can consume roles using the "with" keyword. Using this feature requires Role::Tiny to be installed.with 'ThisClass', 'ThatClass'; hasAccessor generator. Supports the "is", "default", "build", "lazy", "clearer", "predicate", "required", "handles" and "trigger" options, described below. The supported options are about the same as Moose. Other arguments (e.g. "isa" and "coerce") are currently ignored.has this => (); NOTE: Class::XSAccessor will be used for simple accessors if it is installed. This can be disabled by setting $Moos::CAN_HAZ_XS to false or by setting the PERL_MOOS_XS_DISABLE to true.
Class and Object Methods
RolesIf you need roles, then Moos classes have experimental support for Role::Tiny, Moo::Role and Moose::Role roles. (Moos provides a "with" command that uses Role::Tiny to do the work.){ package Local::Class; use Moos; with "Local::Role"; ...; } Limitations: Note that Moo and Moose each allow type constraints for attributes; Moos does not. This means that if you compose, say, a Moose::Role into a Moos class, you end up with a strange situation where the accessor methods will enforce type constraints (because they were generated by Moose) but the constructor will not (because it is inherited from Moos::Object). See also Moos::Role. Method ModifiersIf you need method modifiers, then try Class::Method::Modifiers.Development OptionsMoos has a couple of builtin dev options. They are controlled by environment variables.
WHENCE MOOSI(ngy) created Moos during Pegex development. Pegex uses a clone of Moos called Pegex::Base. (Moos ships with a commandline utility called "remoos" that does this cloning.)Pegex is a parser framework and needs to be fast. While looking into speed issues I noted that accessor calling was the biggest hit. I tried all the various Mo* solutions and Mouse was the fastest. I was happy until I remembered that Mouse uses XS, and for various reasons this broke my toolchain (TestML, Module::Install, etc). So I made a single module/file Moose clone and it worked out well. I've shared Pegex::Base as Moos in case any other projects want it. Later on, Toby Inkster added a bunch of low-cost but very handy features from Moose. The name Moos was chosen because it was the only name left between M and Moose. (Thus adding to the epic confusion that we embrace as Perl Mongers! :) ON SPEEDIn the end, I got Pegex to run even faster with Moos than it originally did with Mouse. I'll tell you my secret...Accessors (usually) do not need to be method calls. Replace these: my $foo = $self->foo; $self->foo($foo); with: my $foo = $self->{foo}; $self->{foo} = $foo; And your code will be faster (and a bit uglier). The only time that you need to call an accessor method is when you are accessing a property and it might invoke a "lazy" "builder", "default" or "trigger" method. Otherwise you are just wasting time. At least with the minimal feature set offered by Moos. The PERL_MOOS_ACCESSOR_CALLS feature described above is for finding these method calls. Note that third parties can still use your module's accessor methods like they would expect to. I'm sure I've missed some subtleties, and would be glad to hear opinions, but in the meantime I'm happy that my code is faster and pure Perl. SEE ALSO
AUTHORSIngy döt Net <ingy@cpan.org>Toby Inkster <tobyink@cpan.org> COPYRIGHT AND LICENSECopyright 2012-2014. Ingy döt Net.This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See <http://www.perl.com/perl/misc/Artistic.html>
Visit the GSP FreeBSD Man Page Interface. |