Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: Your favorite objects NOT of the hashref phylum

by stvn (Monsignor)
on Mar 24, 2006 at 20:48 UTC ( [id://539094]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Your favorite objects NOT of the hashref phylum
in thread Your favorite objects NOT of the hashref phylum

Could you help me understand what's really different about Moose beyond syntax?

Moose is built on top of Class::MOP (a metaclass system for Perl 5), which means that Moose has support for deep class introspection. So for instance, you can do this:

my $type_constraint = Stopwatch->meta->get_attribute('timer')->type_co +nstraint; print $type_constraint->name; # prints DateTime
And since the $type_constraint returned is itself a meta-object, you can get plenty of information from it as well, such as it's parent types (NOTE: this is not reflective of any class/@ISA hierarchy in Moose, it is instead just type/subtype relationships defined in Moose::Util::TypeConstraints)
print $type_constraint->parent->name; # prints Object print $type_constraint->parent->parent->name; # prints Ref print $type_constraint->parent->parent->parent->name; # prints Any
While type information like this is not directly useful for your everyday programmer, it is very useful for tools like ORMs. I am actually working closely with mst, who is the author of DBIx::Class, to explore the possibilities of using this deep introspection from Moose with DBIx::Class somehow.

But Moose has many other features as well, not just automatic accessor generation. It also can create arbitrary type-constraints, which are not directly linked to classes in the system. Here is an example from the Moose test suite:

subtype Protocol => as Str => where { /^HTTP\/[0-9]\.[0-9]$/ };
This is a subtype of the built-in Moose type 'Str' which will only accepts strings which look like HTTP protocol declarations. And you can use these in your attribute declarations just the same as class names, so this will DWIM.

has 'protocol' => (isa => 'Protocol');

Because Str and Protocol are just a type "constraints", we avoid the unessecary overhead of creating a class for this. Moose also has type-coercion features as well, although those are still fairly experimental, if you interested I suggest looking here to see a good example of it's usage.

Moose also offers CLOS style before/after/around method modifiers, which allow for AOP like method wrapping. An example of that can be found here in a recent use.perl posting of mine. And version 0.03 of Moose (due out later this week), will have support for BETA style "inner" calls (think of these as the inverse of "super" calls, /msg me if you want more details).

And to top this all off, Moose will be (although it not currently, it's only 0.02 after all) completely pluggable. Meaning you will be able to (for instance) subclass Moose::Meta::Attribute to either change existing behaviors, or add new behaviors to change how Moose handles attributes. This is all due to the fact that Moose is built on Class::MOP, which is built on the theoretical foundations of other (somewhat more acedemic) object systems like CLOS and Smalltalk (which themselves were built by people far smarter than I will ever be).

In short, Moose is just the syntactic sugar on top of a fully dynamic and reflective object system for Perl 5.

-stvn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://539094]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-19 20:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found