GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
MouseX::Traits(3) User Contributed Perl Documentation MouseX::Traits(3)
 

MouseX::Traits - automatically apply roles at object creation time

Given some roles:
  package Role;
  use Mouse::Role;
  has foo => ( is => 'ro', isa => 'Int' required => 1 );
And a class:
  package Class;
  use Mouse;
  with 'MouseX::Traits';
Apply the roles to the class at "new" time:
  my $class = Class->with_traits('Role')->new( foo => 42 );
Then use your customized class:
  $class->isa('Class'); # true
  $class->does('Role'); # true
  $class->foo; # 42

Often you want to create components that can be added to a class arbitrarily. This module makes it easy for the end user to use these components. Instead of requiring the user to create a named class with the desired roles applied, or apply roles to the instance one-by-one, he can just create a new class from yours with "with_traits", and then instantiate that.
There is also "new_with_traits", which exists for compatibility reasons. It accepts a "traits" parameter, creates a new class with those traits, and then insantiates it.
   Class->new_with_traits( traits => [qw/Foo Bar/], foo => 42, bar => 1 )
returns exactly the same object as
   Class->with_traits(qw/Foo Bar/)->new( foo => 42, bar => 1 )
would. But you can also store the result of "with_traits", and call other methods:
   my $c = Class->with_traits(qw/Foo Bar/);
   $c->new( foo => 42 );
   $c->whatever( foo => 1234 );
And so on.

$class->with_traits( @traits )
Return a new class with the traits applied. Use like:
$class->new_with_traits(%args, traits => \@traits)
"new_with_traits" can also take a hashref, e.g.:
  my $instance = $class->new_with_traits({ traits => \@traits, foo => 'bar' });
    

This role will add the following attributes to the consuming class.

You can override the value of this attribute with "default" to automatically prepend a namespace to the supplied traits. (This can be overridden by prefixing the trait name with "+".)
Example:
  package Another::Trait;
  use Mouse::Role;
  has 'bar' => (
      is       => 'ro',
      isa      => 'Str',
      required => 1,
  );
  package Another::Class;
  use Mouse;
  with 'MouseX::Traits';
  has '+_trait_namespace' => ( default => 'Another' );
  my $instance = Another::Class->new_with_traits(
      traits => ['Trait'], # "Another::Trait", not "Trait"
      bar    => 'bar',
  );
  $instance->does('Trait')          # false
  $instance->does('Another::Trait') # true
  my $instance2 = Another::Class->new_with_traits(
      traits => ['+Trait'], # "Trait", not "Another::Trait"
  );
  $instance2->does('Trait')          # true
  $instance2->does('Another::Trait') # false

Fuji, Goro (gfx) <gfuji(at)cpan.org>

The MouseX::Traits is originated from MooseX::Traits, which is written and maintained by:
Jonathan Rockway "<jrockway@cpan.org>"
Stevan Little "<stevan.little@iinteractive.com>"
Tomas Doran "<bobtfish@bobtfish.net>"
Matt S. Trout "<mst@shadowcatsystems.co.uk>"
Jesse Luehrs "<doy at tozt dot net>"
Shawn Moore "<sartak@bestpractical.com>"
Florian Ragwitz "<rafl@debian.org>"
Chris Prather "<chris@prather.org>"
Yuval Kogman "<nothingmuch@woobling.org>"

Copyright 2008-2010 Infinity Interactive, Inc.
<http://www.iinteractive.com>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2011-01-10 perl v5.28.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.