From: Dave Rolsky Date: Thu, 14 Oct 2010 17:12:29 +0000 (-0500) Subject: Lots of doc improvements for native delegations. X-Git-Tag: 1.16~46 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e132fd56595aa2447bf5ebf53cf73b64bce32003;p=gitmo%2FMoose.git Lots of doc improvements for native delegations. 1. Start calling them native delegations consistently. 2. Doc accepted arguments and return values for all provided methods. --- diff --git a/lib/Moose/Manual/Attributes.pod b/lib/Moose/Manual/Attributes.pod index 4fcc39c..94207db 100644 --- a/lib/Moose/Manual/Attributes.pod +++ b/lib/Moose/Manual/Attributes.pod @@ -497,10 +497,10 @@ attribute metaclasses and traits. See L for some examples. You can also write your own metaclasses and traits. See the "Meta" and "Extending" recipes in L for examples. -=head2 Native Traits +=head2 Native Delegations -The Native Traits feature allows standard Perl data structures to be treated -as if they were objects for the purposes of delegation. +Native delegations allow you to delegate to standard Perl data structures as +if they were objects. For example, we can pretend that an array reference has methods like C, C, C, C, and more. diff --git a/lib/Moose/Manual/Delegation.pod b/lib/Moose/Manual/Delegation.pod index cccf343..cdfae11 100644 --- a/lib/Moose/Manual/Delegation.pod +++ b/lib/Moose/Manual/Delegation.pod @@ -95,10 +95,10 @@ Finally, you can also provide a sub reference to I a mapping. You probably won't need this version often (if ever). See the L docs for more details on exactly how this works. -=head1 NATIVE TRAIT DELEGATION +=head1 NATIVE DELEGATION -The Native Traits feature allows standard Perl data structures to be treated -as if they were objects for the purposes of delegation. +Native delegations allow you to delegate to standard Perl data structures as +if they were objects. has 'queue' => ( traits => ['Array'], @@ -110,10 +110,10 @@ as if they were objects for the purposes of delegation. }, ) -By providing the C trait to the C parameter you tell Moose that -you would like to use the set of Array helpers. Moose will then create -C and C methods that "just works". Behind the scenes -C is something like +The C trait in the C parameter tells Moose that you would like +to use the set of Array helpers. Moose will then create C and +C methods that "just work". Behind the scenes C is +something like sub add_item { my ($self, @items) = @_; @@ -125,7 +125,7 @@ C is something like push @{ $self->queue }, @items; } -Moose includes the following native traits: +Moose includes the following traits for native delegation: =over 4 diff --git a/lib/Moose/Meta/Attribute/Native.pm b/lib/Moose/Meta/Attribute/Native.pm index ede20d7..a9ecda6 100644 --- a/lib/Moose/Meta/Attribute/Native.pm +++ b/lib/Moose/Meta/Attribute/Native.pm @@ -34,7 +34,7 @@ __END__ =head1 NAME -Moose::Meta::Attribute::Native - Extend your attribute interfaces +Moose::Meta::Attribute::Native - Delegate to native Perl types =head1 SYNOPSIS @@ -42,11 +42,11 @@ Moose::Meta::Attribute::Native - Extend your attribute interfaces use Moose; has 'mapping' => ( - traits => [ 'Hash' ], - is => 'rw', - isa => 'HashRef[Str]', - default => sub { {} }, - handles => { + traits => ['Hash'], + is => 'rw', + isa => 'HashRef[Str]', + default => sub { {} }, + handles => { exists_in_mapping => 'exists', ids_in_mapping => 'keys', get_mapping => 'get', @@ -55,16 +55,12 @@ Moose::Meta::Attribute::Native - Extend your attribute interfaces }, ); - - # ... - 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'); @@ -73,42 +69,56 @@ Moose::Meta::Attribute::Native - Extend your attribute interfaces =head1 DESCRIPTION -While L attributes provide a way to name your accessors, readers, -writers, clearers and predicates, this set of traits provides commonly -used attribute helper methods for more specific types of data. +Native delegations allow you to delegate to native Perl data +structure as if they were objects. For example, in the L you can +see a hash reference being treated as if it has methods named C, +C, C, and C. + +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. + +=head1 API -As seen in the L, you specify the data structure via the -C parameter. Available traits are below; see L. +Native delegations are enabled by passing certain options to C when +creating an attribute. -This module used to exist as the L extension. It was -very commonly used, so we moved it into core Moose. Since this gave us a chance -to change the interface, you will have to change your code or continue using -the L extension. L should -continue to work. +=head2 traits -=head1 PARAMETERS +To enable this feature, pass the appropriate name in the C array +reference for the attribute. For example, to enable this feature for hash +reference, we include C<'Hash'> in the list of traits. + +=head2 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 C. + +If you I specify a type, each trait has a default type it will use. =head2 handles -This is like C<< handles >> in L, but only HASH references are -allowed. Keys are method names that you want installed locally, and values are -methods from the method providers (below). Currying with delegated methods -works normally for C<< 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. -=head1 NATIVE TYPES +See the docs for each native trait for details on what methods are available. + +=head1 TRAITS FOR NATIVE DELEGATIONS =over =item L -Common methods for array references. - has 'queue' => ( - traits => ['Array'], - is => 'ro', - isa => 'ArrayRef[Str]', - default => sub { [] }, - handles => { + traits => ['Array'], + is => 'ro', + isa => 'ArrayRef[Str]', + default => sub { [] }, + handles => { add_item => 'push', next_item => 'shift', # ... @@ -117,14 +127,12 @@ Common methods for array references. =item L -Common methods for boolean values. - has 'is_lit' => ( - traits => ['Bool'], - is => 'rw', - isa => 'Bool', - default => 0, - handles => { + traits => ['Bool'], + is => 'ro', + isa => 'Bool', + default => 0, + handles => { illuminate => 'set', darken => 'unset', flip_switch => 'toggle', @@ -135,14 +143,14 @@ Common methods for boolean values. =item L -Common methods for code references. - has 'callback' => ( - traits => ['Code'], - is => 'ro', - isa => 'CodeRef', - default => sub { sub { 'called' } }, - handles => { + traits => ['Code'], + is => 'ro', + isa => 'CodeRef', + default => sub { + sub {'called'} + }, + handles => { call => 'execute', # ... } @@ -150,14 +158,12 @@ Common methods for code references. =item L -Methods for incrementing and decrementing a counter attribute. - has 'counter' => ( - traits => ['Counter'], - is => 'ro', - isa => 'Num', - default => 0, - handles => { + traits => ['Counter'], + is => 'ro', + isa => 'Num', + default => 0, + handles => { inc_counter => 'inc', dec_counter => 'dec', reset_counter => 'reset', @@ -167,14 +173,12 @@ Methods for incrementing and decrementing a counter attribute. =item L -Common methods for hash references. - has 'options' => ( - traits => ['Hash'], - is => 'ro', - isa => 'HashRef[Str]', - default => sub { {} }, - handles => { + traits => ['Hash'], + is => 'ro', + isa => 'HashRef[Str]', + default => sub { {} }, + handles => { set_option => 'set', get_option => 'get', has_option => 'exists', @@ -184,14 +188,12 @@ Common methods for hash references. =item L -Common numerical operations. - has 'integer' => ( - traits => ['Number'], - is => 'ro', - isa => 'Int', - default => 5, - handles => { + traits => ['Number'], + is => 'ro', + isa => 'Int', + default => 5, + handles => { set => 'set', add => 'add', sub => 'sub', @@ -205,14 +207,12 @@ Common numerical operations. =item L -Common methods for string operations. - has 'text' => ( - traits => ['String'], - is => 'rw', - isa => 'Str', - default => q{}, - handles => { + traits => ['String'], + is => 'ro', + isa => 'Str', + default => q{}, + handles => { add_text => 'append', replace_text => 'replace', # ... @@ -221,6 +221,15 @@ Common methods for string operations. =back +=head1 COMPATIBILITY WITH MooseX::AttributeHelpers + +This feature used to be a separated CPAN distribution called +L. + +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. + =head1 BUGS See L for details on reporting bugs. diff --git a/lib/Moose/Meta/Attribute/Native/Trait.pm b/lib/Moose/Meta/Attribute/Native/Trait.pm index 9fa865d..a44b7dd 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait.pm @@ -157,7 +157,7 @@ __END__ =head1 NAME -Moose::Meta::Attribute::Native::Trait - Base role for helpers +Moose::Meta::Attribute::Native::Trait - Shared role for native delegation traits =head1 BUGS @@ -165,7 +165,8 @@ See L for details on reporting bugs. =head1 SEE ALSO -Documentation for Moose native traits starts at L +Documentation for Moose native traits can be found in +L. =head1 AUTHORS diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Array.pm b/lib/Moose/Meta/Attribute/Native/Trait/Array.pm index 544d64a..e05d2e0 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/Array.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/Array.pm @@ -45,7 +45,7 @@ __END__ =head1 NAME -Moose::Meta::Attribute::Native::Trait::Array - Helper trait for ArrayRef attributes +Moose::Meta::Attribute::Native::Trait::Array - Helper trait for array delegation =head1 SYNOPSIS @@ -53,23 +53,23 @@ Moose::Meta::Attribute::Native::Trait::Array - Helper trait for ArrayRef attribu use Moose; has 'options' => ( - traits => ['Array'], - is => 'ro', - isa => 'ArrayRef[Str]', - default => sub { [] }, - handles => { - all_options => 'elements', - add_option => 'push', - map_options => 'map', - filter_options => 'grep', - find_option => 'first', - get_option => 'get', - join_options => 'join', - count_options => 'count', - has_options => 'count', - has_no_options => 'is_empty', - sorted_options => 'sort', - }, + traits => ['Array'], + is => 'ro', + isa => 'ArrayRef[Str]', + default => sub { [] }, + handles => { + all_options => 'elements', + add_option => 'push', + map_options => 'map', + filter_options => 'grep', + find_option => 'first', + get_option => 'get', + join_options => 'join', + count_options => 'count', + has_options => 'count', + has_no_options => 'is_empty', + sorted_options => 'sort', + }, ); no Moose; @@ -84,103 +84,148 @@ array operations. =over 4 -=item B +=item * B Returns the number of elements in the array. - $stuff = Stuff->new; - $stuff->options(["foo", "bar", "baz", "boo"]); + $stuff = Stuff->new; + $stuff->options( [ "foo", "bar", "baz", "boo" ] ); - my $count = $stuff->count_options; - print "$count\n"; # prints 4 + print $stuff->count_options; # prints 4 -=item B +This method does not accept any arguments. + +=item * B Returns a boolean value that is true when the array has no elements. - $stuff->has_no_options ? die "No options!\n" : print "Good boy.\n"; + $stuff->has_no_options ? die "No options!\n" : print "Good boy.\n"; + +This method does not accept any arguments. + +=item * B -=item B +Returns all of the elements of the array as an array (not an array reference). -Returns all of the elements of the array. + my @option = $stuff->all_options; + print "@options\n"; # prints "foo bar baz boo" - my @option = $stuff->all_options; - print "@options\n"; # prints "foo bar baz boo" +This method does not accept any arguments. -=item B +=item * B Returns an element of the array by its index. You can also use negative index numbers, just as with Perl's core array handling. - my $option = $stuff->get_option(1); - print "$option\n"; # prints "bar" + my $option = $stuff->get_option(1); + print "$option\n"; # prints "bar" + +If the specified element does not exist, this will return C. + +This method does accepts just one argument. + +=item * B + +Just like Perl's builtin C. + +This method does not accept any arguments. + +=item * B + +Just like Perl's builtin C. Returns the number of elements in the new +array. + +This method accepts any number of arguments. + +=item * B -=item B +Just like Perl's builtin C. -=item B +This method does not accept any arguments. -=item B +=item * B -=item B +Just like Perl's builtin C. Returns the number of elements in the new +array. -=item B +This method accepts any number of arguments. -These methods are all equivalent to the Perl core functions of the same name. +=item * B -=item B +Just like Perl's builtin C. In scalar context, this returns the last +element removed, or C if no elements were removed. In list context, +this returns all the elements removed from the array. + +This method requires at least one argument. + +=item * B This method returns the first item matching item in the array, just like L's C function. The matching is done with a subroutine -reference you pass to this method. The reference will be called against each +reference you pass to this method. The subroutine will be called against each element in the array until one matches or all elements have been checked. - my $found = $stuff->find_option( sub { /^b/ } ); - print "$found\n"; # prints "bar" + my $found = $stuff->find_option( sub {/^b/} ); + print "$found\n"; # prints "bar" + +This method requires a single argument. -=item B +=item * B This method returns every element matching a given criteria, just like Perl's core C function. This method requires a subroutine which implements the matching logic. - my @found = $stuff->filter_options( sub { /^b/ } ); - print "@found\n"; # prints "bar baz boo" + my @found = $stuff->filter_options( sub {/^b/} ); + print "@found\n"; # prints "bar baz boo" -=item B +This method requires a single argument. + +=item * B This method transforms every element in the array and returns a new array, just like Perl's core C function. This method requires a subroutine which implements the transformation. - my @mod_options = $stuff->map_options( sub { $_ . "-tag" } ); - print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag" + my @mod_options = $stuff->map_options( sub { $_ . "-tag" } ); + print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag" + +This method requires a single argument. -=item B +=item * B -This method condenses an array into a single value, by passing a function the +This method turns an array into a single value, by passing a function the value so far and the next value in the array, just like L's C function. The reducing is done with a subroutine reference you pass to this method. - my $found = $stuff->reduce_options( sub { $_[0] . $_[1] } ); - print "$found\n"; # prints "foobarbazboo" + my $found = $stuff->reduce_options( sub { $_[0] . $_[1] } ); + print "$found\n"; # prints "foobarbazboo" + +This method requires a single argument. -=item B +=item * B -Returns a the array in sorted order. +=item * B + +Returns the elements of the array in sorted order. You can provide an optional subroutine reference to sort with (as you can with -Perl's core C function). However, instead of using C<$a> and C<$b>, you -will need to use C<$_[0]> and C<$_[1]> instead. +Perl's core C function). However, instead of using C<$a> and C<$b> in +this subroutine, you will need to use C<$_[0]> and C<$_[1]>. + + # ascending ASCIIbetical + my @sorted = $stuff->sort_options(); + + # Descending alphabetical order + my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } ); + print "@sorted_options\n"; # prints "foo boo baz bar" - # ascending ASCIIbetical - my @sorted = $stuff->sort_options(); +This method accepts a single argument. - # Descending alphabetical order - my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } ); - print "@sorted_options\n"; # prints "foo boo baz bar" +=item * B -=item B +=item * B Sorts the array I, modifying the value of the attribute. @@ -188,60 +233,89 @@ You can provide an optional subroutine reference to sort with (as you can with Perl's core C function). However, instead of using C<$a> and C<$b>, you will need to use C<$_[0]> and C<$_[1]> instead. -=item B +This method does not define a return value. -Returns the array, with indices in random order, like C from +This method accepts a single argument. + +=item * B + +Returns the elements of the array in random order, like C from L. -=item B +This method does not accept any arguments. + +=item * B -Returns the array, with all duplicate elements removed, like C from +Returns the array with all duplicate elements removed, like C from L. -=item B +This method does not accept any arguments. + +=item * B Joins every element of the array using the separator given as argument, just like Perl's core C function. - my $joined = $stuff->join_options( ':' ); - print "$joined\n"; # prints "foo:bar:baz:boo" + my $joined = $stuff->join_options(':'); + print "$joined\n"; # prints "foo:bar:baz:boo" + +This method requires a single argument. -=item B +=item * B Given an index and a value, sets the specified array element's value. -=item B +This method returns the value at C<$index> after the set. + +This method requires two arguments. + +=item * B Removes the element at the given index from the array. -=item B +This method returns the deleted value. Note that if no value exists, it will +return C. + +This method requires one argument. + +=item * B Inserts a new element into the array at the given index. -=item B +This method returns the new value at C<$index>. + +This method requires two arguments. + +=item * B Empties the entire array, like C<@array = ()>. -=item B +This method does not define a return value. + +This method does not accept any arguments. + +=item * B + +=item * B This method provides a get/set accessor for the array, based on array indexes. If passed one argument, it returns the value at the specified index. If passed two arguments, it sets the value of the specified index. -=item B +When called as a setter, this method returns the new value at C<$index>. + +This method accepts one or two arguments. + +=item * B + +=item * B This method returns an iterator which, on each call, returns C<$n> more items from the array, in order, like C from L. A coderef can optionally be provided; it will be called on each group of C<$n> elements in the array. -=back - -=head1 METHODS - -=over 4 - -=item B +This method accepts one or two arguments. =back diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Bool.pm b/lib/Moose/Meta/Attribute/Native/Trait/Bool.pm index 93cfbe0..b636230 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/Bool.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/Bool.pm @@ -31,11 +31,11 @@ Moose::Meta::Attribute::Native::Trait::Bool - Helper trait for Bool attributes use Moose; has 'is_lit' => ( - traits => ['Bool'], - is => 'rw', - isa => 'Bool', - default => 0, - handles => { + traits => ['Bool'], + is => 'rw', + isa => 'Bool', + default => 0, + handles => { illuminate => 'set', darken => 'unset', flip_switch => 'toggle', @@ -44,10 +44,10 @@ Moose::Meta::Attribute::Native::Trait::Bool - Helper trait for Bool attributes ); my $room = Room->new(); - $room->illuminate; # same as $room->is_lit(1); - $room->darken; # same as $room->is_lit(0); - $room->flip_switch; # same as $room->is_lit(not $room->is_lit); - return $room->is_dark; # same as !$room->is_lit + $room->illuminate; # same as $room->is_lit(1); + $room->darken; # same as $room->is_lit(0); + $room->flip_switch; # same as $room->is_lit(not $room->is_lit); + return $room->is_dark; # same as !$room->is_lit =head1 DESCRIPTION @@ -56,31 +56,27 @@ basic math operations. =head1 PROVIDED METHODS +None of these methods accept arguments. + =over 4 -=item B +=item * B -Sets the value to C<1>. +Sets the value to C<1> and returns C<1>. -=item B +=item * B -Set the value to C<0>. +Set the value to C<0> and returns C<0>. -=item B +=item * B Toggles the value. If it's true, set to false, and vice versa. -=item B - -Equivalent of 'not C<$value>'. - -=back - -=head1 METHODS +Returns the new value. -=over 4 +=item * B -=item B +Equivalent of 'not C<$value>'. =back diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Code.pm b/lib/Moose/Meta/Attribute/Native/Trait/Code.pm index 0647da1..b1333cf 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/Code.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/Code.pm @@ -28,18 +28,19 @@ Moose::Meta::Attribute::Native::Trait::Code - Helper trait for Code attributes use Moose; has 'callback' => ( - traits => ['Code'], - is => 'ro', - isa => 'CodeRef', - default => sub { sub { print "called" } }, - handles => { + traits => ['Code'], + is => 'ro', + isa => 'CodeRef', + default => sub { + sub { print "called" } + }, + handles => { call => 'execute', }, ); my $foo = Foo->new; - $foo->call; # prints "called" - + $foo->call; # prints "called" =head1 DESCRIPTION @@ -49,24 +50,16 @@ This provides operations on coderef attributes. =over 4 -=item B +=item * B Calls the coderef with the given args. -=item B +=item * B Calls the coderef with the the instance as invocant and given args. =back -=head1 METHODS - -=over 4 - -=item B - -=back - =head1 BUGS See L for details on reporting bugs. diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Counter.pm b/lib/Moose/Meta/Attribute/Native/Trait/Counter.pm index eb16ce1..14334d9 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/Counter.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/Counter.pm @@ -37,11 +37,11 @@ Moose::Meta::Attribute::Native::Trait::Counter - Helper trait for counters use Moose; has 'counter' => ( - traits => ['Counter'], - is => 'ro', - isa => 'Num', - default => 0, - handles => { + traits => ['Counter'], + is => 'ro', + isa => 'Num', + default => 0, + handles => { inc_counter => 'inc', dec_counter => 'dec', reset_counter => 'reset', @@ -49,9 +49,9 @@ Moose::Meta::Attribute::Native::Trait::Counter - Helper trait for counters ); my $page = MyHomePage->new(); - $page->inc_counter; # same as $page->counter( $page->counter + 1 ); - $page->dec_counter; # same as $page->counter( $page->counter - 1 ); - + $page->inc_counter; # same as $page->counter( $page->counter + 1 ); + $page->dec_counter; # same as $page->counter( $page->counter - 1 ); + my $count_by_twos = 2; $page->inc_counter($count_by_twos); @@ -65,31 +65,34 @@ amount of change is one. =over 4 -=item B +=item * B -Set the counter to the specified value. +Sets the counter to the specified value and returns the new value. -=item B +This method requires a single argument. -Increase the attribute value by the amount of the argument. -No argument increments the value by 1. +=item * B -=item B +=item * B -Decrease the attribute value by the amount of the argument. -No argument decrements the value by 1. +Increases the attribute value by the amount of the argument, or by 1 if no +argument is given. This method returns the new value. -=item B +This method accepts a single argument. -Resets the value stored in this slot to it's default value. +=item * B -=back +=item * B -=head1 METHODS +Decreases the attribute value by the amount of the argument, or by 1 if no +argument is given. This method returns the new value. -=over 4 +This method accepts a single argument. + +=item * B -=item B +Resets the value stored in this slot to its default value, and returns the new +value. =back diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Hash.pm b/lib/Moose/Meta/Attribute/Native/Trait/Hash.pm index e34639e..6cdf98e 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/Hash.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/Hash.pm @@ -69,33 +69,50 @@ hash-like operations. Returns values from the hash. -In list context return a list of values in the hash for the given keys. -In scalar context returns the value for the last key specified. +In list context it returns a list of values in the hash for the given keys. In +scalar context it returns the value for the last key specified. + +This method requires at least one argument. =item B $value, $key2 =E $value2...)> -Sets the elements in the hash to the given values. +Sets the elements in the hash to the given values. It returns the new values +set for each key, in the same order as the keys passed to the method. + +This method requires at least two arguments, and expects an even number of +arguments. =item B Removes the elements with the given keys. +In list context it returns a list of values in the hash for the deleted +keys. In scalar context it returns the value for the last key specified. + =item B Returns the list of keys in the hash. +This method does not accept any arguments. + =item B Returns true if the given key is present in the hash. +This method requires a single argument. + =item B Returns true if the value of a given key is defined. +This method requires a single argument. + =item B Returns the list of values in the hash. +This method does not accept any arguments. + =item B Returns the key/value pairs in the hash as an array of array references. @@ -104,28 +121,42 @@ Returns the key/value pairs in the hash as an array of array references. print "$pair->[0] = $pair->[1]\n"; } +This method does not accept any arguments. + =item B Returns the key/value pairs in the hash as a flattened list.. +This method does not accept any arguments. + =item B Resets the hash to an empty value, like C<%hash = ()>. +This method does not accept any arguments. + =item B Returns the number of elements in the hash. Also useful for not empty: C<< has_options => 'count' >>. +This method does not accept any arguments. + =item B If the hash is populated, returns false. Otherwise, returns true. -=item B +This method does not accept any arguments. + +=item B + +=item B If passed one argument, returns the value of the specified key. If passed two arguments, sets the value of the specified key. +When called as a setter, this method returns the value that was set. + =back =head1 METHODS diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Number.pm b/lib/Moose/Meta/Attribute/Native/Trait/Number.pm index 40f30cf..361fb81 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/Number.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/Number.pm @@ -33,11 +33,11 @@ Moose::Meta::Attribute::Native::Trait::Number - Helper trait for Num attributes use Moose; has 'integer' => ( - traits => ['Number'], - is => 'ro', - isa => 'Num', - default => 5, - handles => { + traits => ['Number'], + is => 'ro', + isa => 'Num', + default => 5, + handles => { set => 'set', add => 'add', sub => 'sub', @@ -49,8 +49,8 @@ Moose::Meta::Attribute::Native::Trait::Number - Helper trait for Num attributes ); my $real = Real->new(); - $real->add(5); # same as $real->integer($real->integer + 5); - $real->sub(2); # same as $real->integer($real->integer - 2); + $real->add(5); # same as $real->integer($real->integer + 5); + $real->sub(2); # same as $real->integer($real->integer - 2); =head1 DESCRIPTION @@ -65,43 +65,34 @@ package. =over 4 -=item B +=item * B -Alternate way to set the value. +Adds the current value of the attribute to C<$value>. Returns the new value. -=item B +=item * B -Adds the current value of the attribute to C<$value>. +Subtracts C<$value> from the current value of the attribute. Returns the new +value. -=item B +=item * B -Subtracts C<$value> from the current value of the attribute. +Multiplies the current value of the attribute by C<$value>. Returns the new +value. -=item B +=item * B -Multiplies the current value of the attribute by C<$value>. +Divides the current value of the attribute by C<$value>. Returns the new +value. -=item B +=item * B -Divides the current value of the attribute by C<$value>. +Returns the current value of the attribute modulo C<$value>. Returns the new +value. -=item B +=item * B -Returns the current value of the attribute modulo C<$value>. - -=item B - -Sets the current value of the attribute to its absolute value. - -=back - -=head1 METHODS - -=over 4 - -=item B - -=item B +Sets the current value of the attribute to its absolute value. Returns the new +value. =back diff --git a/lib/Moose/Meta/Attribute/Native/Trait/String.pm b/lib/Moose/Meta/Attribute/Native/Trait/String.pm index 60bdb8a..4d690a8 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/String.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/String.pm @@ -40,18 +40,18 @@ Moose::Meta::Attribute::Native::Trait::String - Helper trait for Str attributes use Moose; has 'text' => ( - traits => ['String'], - is => 'rw', - isa => 'Str', - default => q{}, - handles => { + traits => ['String'], + is => 'rw', + isa => 'Str', + default => q{}, + handles => { add_text => 'append', replace_text => 'replace', }, ); my $page = MyHomePage->new(); - $page->add_text("foo"); # same as $page->text($page->text . "foo"); + $page->add_text("foo"); # same as $page->text($page->text . "foo"); =head1 DESCRIPTION @@ -72,59 +72,72 @@ above. This allows for a very basic string definition: =over 4 -=item B +=item * B Increments the value stored in this slot using the magical string autoincrement operator. Note that Perl doesn't provide analogous behavior in C<-->, so -C is not available. +C is not available. This method returns the new value. -=item B +This method does not accept any arguments. -Append a string, like C<.=>. +=item * B -=item B +Appends to the string, like C<.=>, and returns the new value. -Prepend a string. +This method requires a single argument. -=item B +=item * B + +Prepends to the string and returns the new value. + +This method requires a single argument. + +=item * B Performs a regexp substitution (L). There is no way to provide the C flag, but code references will be accepted for the replacement, causing the regex to be modified with a single C. C can be applied using the -C operator. +C operator. This method returns the new value. -=item B +This method requires two arguments. -Like C but without the replacement. Provided mostly for completeness. +=item * B -=item B +Runs the regex against the string and returns the matching value(s). -L +This method requires a single argument. -=item B +=item * B -L +Just like L. This method returns the chopped character. -=item B +This method does not accept any arguments. -Sets the string to the empty string (not the value passed to C). +=item * B + +Just like L. This method returns the number of characters +removed. -=item B +This method does not accept any arguments. -L +=item * B -=item B +Sets the string to the empty string (not the value passed to C). -L. We go to some lengths to match the different functionality -based on C's arity. +This method does not have a defined return value. -=back +This method does not accept any arguments. -=head1 METHODS +=item * B -=over 4 +Just like L, returns the length of the string. + +=item * B + +This acts just like L. When called as a writer, it returns +the substring that was replaced, just like the Perl builtin. -=item B +This method requires at least one argument, and accepts no more than three. =back