Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.

In reply to Re: Migrate your perl project to Moose by tospo
in thread Migrate your perl project to Moose by py_201

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    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: (4)
    As of 2014-10-01 01:41 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (386 votes), past polls