MouseX...rayRef(3pm)
π NAME π
MouseX::NativeTraits::ArrayRef β bestie, it's a helper trait for ArrayRef attributes. That's the whole vibe. π
This page lives in the User Contributed Perl Documentation corner of the manual, and honestly? She's serving.
β¨ SYNOPSIS β¨
Okay so picture this. You're in Stuff, you use Mouse, and you want an array attribute that does everything:
package Stuff;
use Mouse;
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',
},
);
Look at that handles block. That's not a config, that's a glow up. π
πΈ DESCRIPTION πΈ
This module hands you an Array attribute that comes with a whole squad of array operations attached. It's giving "I do it all and I do it cute." π¦
π PROVIDED METHODS π
These methods are implemented in MouseX::NativeTraits::MethodProvider::ArrayRef. Every single one, no cap.
countβ Returns the number of elements in the array. That's it, that's the job. β¨
$stuff = Stuff->new;
$stuff->options(["foo", "bar", "baz", "boo"]);
my $count = $stuff->count_options;
print "$count\n"; # prints 4
is_emptyβ Returns a boolean value that is true when the array has no elements. Empty array energy check. π«Ά
$stuff->has_no_options ? die "No options!\n" : print "Good boy.\n";
elementsβ Returns all of the elements of the array. The whole roster, front to back. π
my @option = $stuff->all_options;
print "@options\n"; # prints "foo bar baz boo"
get($index)β Returns an element of the array by its index. And yes bestie, you can use negative index numbers too, just as with Perl's core array handling. Negative indices ate and left no crumbs. π
my $option = $stuff->get_option(1);
print "$option\n"; # prints "bar"
poppush($value1, $value2, value3 ...)shiftunshift($value1, $value2, value3 ...)splice($offset, $length, @values)
These methods are all equivalent to the Perl core functions of the same name. Same names, same behaviour, iconic as always. π₯
first( sub { ... } )β This method returns the first item matching item in the array, just likeList::Util'sfirstfunction. The matching is done with a subroutine reference you pass to this method. The reference will be called against each element in the array until one matches or all elements have been checked. First one to match wins, periodt. π
my $found = $stuff->find_option( sub { /^b/ } );
print "$found\n"; # prints "bar"
any( sub { ... } )β This method returns true if any item in the array meets the criterion given through the subroutine, otherwise returns false. It sets$_for each item in the array. Lowkey the simplest vibe check in the whole module. πΈ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. No sub, no results, that's the deal. π
my @found = $stuff->filter_options( sub { /^b/ } );
print "@found\n"; # prints "bar baz boo"
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. Glow-up generator fr fr. β¨
my @mod_options = $stuff->map_options( sub { $_ . "-tag" } );
print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
apply( sub { ... } )β This method also transform every element in the array and returns a new array, just likeList::MoreUtils'sapplyfunction. It's similar tomap, but does not modify the element of the array. Respectful. Leaves your elements exactly how she found them. π«Άreduce( sub { ... } )β This method condenses an array into a single value, by passing a function the value so far and the next value in the array, just likeList::Util'sreducefunction. The reducing is done with a subroutine reference you pass to this method. Whole squad collapses into one main character. π
my $found = $stuff->reduce_options( sub { $_[0] . $_[1] } );
print "$found\n"; # prints "foobarbazboo"
sort( \&compare )β Returns the array in sorted order.
You can provide an optional subroutine reference to sort with (as you can with Perl's core sort function). However, instead of using $a and $b, you will need to use $_[0] and $_[1] instead. That's the one swap you gotta remember, bestie, don't come for me. π
# 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"
sort_in_place( \&compare )β Sorts the array in place, modifying the value of the attribute. Not a copy. She changes the attribute. Be aware. π
You can provide an optional subroutine reference to sort with (as you can with Perl's core sort function). However, instead of using $a and $b, you will need to use $_[0] and $_[1] instead.
sort_by( \&by, \&compare )β Returns the array in sorted order, applying\&byfunction to each item.
This is equivalent to sort(sub{ by($_[0]) cmp by($_[1]) }), but implemented effectively.
Currently (as of Moose 0.98) this is a Mouse specific method. π
sort_in_place_by( \&by, \&compare )β Sorts the array, applying\&byfunction to each item, modifying the value of the attribute.
This is equivalent to sort_in_place(sub{ by($_[0]) cmp by($_[1]) }), but implemented effectively.
Currently (as of Moose 0.98) this is a Mouse specific method. π
shuffleβ Returns the array, with indices in random order, likeshufflefromList::Util. Chaos, but make it organized. π²uniqβ Returns the array, with all duplicate elements removed, likeuniqfromList::MoreUtils. Duplicates? Never heard of her. π¦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"
set($index, $value)β Given an index and a value, sets the specified array element's value. Targeted. Surgical. πdelete($index)β Removes the element at the given index from the array. Gone, and not coming back. πinsert($index, $value)β Inserts a new element into the array at the given index. Squeezing her into the lineup exactly where you want. β¨clearβ Empties the entire array, like@array = (). Full reset. Fresh start. πΈaccessorβ 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. One arg = read, two args = write. Simple as that. π«Άfor_each(sub{ ... })β This method calls the given subroutine with each element of the array, like Perl's coreforeachstatement.
Currently (as of Moose 0.98) this is a Mouse specific method. π
for_each_pair( sub{ ... } )β This method calls the given subroutine with each two element of the array, as if the array is a list of pairs. Two at a time, buddy system. π―
Currently (as of Moose 0.98) this is a Mouse specific method. π
π METHODS π
Short section, no drama, just the trio:
metamethod_provider_classhelper_type
π SEE ALSO π
MouseX::NativeTraitsβ go read her next, bestie, iykyk. π
And that's the whole page, angel. This doc shipped with perl v5.34.0 and was dated 2022-06-16 β so if you're reading this, thank the 2022 era for the lore. Go forth and push responsibly. π
β¨π
π¦ where this page comes from
- debian bookworm: libmousex-nativetraits-perl 1.09-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmousex-nativetraits-perl 1.09-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmousex-nativetraits-perl 1.09-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmousex-nativetraits-perl 1.09-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmousex-nativetraits-perl 1.09-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms