Introduction to Moose, Perl’s OO system
houseabsolute.com
houseabsolute.com
I can get by in C++(using multiple inheritance and the CRTP), and Haskell(with typeclasses).
D has some interesting features - like template mixins - that look promising, but I haven't been gutsy enough to use it for any new project.
Here's a fun game: the next time someone asks you what technologies you work with or languages you prefer, somehow work in that you like Perl, at least Modern Perl with Moose. Count which one occurs most frequently: people backing away like you've begun frothing at the mouth, people asking if you've ever heard of ANOTHER_TECHNOLOGY (usually Ruby, sometimes Python), and people who assume you're a sysadmin who occasionally scripts something (they'll ask if you've ever used Puppet or Chef).
If you were to 'speak' Perl verbatim, you probably would start frothing at the mouth (from exertion, not from a deadly disease).
To be honest, as a ruby person, I don't really see what benefits it brings to ruby but maybe I am just missing something.
use Something\CoolTrait;
inside of a class is just great!What sort of functionality do you use traits for?
You've just implemented traits.
A class with a trait/role can be queried for that interface:
smile() if $my_object->does('SomeTrait');
Methods provided by a trait/role have direct access to $self.Also, Moose gives you the ability to apply traits to classes that are not predefined with those traits:
use Moose::Util;
my $instance = Moose::Util::with_traits('MooseClass', 'MyTrait')->new(%params);
You can parameterize roles (as in, pass parameters that affect what the role provides). If you put some method modifiers into your roles, you can enhance existing functionality; for example, you can add logging, memoization, and persistence, all without the class having those features beforehand.In other words, roles/traits make your OOP functionality much more composable.
This is the Moose Cookbook https://metacpan.org/pod/Moose::Cookbook
I think this is pretty much everything you need to start using Moose.
Perl's OO system is often derided as "bolted-on", but once you thoroughly understand `bless` and `->`, your imagination is the limit.
Lisp
...sorry I couldn't help myself
Joose js tutorial in portuguese =)
Its possible to have all the moose power in javascript
I really miss Moose. Pythonistas generally can't believe I could possibly miss anything from the Perl world, and usually aren't interested in hearing my reasons why.
But honestly - I miss doing 75% of my work in bashing out an declarative specification with Moose, especially with the type system (as slow as that can be at runtime; the work-around is making objects immutable which leads to cleaner code anyway).
Python does have the traits package by enthought, which has some vaguely similar stuff in it but nobody seems to use it. Being out of the ruby loop for a while, what's ruby's answer to Moose?
Python patterns are still solid today, so I gladly choose doing things the Pythonic way over the Perl way. Not to mention the meta reasons to use a language (community, libraries, ecosystem).
Say what now? Moose draws from most heavily from CLOS and Smalltalk, as filtered through Perl 6. I don't know of any Java influence in Moose at all.
Moose is the opposite of Java: it has a type system which actually does work for you and brings you amazing helpful stuff for free, whereas Java makes you go through rituals and pain to use a type-system that mostly only ever gets in your way.
Ruby code wouldn't be as verbose as this, but still once you're used to building classes with Moose accessors, types, traits etc. in a mostly declarative manner, going back to hand-rolling checks and exceptions on bad attribute values, not to mention accessor methods and so on definitely feels like a step backwards.
In fact now that I'm doing a lot of Python these days I've come to the horrible realization that it's Moose which has made me yearn for a language with stronger emphasis on typing and correctness than Python can provide!
Edit: and I don't mean "like java", where it only ever seems to get in your way...
Moose does useful things and gives you stuff "for free" once you've told it what
type something should be. And allows you trivially inherit/override type
declarations, rather than jumping through hoops as in Java.
It's a full about-face compared to the liberation I felt going from C/C++/Java to Ruby back around 2006-2007.Slowly working through Learn you a Haskell, but doubt I'll get a chance to use Haskell professionally.
# Implementation:
module TypedAttrs
def attr_accessor_type name, type
define_method name do
instance_variable_get "@#{name}"
end
define_method "#{name}=" do |value|
raise ArgumentError unless value.is_a? type
instance_variable_set "@#{name}", value
end
end
end
# Example:
class Foo
extend TypedAttrs
attr_accessor_type :bar, Integer
end
a = Foo.new
a.bar = 5
p a.bar
a.bar = "hi" # ArgumentErrorMoose isn't hard to implement, it's actually had quite a few alternate implementations even in Perl. Python has enthought's traits package, and I've just been pointed to https://github.com/frasertweedale/elk as well.
BTW it sucks to nit-pick, but the Moose version isn't any more verbose than yours:
package Foo;
use Moose;
has 'bar' => ( isa => 'Integer' );¹in fact, under the hood, Ruby modules are classes, and mixins are multiple inheritance— it's an artificial, deliberate limitation.
This is all achievable in Ruby of course, the Moose docs sort of codify it and provide sugar to make these patterns the path of least resistance.
Most cases of multiple inheritence I've seen is in non-Moose Perl code, where it's being used a little like a mixin. DBI classes and code making lots of use of meta-programming (Class::MOP stuff).
has foo => (is => 'rw', required => 1);
has bar => (is => 'ro', lazy => 1, builder => '_build_bar');
sets up both accessors and the relevant constructor logic - and bar's accessor will call the _build_bar method on self to get the value if it wasn't provided to the constructor.Method modifiers (before/after/around) are way more elegant than rename-the-method, and compose nicely from roles - and roles are substantially more powerful than mixins.
Personally the reason to use Perl over Ruby is the community. I gel a lot better with the members of the Perl community I've met compared to when I was poking my head around Ruby. I don't mean this in a bad/flamy way. No one was "mean" to me or anything. My brain is just damaged in the Perl kind of way rather than the Ruby kind of way.
Anyways, the actual question shouldn't be why Ruby over Perl, rather it should be "why don't we have Moose (or something like it) in every language." There's nothing about it that ties it to Perl. A dynamic runtime isn't a bad thing to have but I can't really see why most of the really nice parts couldn't be implemented in other languages at compile time.
Simply put, Moose is awesome. You can build so much functionality with not a lot of code and it actually glues together well. Type coercions are awesome and super useful in day to day programming. I've been doing a lot of API work where I need flexibility in terms of output (JSON, XML, RSS, etc) where a plain object dump to the specified format isn't enough. In the case of the XML there is an XSD it needs to match against and the structure isn't particularly amicable to sane JSON output.
Care of Roles, Type coercions, Attribute meta roles, and some preemptive thinking I basically just create objects with attributes and a few methods containing business logic and not a lick of serializer logic.
package My::Foo;
use MyCo::Moose; #Exports a few things I usually need like aliasing,
use My::Type::Exports qw(:all);
with 'My::Role::AwesomeSerializer';
has 'some_attribute' => (
is => 'ro',
isa => MaybeSuperComplexObjectType,
coerce => 1,
traits => ['UseMyJsonSerializer'],
json_name => 'someAttribute',
xml_name => 'My::SomeAttribute',
alias => 'some_call_it_this',
);
# yatta
__PACKAGE__->meta->make_immutable();
1;
In my types package I have various coerce methods that accept different data formats. The logic in the class doesn't have to change, nor do I need to write a new constructor/factory like I would in other languages, when the data I'm being given changes in some non-trivial way.[1] http://pacman.blog.br/blog/2014/02/07/moosex-a-new-ruby-dsl-...