-
-
31 Mar 2021 01:11:39 UTC
- Distribution: Moose
- Module version: 2.2015
- Source (raw)
- Browse (raw)
- Changes
- Homepage
- How to Contribute
- Repository
- Issues (71)
- Testers (424 / 0 / 0)
- Kwalitee
Bus factor: 3- 92.10% Coverage
- License: perl_5
- Perl: v5.8.3
- Activity
24 month- Tools
- Download (878.24KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
and 142 contributors-
Stevan Little
-
Dave Rolsky
-
Jesse Luehrs
-
Shawn M Moore
-
יובל קוג'מן (Yuval Kogman)
-
Florian Ragwitz
-
Hans Dieter Pearcey
-
Chris Prather
-
Matt S Trout
-
Upasana Shukla
-
Graham Knop
-
Tomas Doran
-
Ricardo Signes
-
Guillermo Roditi
-
John Napiorkowski
-
Aankhen
-
Todd Hepler
-
Jonathan Rockway
-
Gerda Shank
-
Perlover
-
Shlomi Fish
-
Brad Bowman
-
Justin Hunter
-
Kent Fredric
-
Paul Driver
-
Anders Nor Berle
-
Brian Manning
-
gfx
-
Lars Dɪᴇᴄᴋᴏᴡ 迪拉斯
-
Leon Brocard
-
Olivier Mengué
-
Rafael Kitover
-
Christian Hansen
-
Cory Watson
-
Dagfinn Ilmari Mannsåker
-
Paul Jamieson Fenwick
-
Robert Buels
-
Dan Dascalescu
-
Marcel Grünauer
-
Scott McWhirter
-
Ævar Arnfjörð Bjarmason
-
Daisuke Maki (lestrrat)
-
Dylan William Hardison
-
Jay Hannah
-
Patrick Donelan
-
Stefan O'Rear
-
Tokuhiro Matsuno
-
Ash Berlin
-
Chris Weyl
-
Eric Wilhelm
-
Jess Robinson
-
Marc Mims
-
Marcus Ramberg
-
Mark Allen
-
Mateu X Hunter
-
matthof
-
Robert 'phaylon' Sedlacek
-
Zachary Lome
-
Aran Clary Deltac
-
Chip
-
Christopher J. Madsen
-
Curtis Jewell
-
Evan Carroll
-
Mark A. Stratman
-
Mark Fowler
-
Matthew Horsfall
-
mauke
-
Michael LaGrasta
-
Michael Rykov
-
Mike Whitaker
-
Moritz Onken
-
Nelo Onyiah
-
Nick Perez
-
Robert Boone
-
Robin V
-
rodrigolive
-
shelling
-
Thomas Sibley
-
Tom Hukins
-
Wallace Reis
-
Aaron Cohen
-
Adam J. Foxson
-
Adam Kennedy
-
Andy Jack
-
Anirvan Chatterjee
-
Ansgar Burchardt
-
A. Sinan Unur
-
Ben Hutton
-
Brendan Byrd
-
Chad Granum
-
Chankey Pathak
-
Chia-liang Kao
-
Christian Walde (Mithaldu)
-
chromatic
-
Dann
-
Dave Romano
-
David Leadbeater
-
David Steinbrunner
-
dmaestro
-
E. Choroba
-
franck cuny
-
Frew Schmidt
-
gregor herrmann
-
hakim
-
Henry Van Styn
-
James Marca
-
Jason May
-
Jay Allen
-
Jay Kuri
-
Jeff Bisbee
-
Jens Berthold
-
Jesse Vincent
-
joel
-
John Douglas Porter
-
John Goulah
-
Justin DeVuyst
-
Kang-min Liu
-
Leon Timmermans
-
Mark O Grady
-
Matt Kraai
-
Michael Schout
-
Nathan Gray
-
Olaf Alders
-
Olof Johansson
-
Paul Cochrane
-
Paweł Murias
-
Pedro Melo
-
Peter Shangov
-
Philippe Bruhat (BooK)
-
Philipp Gortan
-
Phillip Smith
-
Piotr Roszatycki
-
pktm
-
rouzier
-
Sam Vilain
-
sherrardb
-
Simon Reinhardt
-
sue spence
-
Tuomas Jormola
-
wickline
-
Yanick Champoux
-
Zoffix Znet
- Dependencies
- Carp
- Class::Load
- Class::Load::XS
- Data::OptList
- Devel::GlobalDestruction
- Devel::OverloadInfo
- Devel::StackTrace
- Dist::CheckConflicts
- Eval::Closure
- List::Util
- MRO::Compat
- Module::Runtime
- Module::Runtime::Conflicts
- Package::DeprecationManager
- Package::Stash
- Package::Stash::XS
- Params::Util
- Scalar::Util
- Sub::Exporter
- Sub::Name
- Try::Tiny
- parent
- strict
- warnings
- Reverse dependencies
- CPAN Testers List
- Dependency graph
- NAME
- VERSION
- SYNOPSIS
- DESCRIPTION
- API
- TRAITS FOR NATIVE DELEGATIONS
- COMPATIBILITY WITH MooseX::AttributeHelpers
- BUGS
- AUTHORS
- COPYRIGHT AND LICENSE
NAME
Moose::Meta::Attribute::Native - Delegate to native Perl types
VERSION
version 2.2015
SYNOPSIS
package MyClass; use Moose; has 'mapping' => ( traits => ['Hash'], is => 'rw', isa => 'HashRef[Str]', default => sub { {} }, handles => { exists_in_mapping => 'exists', ids_in_mapping => 'keys', get_mapping => 'get', set_mapping => 'set', set_quantity => [ set => 'quantity' ], }, ); my $obj = MyClass->new; $obj->set_quantity(10); # quantity => 10 $obj->set_mapping('foo', 4); # foo => 4 $obj->set_mapping('bar', 5); # bar => 5 $obj->set_mapping('baz', 6); # baz => 6 # prints 5 print $obj->get_mapping('bar') if $obj->exists_in_mapping('bar'); # prints 'quantity, foo, bar, baz' print join ', ', $obj->ids_in_mapping;
DESCRIPTION
Native delegations allow you to delegate to native Perl data structures as if they were objects. For example, in the "SYNOPSIS" you can see a hash reference being treated as if it has methods named
exists()
,keys()
,get()
, andset()
.The delegation methods (mostly) map to Perl builtins and operators. The return values of these delegations should be the same as the corresponding Perl operation. Any deviations will be explicitly documented.
API
Native delegations are enabled by passing certain options to
has
when creating an attribute.traits
To enable this feature, pass the appropriate name in the
traits
array reference for the attribute. For example, to enable this feature for hash reference, we include'Hash'
in the list of traits.isa
You will need to make sure that the attribute has an appropriate type. For example, to use this with a Hash you must specify that your attribute is some sort of
HashRef
.handles
This is just like any other delegation, but only a hash reference is allowed when defining native delegations. The keys are the methods to be created in the class which contains the attribute. The values are the methods provided by the associated trait. Currying works the same way as it does with any other delegation.
See the docs for each native trait for details on what methods are available.
TRAITS FOR NATIVE DELEGATIONS
Below are some simple examples of each native trait. More features are available than what is shown here; this is just a quick synopsis.
- Array (Moose::Meta::Attribute::Native::Trait::Array)
-
has 'queue' => ( traits => ['Array'], is => 'ro', isa => 'ArrayRef[Str]', default => sub { [] }, handles => { add_item => 'push', next_item => 'shift', # ... } );
- Bool (Moose::Meta::Attribute::Native::Trait::Bool)
-
has 'is_lit' => ( traits => ['Bool'], is => 'ro', isa => 'Bool', default => 0, handles => { illuminate => 'set', darken => 'unset', flip_switch => 'toggle', is_dark => 'not', # ... } );
- Code (Moose::Meta::Attribute::Native::Trait::Code)
-
has 'callback' => ( traits => ['Code'], is => 'ro', isa => 'CodeRef', default => sub { sub {'called'} }, handles => { call => 'execute', # ... } );
- Counter (Moose::Meta::Attribute::Native::Trait::Counter)
-
has 'counter' => ( traits => ['Counter'], is => 'ro', isa => 'Num', default => 0, handles => { inc_counter => 'inc', dec_counter => 'dec', reset_counter => 'reset', # ... } );
- Hash (Moose::Meta::Attribute::Native::Trait::Hash)
-
has 'options' => ( traits => ['Hash'], is => 'ro', isa => 'HashRef[Str]', default => sub { {} }, handles => { set_option => 'set', get_option => 'get', has_option => 'exists', # ... } );
- Number (Moose::Meta::Attribute::Native::Trait::Number)
-
has 'integer' => ( traits => ['Number'], is => 'ro', isa => 'Int', default => 5, handles => { set => 'set', add => 'add', sub => 'sub', mul => 'mul', div => 'div', mod => 'mod', abs => 'abs', # ... } );
- String (Moose::Meta::Attribute::Native::Trait::String)
-
has 'text' => ( traits => ['String'], is => 'ro', isa => 'Str', default => q{}, handles => { add_text => 'append', replace_text => 'replace', # ... } );
COMPATIBILITY WITH MooseX::AttributeHelpers
This feature used to be a separated CPAN distribution called MooseX::AttributeHelpers.
When the feature was incorporated into the Moose core, some of the API details were changed. The underlying capabilities are the same, but some details of the API were changed.
BUGS
See "BUGS" in Moose for details on reporting bugs.
AUTHORS
Stevan Little <stevan@cpan.org>
Dave Rolsky <autarch@urth.org>
Jesse Luehrs <doy@cpan.org>
Shawn M Moore <sartak@cpan.org>
יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
Karen Etheridge <ether@cpan.org>
Florian Ragwitz <rafl@debian.org>
Hans Dieter Pearcey <hdp@cpan.org>
Chris Prather <chris@prather.org>
Matt S Trout <mstrout@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2006 by Infinity Interactive, Inc.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Module Install Instructions
To install Moose, copy and paste the appropriate command in to your terminal.
cpanm Moose
perl -MCPAN -e shell install Moose
For more information on module installation, please visit the detailed CPAN module installation guide.