Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Migrate your perl project to Moose

by tospo (Hermit)
on Oct 17, 2011 at 10:28 UTC ( #931879=note: print w/replies, xml ) Need Help??

in reply to Migrate your perl project to Moose

If you have been programming in object-oriented style previously then the move to Moose is not too difficult. The main thing to look for are all those attribute read/write boilerplate codes in the old classes, which might look something like this
sub name { my $self = shift; my $new_value = shift; $self->{_name} = $new_value if defined $new_value; return $self->{_name}; }
and translate to Moose code like this:
has 'name' => ( is => 'rw', isa => 'Str', );
So, first identify attributes of the class which store data and convert them to Moose using the "has" declaration. You will be left with methods in the original code that perform some action on the attributes and you can leave them mostly untouched except that you want to make sure you don't access the underlying blessed hash of attributes directly, so you want to change (in my example) all occurrences of $self->{_name} to $self->name throughout the code.
This will get you a long way in converting your code to Moose style. It's all explained in detail in the Moose tutorials, start here. This one is quite helpful to quickly understand the differences between Moose and non-Moose OO code. To really make the most of Moose you should then look into things like Moose Roles etc, but for now this should get you going.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://931879]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2021-05-12 09:07 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (126 votes). Check out past polls.