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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|