MouseX...Counter(3pm)
β¨ 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:
metamethod_provider_classhelper_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
- 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