Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Moose is lovely

by revdiablo (Prior)
on Feb 29, 2008 at 01:08 UTC ( #671069=perlmeditation: print w/ replies, xml ) Need Help??

Hello friends,

I know most of you have probably heard of Moose already. Many of you have probably even used it. I am certainly not one to claim I'm the first on any given block. But, after finally bucking up and building something with it, I felt a burning itch (maybe I should get that checked out?) to share my enthusiasm.

I wrote an interface to access data from a website. I do this a lot, but the busywork involved in making even a very simple OO API usually prevents me from bothering. Mostly, I write a couple of subroutines to abstract the various WWW::Mechanize and HTML::TreeBuilder bits, build some plain data structures, and hack things up and down the stack.

On a lark, I thought I might want to do it a little differently this time. I installed Moose, went through the docs a bit, and discovered the development model was surprisingly fun. Even while learning how to prod things into the proper shape.

The beginning was crude and simple. I defined the attributes of each object with has calls, and the object interrelationships with isa declarations. The basic data model took very little time to get right. I quickly had a program that pretended it was reading my LibraryThing library.

Then I wrote default hooks that fetched the appropriate data from the site. It kind of snuck on me, but then I realized that's all I had to do. Once I had the loading code in place, it was done. I had a functional API before I even knew it. It was clear and straightforward. Even better, since the data-fetching methods were all defined with lazy turned on, the fetching doesn't happen until the data's needed.

There is a lot of functionality that comes from Moose's simple, declarative interface. All I could think of was the infamous tagline from a Ruby on Rails promotional video: look at all the things I'm not doing! I gotta say, Moose; I like it, I really do.

Three cheers for stvn!

Comment on Moose is lovely
Select or Download Code
Re: Moose is lovely
by ysth (Canon) on Feb 29, 2008 at 01:49 UTC

      Sure! I've omitted the _fetch routines here, because they're not particularly relevant to the Moose portions of the code, and the size would be a bit unmanageable.

        You are my new hero.

        I was at the to.pm meeting last night and I asked "has anyone used Moose?" The answer was no but talexb suggest I post to perlmonks to see if anyone knew of some real-world examples on CPAN. So here I am and here this is ready and waiting for me!

        I admit, I don't quite grok it despite looking at merlyn's articles on the subject. But something keeps telling me "this is good stuff, you should learn it".

        Methinks you are a little psychic. My thanks.

        --
        meraxes

        -- A Mse once bit my sister

        Very nice, I am glad you are enjoying Moose :)

        Just one suggestion, if your "default" subs get large, it is usualy a good idea to convert them to 'builder' methods instead. You can find this feature documented in Class::MOP::Attribute. This:

        has 'foo' => ( builder => 'build_foo' );
        is basically a shortcut for:
        has 'foo' => ( default => sub { (shift)->build_foo } );
        It also gives the added benefit that any subclasses can easily override 'build_foo' themselves to get additional behavior.

        -stvn
Re: Moose is lovely
by Anonymous Monk on Feb 29, 2008 at 04:19 UTC
    June wedding?

      I would prefer a spring wedding, but to each his own.

Re: Moose is lovely
by Your Mother (Canon) on Feb 29, 2008 at 21:24 UTC

    Thank you very much for posting the experience and the code. Moose is something I keep hovering over without clicking on, to use a modern metaphor. Think you just tipped me.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://671069]
Approved by ikegami
Front-paged by nothingmuch
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-12-19 23:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (94 votes), past polls