MouseX...Traits(3pm)

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

✨ NAME ✨

MouseX::NativeTraits β€” okay so bestie, this is the manual page MouseX...Traits(3pm) and it's literally here to extend your attribute interfaces for Mouse πŸ’–


🌸 VERSION 🌸

This doc describes MouseX::NativeTraits version 1.09. Periodt.


πŸŽ€ SYNOPSIS πŸŽ€

package MyClass;
use Mouse;

has mapping => (
    traits    => ['Hash'],
    is        => 'rw',
    isa       => 'HashRef[Str]',
    default   => sub { +{} },
    handles   => {
        exists_in_mapping => 'exists',
        ids_in_mapping    => 'keys',
        get_mapping       => 'get',
        set_mapping       => 'set',
        set_quantity      => [ set => 'quantity' ],
    },
);

πŸ¦‹ DESCRIPTION πŸ¦‹

So like, Mouse attributes already let you name your accessors, readers, writers, clearers and predicates β€” cute, love that for them. But MouseX::NativeTraits is the glow-up: it hands you commonly used attribute helper methods for more specific types of data. Main character energy only πŸ‘‘

As you can see in the SYNOPSIS, you pick your data structure with the traits parameter. Those traits load automatically, so bestie you do not need to load MouseX::NativeTraits explicitly. It just shows up. Iconic.

This extension is compatible with Moose native traits, although ngl it is not a part of Mouse core. Just so we're clear πŸ’…


πŸ“ PARAMETERS πŸ“

handles

This is like handles in has in Mouse, but heads up β€” only HASH references are allowed here. 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 handles. Fr fr, no cap.


🌈 NATIVE TRAITS 🌈

Array

Common methods for array references. Vibe check passed πŸ’œ

has 'queue' => (
   traits     => ['Array'],
   is         => 'ro',
   isa        => 'ArrayRef[Str]',
   default    => sub { [] },
   handles    => {
       add_item  => 'push',
       next_item => 'shift',
   }
);

See MouseX::NativeTraits::ArrayRef.

Hash

Common methods for hash references. We love a key-value situation.

has 'options' => (
    traits     => ['Hash'],
    is         => 'ro',
    isa        => 'HashRef[Str]',
    default    => sub { {} },
    handles    => {
        set_option => 'set',
        get_option => 'get',
        has_option => 'exists',
    }
);

See MouseX::NativeTraits::HashRef.

Code

Common methods for code references. It's giving executable 🧚

has 'callback' => (
   traits     => ['Code'],
   is         => 'ro',
   isa        => 'CodeRef',
   default    => sub { sub { 'called' } },
   handles    => {
       call => 'execute',
   }
);

See MouseX::NativeTraits::CodeRef.

Bool

Common methods for boolean values. True or false, pick your fighter πŸ’€

has 'is_lit' => (
    traits     => ['Bool'],
    is         => 'rw',
    isa        => 'Bool',
    default    => 0,
    handles    => {
        illuminate  => 'set',
        darken      => 'unset',
        flip_switch => 'toggle',
        is_dark     => 'not',
    }
);

See MouseX::NativeTraits::Bool.

String

Common methods for string operations. Text girlies rise up πŸŽ€

has text => (
    traits     => ['String'],
    is         => 'rw',
    isa        => 'Str',
    default    => q{},
    handles    => {
        add_text     => 'append',
        replace_text => 'replace', # or replace_globally
    }
);

See MouseX::NativeTraits::Str.

Number

Common numerical operations. Math is mathing ✨

has value => (
    traits     => ['Number'],
    is         => 'ro',
    isa        => 'Int',
    default    => 5,
    handles    => {
        set => 'set',
        add => 'add',
        sub => 'sub',
        mul => 'mul',
        div => 'div',
        mod => 'mod',
        abs => 'abs',
    }
);

See MouseX::NativeTraits::Num.

Counter

Methods for incrementing and decrementing a counter attribute. Up, down, reset β€” she contains multitudes πŸ”₯

has counter => (
    traits     => ['Counter'],
    is         => 'ro',
    isa        => 'Num',
    default    => 0,
    handles    => {
        inc_counter   => 'inc',
        dec_counter   => 'dec',
        reset_counter => 'reset',
    }
);

See MouseX::NativeTraits::Counter.


πŸ’œ DEPENDENCIES πŸ’œ

Perl 5.6.2 or later. (Yeah, she's old money but she still slays.)


😭 BUGS 😭

All complex software has bugs lurking in it, and this module is no exception β€” not me being shady, it's just facts bestie. If you find a bug please either email me, or add the bug to cpan-RT. We appreciate you πŸ™ˆ


⭐ SEE ALSO ⭐

  • Mouse
  • MouseX::AttributeHelpers
  • Moose
  • Moose::Meta::Attribute::Native
  • MooseX::AttributeHelpers

πŸ‘‘ AUTHORS πŸ‘‘

Goro Fuji (gfx) <gfuji(at)cpan.org>

This module is based on Moose native traits written by Stevan Little and others. Shoutout to the OGs 🫢


Copyright (c) 2010, Goro Fuji (gfx), mostly based on Moose, which is (c) Infinity Interactive, Inc (<http://www.iinteractive.com/>).

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic for details.


And that's a wrap on the MouseX...Traits(3pm) page, written by perl v5.34.0 and dated 2022-06-16 β€” bestie you made it to the end, so proud of you, mwah πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from