
Introduction to Moose, Perl’s OO system - lelf
http://www.houseabsolute.com/presentations/intro-moose-class/
======
MichaelBurge
Moose is a killer library for Perl. I really miss roles when working in other
languages. I often feel like I'm fighting the language if I can't split out
some common program logic into a separate module and compose it in.

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.

~~~
csirac2
I'm glad I'm not the only one! Explaining why I'd possibly miss anything from
the Perl world can sometimes be like pulling teeth.

~~~
TheDauthi
I'm a heavy Rubyist these days, and Moose is one of the very few things I miss
from the realm of Perl.

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).

~~~
rurounijones
[https://github.com/peczenyj/MooseX](https://github.com/peczenyj/MooseX) may
be of interest to you then, there was a reddit post about it a few days ago.

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.

------
swuecho
The talk is great. For those who do not have the patience, this is the doc of
Moose [https://metacpan.org/pod/Moose](https://metacpan.org/pod/Moose)

This is the Moose Cookbook
[https://metacpan.org/pod/Moose::Cookbook](https://metacpan.org/pod/Moose::Cookbook)

I think this is pretty much everything you need to start using Moose.

------
autarch
I should point out that these are slides from a full day class where people
also do a lot of exercises. You can see the whole thing at
[https://github.com/moose/intro-to-moose](https://github.com/moose/intro-to-
moose)

------
lightblade
The JavaScript library Joose was inspired by moose.

[http://joose.it](http://joose.it)

------
lelf
Notice that they are all just libraries, dedicated changes in Perl’s core: 0.

~~~
peteretep
I see that as a feature, honestly; what other languages have an OO system so
massively flexible that you can easily build stuff like this on top of it?

Perl's OO system is often derided as "bolted-on", but once you thoroughly
understand `bless` and `->`, your imagination is the limit.

~~~
lispm
See the Common Lisp Object System (CLOS) and its Meta-Object Protocol (MOP).

~~~
kbenson
Moose is back-ported Perl6 objects, which borrows heavily from CLOS and the
concepts introduced in the Smalltalk traits paper. This is a bit of a
simplification of the history, but the ancestry is present, documented and
obvious.

------
hernan604
[https://github.com/hernan604/Tutorial-Joose-JS-
PT](https://github.com/hernan604/Tutorial-Joose-JS-PT)

Joose js tutorial in portuguese =)

Its possible to have all the moose power in javascript

------
mkadlec
Why not just use Ruby, all of this is already built in?

~~~
RVuRnvbM2e
This was my first thought too. Can anyone elaborate on features Moose has that
Ruby doesn't?

~~~
csirac2
Did you read the linked article? It's not the prettiest intro I've ever seen,
but for perl users its benefits can be seen on
[http://www.houseabsolute.com/presentations/intro-moose-
class...](http://www.houseabsolute.com/presentations/intro-moose-
class/#slide27) vs [http://www.houseabsolute.com/presentations/intro-moose-
class...](http://www.houseabsolute.com/presentations/intro-moose-
class/#slide28)

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.

~~~
RVuRnvbM2e
Do you mean the optional type enforcement? Admittedly it's not built-in to
ruby, but it's easy to replicate.. and a lot less verbose:

    
    
      # 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" # ArgumentError

~~~
csirac2
Sure. And just for attributes, there's also delegation, read-only attributes,
builders, lazy init, roles, modifiers which are enforced at construction time
(if `required` is true).

Moose 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](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' );

------
tmaly
Great presentation. I have not used Moose much, but I have started using a
lighter weight version of it called Moo.

------
victormx
¿it's hard to put a next/previous button?

~~~
esaym
bottom right if you have js enabled...

~~~
ben0x539
If you have js disabled, you get the whole thing as a normal document with a
scrollbar.

