MouseX...Counter(3pm)

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

✨ MouseX::NativeTraits::Counter β€” the helper trait for counter attributes ✨

okay bestie, we're in the MouseX...Counter(3pm) zone, straight outta the User Contributed Perl Documentation πŸ’… this one's a little gem, let's get into it


πŸ’– NAME πŸ’–

MouseX::NativeTraits::Counter β€” it's the helper trait for counter attributes. that's it, that's the whole job, and it does it flawlessly πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

here's the vibe, copy-paste ready, no notes:

package MyHomePage;
use Mouse;

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

my $page = MyHomePage->new();
$page->inc_counter; # same as $page->counter( $page->counter + 1 );
$page->dec_counter; # same as $page->counter( $page->counter - 1 );

see how the handles block just hands you named methods and they map onto inc, dec and reset? iconic. main character energy fr fr πŸ‘‘


🌸 DESCRIPTION 🌸

so what this module does is give you a simple counter attribute β€” one you can increment and decrement. that's the whole arc, no plot twists 🧚

and here's the lowkey best part: if your attribute definition doesn't include any of is, isa, default or handles β€” but it does use the "Counter" trait β€” then this module just goes ahead and applies the defaults for you, exactly like in the SYNOPSIS up there. which means a truly bare-bones counter definition is legal and it slays:

has 'foo' => (traits => ['Counter']);
$obj->inc_foo;

that's it. two lines. obsessed. πŸ’–


πŸŽ€ PROVIDED METHODS πŸŽ€

okay so all of these live in MouseX::NativeTraits::MethodProvider::Counter. and important, do not skip this: every single one of these methods does in-place modification of the value stored in the attribute. like, they're not handing you back a new shiny object, they're reaching into the attribute and changing it. noted? noted. πŸ’…

  • set($value) β€” sets the counter to the value you passed in. done, dusted.
  • inc β€” bumps the value stored in this slot up by 1. and if you hand it an argument, the counter goes up by that specified amount instead. flexible king πŸ‘‘
  • dec β€” drops the value stored in this slot down by 1. and if you hand it an argument, the counter gets increased by the specified amount. yep, that's exactly what the original says, we're not editorializing, that's the text bestie πŸ™ˆ
  • reset β€” resets the value stored in this slot back to it's default value. (their apostrophe, not mine 🫢)

πŸ’œ METHODS πŸ’œ

these are the ones hanging around on the meta side:

  • meta
  • method_provider_class
  • helper_type

🌈 SEE ALSO 🌈

  • MouseX::NativeTraits β€” go read it, it's the whole family, you'll love it πŸ’–

and that's a wrap!! this page is from perl v5.34.0, dated 2022-06-16, and it's MouseX...Counter(3pm) all day. go forth and count things, bestie πŸŒŸπŸ“ no cap.

πŸ“¦ where this page comes from