Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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!

Replies are listed 'Best First'.
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.

        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.


        You are my new hero.

        I was at the 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.


        -- A Mse once bit my sister
Re: Moose is lovely
by Your Mother (Archbishop) 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.

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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2021-07-26 00:49 GMT
Find Nodes?
    Voting Booth?

    No recent polls found