.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Moose::Meta::Attribute::Native::Trait::Array 3pm" .TH Moose::Meta::Attribute::Native::Trait::Array 3pm "2023-01-30" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Moose::Meta::Attribute::Native::Trait::Array \- Helper trait for ArrayRef attributes .SH "VERSION" .IX Header "VERSION" version 2.2203 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& package Stuff; \& use Moose; \& \& has \*(Aqoptions\*(Aq => ( \& traits => [\*(AqArray\*(Aq], \& is => \*(Aqro\*(Aq, \& isa => \*(AqArrayRef[Str]\*(Aq, \& default => sub { [] }, \& handles => { \& all_options => \*(Aqelements\*(Aq, \& add_option => \*(Aqpush\*(Aq, \& map_options => \*(Aqmap\*(Aq, \& filter_options => \*(Aqgrep\*(Aq, \& find_option => \*(Aqfirst\*(Aq, \& get_option => \*(Aqget\*(Aq, \& join_options => \*(Aqjoin\*(Aq, \& count_options => \*(Aqcount\*(Aq, \& has_options => \*(Aqcount\*(Aq, \& has_no_options => \*(Aqis_empty\*(Aq, \& sorted_options => \*(Aqsort\*(Aq, \& }, \& ); \& \& no Moose; \& 1; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This trait provides native delegation methods for array references. .SH "DEFAULT TYPE" .IX Header "DEFAULT TYPE" If you don't provide an \f(CW\*(C`isa\*(C'\fR value for your attribute, it will default to \&\f(CW\*(C`ArrayRef\*(C'\fR. .SH "PROVIDED METHODS" .IX Header "PROVIDED METHODS" .IP "\(bu" 4 \&\fBcount\fR .Sp Returns the number of elements in the array. .Sp .Vb 2 \& $stuff = Stuff\->new; \& $stuff\->options( [ "foo", "bar", "baz", "boo" ] ); \& \& print $stuff\->count_options; # prints 4 .Ve .Sp This method does not accept any arguments. .IP "\(bu" 4 \&\fBis_empty\fR .Sp Returns a boolean value that is true when the array has no elements. .Sp .Vb 1 \& $stuff\->has_no_options ? die "No options!\en" : print "Good boy.\en"; .Ve .Sp This method does not accept any arguments. .IP "\(bu" 4 \&\fBelements\fR .Sp In list context, returns all of the elements of the array as a list. .Sp In scalar context, returns the number of elements in the array. .Sp .Vb 3 \& my @options = $stuff\->all_options; \& print "@options"; # prints "foo bar baz boo" \& print scalar $stuff\->all_options; # prints 4 .Ve .Sp This method does not accept any arguments. .IP "\(bu" 4 \&\fBget($index)\fR .Sp Returns an element of the array by its index. You can also use negative index numbers, just as with Perl's core array handling. .Sp .Vb 2 \& my $option = $stuff\->get_option(1); \& print "$option\en"; # prints "bar" .Ve .Sp If the specified element does not exist, this will return \f(CW\*(C`undef\*(C'\fR. .Sp This method accepts just one argument. .IP "\(bu" 4 \&\fBpop\fR .Sp Just like Perl's builtin \f(CW\*(C`pop\*(C'\fR. .Sp This method does not accept any arguments. .IP "\(bu" 4 \&\fBpush($value1, \f(CB$value2\fB, value3 ...)\fR .Sp Just like Perl's builtin \f(CW\*(C`push\*(C'\fR. Returns the number of elements in the new array. .Sp This method accepts any number of arguments. .IP "\(bu" 4 \&\fBshift\fR .Sp Just like Perl's builtin \f(CW\*(C`shift\*(C'\fR. .Sp This method does not accept any arguments. .IP "\(bu" 4 \&\fBunshift($value1, \f(CB$value2\fB, value3 ...)\fR .Sp Just like Perl's builtin \f(CW\*(C`unshift\*(C'\fR. Returns the number of elements in the new array. .Sp This method accepts any number of arguments. .IP "\(bu" 4 \&\fBsplice($offset, \f(CB$length\fB, \f(CB@values\fB)\fR .Sp Just like Perl's builtin \f(CW\*(C`splice\*(C'\fR. In scalar context, this returns the last element removed, or \f(CW\*(C`undef\*(C'\fR if no elements were removed. In list context, this returns all the elements removed from the array. .Sp This method requires at least one argument. .IP "\(bu" 4 \&\fBfirst( sub { ... } )\fR .Sp This method returns the first matching item in the array, just like List::Util's \f(CW\*(C`first\*(C'\fR function. The matching is done with a subroutine 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. Each list element will be available to the sub in \f(CW$_\fR. .Sp .Vb 2 \& my $found = $stuff\->find_option( sub {/^b/} ); \& print "$found\en"; # prints "bar" .Ve .Sp This method requires a single argument. .IP "\(bu" 4 \&\fBfirst_index( sub { ... } )\fR .Sp This method returns the index of the first matching item in the array, just like \*(L"first_index\*(R" in List::SomeUtils. The matching is done with a subroutine 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. Each list element will be available to the sub in \f(CW$_\fR. If no match is made, \-1 is returned. .Sp This method requires a single argument. .IP "\(bu" 4 \&\fBgrep( sub { ... } )\fR .Sp This method returns every element matching a given criteria, just like Perl's core \f(CW\*(C`grep\*(C'\fR function. This method requires a subroutine which implements the matching logic; each list element will be available to the sub in \f(CW$_\fR. .Sp .Vb 2 \& my @found = $stuff\->filter_options( sub {/^b/} ); \& print "@found\en"; # prints "bar baz boo" .Ve .Sp This method requires a single argument. .IP "\(bu" 4 \&\fBmap( sub { ... } )\fR .Sp This method transforms every element in the array and returns a new array, just like Perl's core \f(CW\*(C`map\*(C'\fR function. This method requires a subroutine which implements the transformation; each list element will be available to the sub in \f(CW$_\fR. .Sp .Vb 2 \& my @mod_options = $stuff\->map_options( sub { $_ . "\-tag" } ); \& print "@mod_options\en"; # prints "foo\-tag bar\-tag baz\-tag boo\-tag" .Ve .Sp This method requires a single argument. .IP "\(bu" 4 \&\fBreduce( sub { ... } )\fR .Sp 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 List::Util's \&\f(CW\*(C`reduce\*(C'\fR function. The reducing is done with a subroutine reference you pass to this method; each list element will be available to the sub in \f(CW$_\fR. .Sp .Vb 2 \& my $found = $stuff\->reduce_options( sub { $_[0] . $_[1] } ); \& print "$found\en"; # prints "foobarbazboo" .Ve .Sp This method requires a single argument. .IP "\(bu" 4 \&\fBsort\fR .IP "\(bu" 4 \&\fBsort( sub { ... } )\fR .Sp Returns the elements of the array (not an array reference) in sorted order, or, like \f(CW\*(C`elements\*(C'\fR, returns the number of elements in the array in scalar context. .Sp You can provide an optional subroutine reference to sort with (as you can with Perl's core \f(CW\*(C`sort\*(C'\fR function). However, instead of using \f(CW$a\fR and \f(CW$b\fR in this subroutine, you will need to use \f(CW$_[0]\fR and \f(CW$_[1]\fR. .Sp .Vb 2 \& # 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\en"; # prints "foo boo baz bar" .Ve .Sp This method accepts a single argument. .IP "\(bu" 4 \&\fBsort_in_place\fR .IP "\(bu" 4 \&\fBsort_in_place( sub { ... } )\fR .Sp Sorts the array \fIin place\fR, modifying the value of the attribute. .Sp You can provide an optional subroutine reference to sort with (as you can with Perl's core \f(CW\*(C`sort\*(C'\fR function). However, instead of using \f(CW$a\fR and \f(CW$b\fR, you will need to use \f(CW$_[0]\fR and \f(CW$_[1]\fR instead. .Sp This method does not define a return value. .Sp This method accepts a single argument. .IP "\(bu" 4 \&\fBshuffle\fR .Sp Returns the elements of the array in random order, like \f(CW\*(C`shuffle\*(C'\fR from List::Util. .Sp This method does not accept any arguments. .IP "\(bu" 4 \&\fBuniq\fR .Sp Returns the array with all duplicate elements removed, like \*(L"uniq\*(R" in List::Util. .Sp This method does not accept any arguments. .IP "\(bu" 4 \&\fBjoin($str)\fR .Sp Joins every element of the array using the separator given as argument, just like Perl's core \f(CW\*(C`join\*(C'\fR function. .Sp .Vb 2 \& my $joined = $stuff\->join_options(\*(Aq:\*(Aq); \& print "$joined\en"; # prints "foo:bar:baz:boo" .Ve .Sp This method requires a single argument. .IP "\(bu" 4 \&\fBset($index, \f(CB$value\fB)\fR .Sp Given an index and a value, sets the specified array element's value. .Sp This method returns the value at \f(CW$index\fR after the set. .Sp This method requires two arguments. .IP "\(bu" 4 \&\fBdelete($index)\fR .Sp Removes the element at the given index from the array. .Sp This method returns the deleted value. Note that if no value exists, it will return \f(CW\*(C`undef\*(C'\fR. .Sp This method requires one argument. .IP "\(bu" 4 \&\fBinsert($index, \f(CB$value\fB)\fR .Sp Inserts a new element into the array at the given index. .Sp This method returns the new value at \f(CW$index\fR. .Sp This method requires two arguments. .IP "\(bu" 4 \&\fBclear\fR .Sp Empties the entire array, like \f(CW\*(C`@array = ()\*(C'\fR. .Sp This method does not define a return value. .Sp This method does not accept any arguments. .IP "\(bu" 4 \&\fBaccessor($index)\fR .IP "\(bu" 4 \&\fBaccessor($index, \f(CB$value\fB)\fR .Sp 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. .Sp When called as a setter, this method returns the new value at \f(CW$index\fR. .Sp This method accepts one or two arguments. .IP "\(bu" 4 \&\fBnatatime($n)\fR .IP "\(bu" 4 \&\fBnatatime($n, \f(CB$code\fB)\fR .Sp This method returns an iterator which, on each call, returns \f(CW$n\fR more items from the array, in order, like \*(L"natatime\*(R" in List::SomeUtils. .Sp If you pass a coderef as the second argument, then this code ref will be called on each group of \f(CW$n\fR elements in the array until the array is exhausted. .Sp This method accepts one or two arguments. .IP "\(bu" 4 \&\fBshallow_clone\fR .Sp This method returns a shallow clone of the array reference. The return value is a reference to a new array with the same elements. It is \fIshallow\fR because any elements that were references in the original will be the \fIsame\fR references in the clone. .SH "BUGS" .IX Header "BUGS" See \*(L"\s-1BUGS\*(R"\s0 in Moose for details on reporting bugs. .SH "AUTHORS" .IX Header "AUTHORS" .IP "\(bu" 4 Stevan Little .IP "\(bu" 4 Dave Rolsky .IP "\(bu" 4 Jesse Luehrs .IP "\(bu" 4 Shawn M Moore .IP "\(bu" 4 יובל קוג'מן (Yuval Kogman) .IP "\(bu" 4 Karen Etheridge .IP "\(bu" 4 Florian Ragwitz .IP "\(bu" 4 Hans Dieter Pearcey .IP "\(bu" 4 Chris Prather .IP "\(bu" 4 Matt S Trout .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2006 by Infinity Interactive, Inc. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.