Here's an example using my latest plaything, MooX::Pression. Raku-like syntax, but it's Perl 5.
use v5.14;
use strict;
use warnings;
package Local {
use MooX::Pression;
class Employee {
has name (type => Str, required => true);
has title (type => Str, required => true);
method name_and_title () {
my $name = $self->name;
my $title = $self->title;
return "$name, $title";
}
}
class Employee::Former {
extends Employee;
factory former_employee;
has +title = "Team Member";
around name_and_title () {
my $old = $self->$next(@_);
return "$old (Former)";
}
}
}
my $peon = Local->new_employee(
name => 'William Toady',
title => 'Associate Assistant',
);
say $peon->name_and_title;
my $ex_peon = Local->former_employee(
name => 'William Toady',
title => 'Associate Assistant',
);
say $ex_peon->name_and_title;
my $ex_peon2 = Local->former_employee(
name => 'William Toady',
);
say $ex_peon2->name_and_title;
__END__
William Toady, Associate Assistant
William Toady, Associate Assistant (Former)
William Toady, Team Member (Former)
Of course, this uses Moo instead of Moose. If you prefer Moose...
use MooX::Pression toolkit => "Moose";
-
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.