Indeed Moose Roles can do this kind of thing easily.

First we start with your basic "TextClass" class:

package TextClass; use Moose; has 'text' => ( is => 'rw', isa => 'Str', ); with 'PrettyPrint'; no Moose; __PACKAGE__->meta->make_immutable; 1;
Note the with 'PrettyPrint'; That is the module which contains the role we will add.

We make another module to implement the "prettyprint" role:

package PrettyPrint; use Moose::Role; requires 'text'; sub prettyprint { my $self = shift; print "So handsome:\n\t", $self->text, "\nYeah, sure!\n"; $self; } 1;
The prettyprint sub from this module will now become a method in your 'TextClass' class or any other class that provides a text method. That is what the requires 'text' takes care of. Use this role in another class that doesn't provide the text method and it will complain and end the compilation of your script.

As you see, the methods in your Role-module have access to all the public methods of your object, even those which are provided auto-magically by Moose itself, such as the getters and setters, in this case the text method.

Using all this now is standard:

use Modern::Perl qw/2014/; use lib 'D:/Perl/scripts'; # or wherever you have saved the modules use TextClass; my $person = TextClass->new( text => q/I'm CountZero/ ); $person->prettyprint;
Output:
So handsome: I'm CountZero Yeah, sure!
Note, that a Role isn't a class: it cannot be instantiated on its own, it will not provide its own objects. Rather it is a bunch of subroutines that get directly included in your basic class without any intermediary object. So you do not do $text->PrettyPrint::prettyprint or $text->PrettyPrintObject->prettyprint

Update: clarified about adding a Role module.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

In reply to Re: Moose attribute modification by CountZero
in thread Moose attribute modification by learning.moose

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.