Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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.


Comment on Re: Migrate your perl project to Moose
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (15)
As of 2014-10-21 12:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (103 votes), past polls