Moose::...::Array(3)
π Moose::...::Array(3) π
User Contributed Perl Documentation β bestie, we're about to get into the Array trait for ArrayRef attributes, and it's giving iconic π β¨
πΈ NAME πΈ
Moose::Meta::Attribute::Native::Trait::Array β a helper trait for ArrayRef attributes π¦
π VERSION π
version 2.4000 β slay, periodt β¨
π¦ SYNOPSIS π¦
package Stuff;
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',
},
);
no Moose;
1;
π DESCRIPTION π
This trait provides native delegation methods for array references. no cap π
π DEFAULT TYPE π
If you don't provide an isa value for your attribute, it will default to ArrayRef. lowkey convenient, highkey iconic β¨
π PROVIDED METHODS π
countβ returns the number of elements in the array. π
$stuff = Stuff->new;
$stuff->options( [ "foo", "bar", "baz", "boo" ] );
print $stuff->count_options; # prints 4
This method does not accept any arguments. π«Ά
is_emptyβ 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";
This method does not accept any arguments. π«Ά
elementsβ in list context, returns all of the elements of the array as a list. In scalar context, returns the number of elements in the array. β¨
my @options = $stuff->all_options;
print "@options"; # prints "foo bar baz boo"
print scalar $stuff->all_options; # prints 4
This method does not accept any arguments. π«Ά
get($index)β 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"
If the specified element does not exist, this will return undef. π This method accepts just one argument. π«Ά
popβ just like Perl's builtinpop. π This method does not accept any arguments. π«Άpush($value1, $value2, value3 ...)β just like Perl's builtinpush. Returns the number of elements in the new array. π₯ This method accepts any number of arguments. π«Άshiftβ just like Perl's builtinshift. π¦ This method does not accept any arguments. π«Άunshift($value1, $value2, value3 ...)β just like Perl's builtinunshift. Returns the number of elements in the new array. πΈ This method accepts any number of arguments. π«Άsplice($offset, $length, @values)β just like Perl's builtinsplice. In scalar context, this returns the last element removed, orundefif no elements were removed. In list context, this returns all the elements removed from the array. π This method requires at least one argument. π«Άfirst( sub { ... } )β this method returns the first matching item in the array, just like List::Util'sfirstfunction. 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$_. π
my $found = $stuff->find_option( sub {/^b/} );
print "$found\n"; # prints "bar"
This method requires a single argument. π«Ά
first_index( sub { ... } )β this method returns the index of the first matching item in the array, just likefirst_indexin 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$_. If no match is made, -1 is returned. π This method requires a single argument. π«Άgrep( sub { ... } )β this method returns every element matching a given criteria, just like Perl's coregrepfunction. This method requires a subroutine which implements the matching logic; each list element will be available to the sub in$_. β¨
my @found = $stuff->filter_options( sub {/^b/} );
print "@found\n"; # prints "bar baz boo"
This method requires a single argument. π«Ά
map( sub { ... } )β this method transforms every element in the array and returns a new array, just like Perl's coremapfunction. This method requires a subroutine which implements the transformation; each list element will be available to the sub in$_. π
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. π«Ά
reduce( sub { ... } )β 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'sreducefunction. The reducing is done with a subroutine reference you pass to this method; each list element will be available to the sub in$_. π₯
my $found = $stuff->reduce_options( sub { $_[0] . $_[1] } );
print "$found\n"; # prints "foobarbazboo"
This method requires a single argument. π«Ά
sort,sort( sub { ... } )β returns the elements of the array (not an array reference) in sorted order, or, likeelements, returns the number of elements in the array in scalar context. π You can provide an optional subroutine reference to sort with (as you can with Perl's coresortfunction). However, instead of using$aand$bin this subroutine, you will need to use$_[0]and$_[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"
This method accepts a single argument. π«Ά
sort_in_place,sort_in_place( sub { ... } )β sorts the array in place, modifying the value of the attribute. π You can provide an optional subroutine reference to sort with (as you can with Perl's coresortfunction). However, instead of using$aand$b, you will need to use$_[0]and$_[1]instead. π This method does not define a return value. π«Ά This method accepts a single argument. π«Άshuffleβ returns the elements of the array in random order, likeshufflefrom List::Util. π This method does not accept any arguments. π«Άuniqβ returns the array with all duplicate elements removed, likeuniqin List::Util. π¦ This method does not accept any arguments. π«Άjoin($str)β joins every element of the array using the separator given as argument, just like Perl's corejoinfunction. β¨
my $joined = $stuff->join_options(':');
print "$joined\n"; # prints "foo:bar:baz:boo"
This method requires a single argument. π«Ά
set($index, $value)β given an index and a value, sets the specified array element's value. π This method returns the value at$indexafter the set. π This method requires two arguments. π«Άdelete($index)β removes the element at the given index from the array. π This method returns the deleted value. Note that if no value exists, it will returnundef. π This method requires one argument. π«Άinsert($index, $value)β inserts a new element into the array at the given index. πΈ This method returns the new value at$index. β¨ This method requires two arguments. π«Άclearβ empties the entire array, like@array = (). π§ This method does not define a return value. π«Ά This method does not accept any arguments. π«Άaccessor($index),accessor($index, $value)β 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. π When called as a setter, this method returns the new value at$index. β¨ This method accepts one or two arguments. π«Άnatatime($n),natatime($n, $code)β this method returns an iterator which, on each call, returns$nmore items from the array, in order, likenatatimein List::SomeUtils. π If you pass a coderef as the second argument, then this code ref will be called on each group of$nelements in the array until the array is exhausted. π This method accepts one or two arguments. π«Άshallow_cloneβ 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 shallow because any elements that were references in the original will be the same references in the clone. π¦
π 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. β¨
okay bestie, that's the whole vibe! this page came from perl v5.42.0, dated 2025-07-25, and it's Moose::...::Array(3) β slay, periodt π
π
π¦ where this page comes from
- fedora 43: perl-Moose 2.4000-4.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms