Moose
Delegations
Alternatively, you could utilize Moose's delegation feature when you set up the fired_dt attribute, like this:
has 'fired_dt' => (
is => 'rw',
isa => 'DateTime',
handles => {
last_fired => 'datetime'
}
);
This will set up another accessor method named last_fired and map it to
the datetime method of the attribute. This makes the invocations of
$self->last_fired and
$self->fired_dt->datetime equivalent. This is
worthwhile because it allows you to keep your API simpler.
Types
Moose provides its own type system for enforcing constraints on the value to which an attribute can be set. As I mentioned earlier, type constraints are set with the isa attribute option.
Moose provides a built-in hierarchy of named types for general-purpose use.
For example, Int is a subtype of Num, and Num is a subtype of Str.
The value 'foo' would pass Str but not Num or Int; 3.4 would pass Str and
Num but not Int, and 7 would pass all of Str, Num and Int.
There also are certain built-in types that can be
"parameterized", such as
ArrayRef (a reference to an array). This lets you not only require an
attribute to contain an ArrayRef, but also set type constraints on the
values that ArrayRef can contain. For example, setting isa =>
'ArrayRef[Int]' requires an ArrayRef of Ints. These can be nested multiple
levels deep, such as 'ArrayRef[HashRef[Str]]' and so on.
Another special parameterized type is Maybe, which allows a value to be
undef. For example, 'Maybe[Num]' means the value is either undef or a Num.
You also can use type "unions". For example,
'Bool | Ref' means either Bool
or Ref.
If the built-in types aren't sufficient for your needs, you can define your own subtypes to do any kind of custom validation you want. The Moose::Util::TypeConstraints documentation provides details on creating subtypes, as well as a complete listing of the built-in types that are available (see Resources).
Finally, instead of specifying the name of a defined type, you can specify
a class name, which will require an object of that class type (such as in
our DateTime attribute example). All of these concepts can be intermixed
for maximum flexibility. So, for example, if you set isa =>
'ArrayRef[MyApp::Rifle]', it would require an ArrayRef of MyApp::Rifle
objects.
Inheritance
Subclassing is relatively painless in Moose. Use the extends function to make a class a subclass of another. The subclass inherits all the parent's methods and attributes, and then you can define new ones or override existing ones simply by defining them again.
Moose also provides helpful attribute inheritance sugar that allows you to inherit an attribute from the parent, but override specific options in the subclass. To tell Moose to do this, prepend the attribute name with a plus sign (+) in a "has" declaration in the subclass. (Note: attribute options related to accessor method names cannot be changed using this technique.)
For example, you could create a new class named MyApp::AutomaticRifle that inherits from the MyApp::Rifle class from the previous example:
<![CDATA[
package MyApp::AutomaticRifle;
use Moose;
extends 'MyApp::Rifle';
has '+rounds' => ( default => 50 );
has 'last_burst_num' => ( is => 'rw', isa => 'Int' );
sub burst_fire {
my ($self, $num) = @_;
$self->last_burst_num($num);
for (my $i=0; $i<$num; $i++) {
$self->fire;
}
}
1;
]]>
Here, MyApp::AutomaticRifle can do everything MyApp::Rifle can do, but it also can "burst_fire". Also, the default of the rounds attribute has been changed to 50 in AutomaticRifle, but the rest of the options for the rounds attribute still are inherited from the parent Rifle class.
You might use MyApp::AutomaticRifle like this:
use strict;
use MyApp::AutomaticRifle;
my $rifle = MyApp::AutomaticRifle->new;
print "There are " . $rifle->rounds . " rounds in the rifle\n";
$rifle->burst_fire(35);
print "Now there are " . $rifle->rounds . " rounds in the rifle\n";
Although Moose automatically sets up the "new" constructor for you, there still are times when you need to execute custom code at construction. If you need to do that, define a method named BUILD, and it will be called immediately after the object has been constructed. Don't create a "new" method; that will interfere with Moose's operation.
BUILD is also special as it relates to inheritance. Unlike normal methods that override the parents' methods when redefined in subclasses, BUILD can be defined in every class in the inheritance tree and every one will be called, in order from parent to child.
Roles
Roles define some set of behaviors (attributes and methods) without being full-blown classes themselves (capable of instantiation as objects directly). Instead, Roles are "composed" into other classes, applying the defined behaviors to those classes. Roles are conceptually similar to "mixins" in Ruby.
Roles also can require that consuming classes have certain methods by calling the "requires" sugar function in the Role definition (or throw an exception).
You call the "with" sugar function to consume a Role by name, just like you call "extends" to inherit from a regular class.
Here is an example of a simple Role that could be composed into either MyApp::Rifle or MyApp::AutomaticRifle:
package MyApp::FireAll;
use strict;
use Moose::Role;
requires 'fire', 'rounds';
sub fire_all {
my $self = shift;
$self->fire while($self->rounds > 0);
}
1;
You would then add this single line to MyApp::Rifle or MyApp::AutomaticRifle to give either class the fire_all method:
with 'MyApp::FireAll';
In the case of MyApp::AutomaticRifle, the with statement must be called after the extends statement, because the "fire" and "rounds" methods don't exist within MyApp::AutomaticRifle before that, and the Role's requires statements would fail.
If you add the Role to MyApp::Rifle, it will be inherited by MyApp::AutomaticRifle automatically, so there would be no need to add it there also (although it won't break anything if you do).
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Reply to comment | Linux Journal
2 hours 7 min ago - Nice article, thanks for the
12 hours 47 min ago - I once had a better way I
18 hours 33 min ago - Not only you I too assumed
18 hours 50 min ago - another very interesting
20 hours 44 min ago - Reply to comment | Linux Journal
22 hours 37 min ago - Reply to comment | Linux Journal
1 day 5 hours ago - Reply to comment | Linux Journal
1 day 5 hours ago - Favorite (and easily brute-forced) pw's
1 day 7 hours ago - Have you tried Boxen? It's a
1 day 13 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
I guess the basic question
I guess the basic question this raises is whether ease of development for you beats usefulness to others. If that ticket is representative, it suggests that actual users feel that converting to Moose is transferring an unwelcome cost them, and so you might end up with modules that are easy to maintain, but unused. That more people might use Moose in the future is speculation, but this complaint is from an actual user.
Perhaps it would be better to wait for requests to Moose-ify existing modules, and if they don’t come, don’t bother. If converting to Moose is that simple, surely someone will send some patches to you, or fork the module, if they want a Moose version.
Instead, just consider Moose for new code, where there is no opportunity to encourage take-up by people looking for lightweight code, only to find it switched to what they think is heaviness after the fact.
Ianis from bet365.com
nice of artical Save the
nice of artical Save the aloft chic analogue into a book called MyApp/Rifle.pm aural one of your Perl's cover directories, and again you can use it in a Perl affairs like this. jogos de motos
Moose is awesome!
Thanks for this tutorial on Moose. Moose is simply awesome!
Intresting I have ner use
Intresting I have ner use Mose, but after reading this i might give it a try
----- http://ai.vc/zd
----- http://ai.vc/zd -----
Hi,Dear Ladies and Gentlemen,
1. sport shoes : Jordan ,Nike, adidas, Puma, Gucci, LV, UGG , etc. including women shoes and kids shoes.
2. T-Shirts : BBC T-Shirts, Bape T-Shirts, Armani T-Shirts, Polo T-Shirts,etc.
3. Hoodies : Bape hoody, hoody, AFF hoody, GGG hoody, ED hoody ,etc.
4. Jeans : Levis jeans , Gucci jeans, jeans, Bape jeans , DG jeans ,etc.
----- http://ai.vc/zd -----
----- http://ai.vc/zd -----
Service is our Lift.
enjoy yourself.
thank you!!
::∴★∵**☆.∴★∵**☆.∴★∵**☆.
█████.::∴★∵**☆.∴★∵**☆.
█田█田█::∴★∵**☆.∴★∵**☆.
█田█田█.∴★∵**☆.∴★∵**☆.
█田█田█∴★∵**☆.∴★∵**☆.
█田█田█.★∵**☆.∴★∵**☆.
█████.*******************
◢██□██◣.~~~~~*^_^*
thanx
thanx for this article.
I've been laying with perl for some years now, but never gave a try to Moose.
Now that i have a clearer view to what Moose it, i have to try it
ps : Perl IS wonderful !! Can't understand why people enjoy php much more than Perl