MouseX...rayRef(3pm)

πŸ“¦
version 1.09 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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"
  • pop
  • push($value1, $value2, value3 ...)
  • shift
  • unshift($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 like List::Util's first function. 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 core grep function. 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 core map function. 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 like List::MoreUtils's apply function. It's similar to map, 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 like List::Util's reduce function. 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 \&by function 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 \&by function 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, like shuffle from List::Util. Chaos, but make it organized. 🎲
  • uniq β€” Returns the array, with all duplicate elements removed, like uniq from List::MoreUtils. Duplicates? Never heard of her. πŸ¦‹
  • join($str) β€” Joins every element of the array using the separator given as argument, just like Perl's core join function.
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 core foreach statement.

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:

  • meta
  • method_provider_class
  • helper_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