| 1 | package Catmandu::Counter; | ||||||||||
| 2 | |||||||||||
| 3 | 21 | ▶ | use Catmandu::Sane; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 21 | our $VERSION = '1.0201_02'; | |||||||||
| 6 | |||||||||||
| 7 | 21 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 21 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 65 | ▶ | has count => (is => 'rwp', default => sub {0}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | sub inc_count { | ||||||||||
| 13 | 267 | ▶ | my $self = $_[0]; | ||||||||
|
Subroutine
inc_count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 14 | 267 | $self->_set_count($self->count + 1); | |||||||||
| 15 | } | ||||||||||
| 16 | |||||||||||
| 17 | sub dec_count { | ||||||||||
| 18 | 2 | ▶ | my $self = $_[0]; | ||||||||
|
Subroutine
dec_count: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 19 | 2 | ▶ | $self->count ? $self->_set_count($self->count - 1) : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | sub reset_count { | ||||||||||
| 23 | 1 | ▶ | my $self = $_[0]; | ||||||||
|
Subroutine
reset_count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 24 | 1 | $self->_set_count(0); | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | 21 | 1; | |||||||||
| 28 | |||||||||||
| 29 | __END__ | ||||||||||
| 30 | |||||||||||
| 31 | =pod | ||||||||||
| 32 | |||||||||||
| 33 | =head1 NAME | ||||||||||
| 34 | |||||||||||
| 35 | Catmandu::Counter - A Base class for modules who need to count things | ||||||||||
| 36 | |||||||||||
| 37 | =head1 SYNOPSIS | ||||||||||
| 38 | |||||||||||
| 39 | package MyPackage; | ||||||||||
| 40 | |||||||||||
| 41 | use Moo; | ||||||||||
| 42 | |||||||||||
| 43 | with 'Catmandu::Counter'; | ||||||||||
| 44 | |||||||||||
| 45 | sub calculate { | ||||||||||
| 46 | my ($self) = @_; | ||||||||||
| 47 | $self->inc_count; | ||||||||||
| 48 | #...do stuff | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | package main; | ||||||||||
| 52 | |||||||||||
| 53 | my $x = MyPackage->new; | ||||||||||
| 54 | |||||||||||
| 55 | $x->calculate; | ||||||||||
| 56 | $x->calculate; | ||||||||||
| 57 | $x->calculate; | ||||||||||
| 58 | |||||||||||
| 59 | print "Executed calculate %d times\n" , $x->count; | ||||||||||
| 60 | |||||||||||
| 61 | =head1 ATTRIBUTES | ||||||||||
| 62 | |||||||||||
| 63 | =head2 count | ||||||||||
| 64 | |||||||||||
| 65 | The current value of the counter. | ||||||||||
| 66 | |||||||||||
| 67 | =head1 METHODS | ||||||||||
| 68 | |||||||||||
| 69 | =head2 inc_count() | ||||||||||
| 70 | |||||||||||
| 71 | =head2 inc_count(NUMBER) | ||||||||||
| 72 | |||||||||||
| 73 | Increment the counter. | ||||||||||
| 74 | |||||||||||
| 75 | =head2 dec_count() | ||||||||||
| 76 | |||||||||||
| 77 | =head2 dec_count(NUMBER) | ||||||||||
| 78 | |||||||||||
| 79 | Decrement the counter. | ||||||||||
| 80 | |||||||||||
| 81 | =head2 reset_count() | ||||||||||
| 82 | |||||||||||
| 83 | Reset the counter to zero. | ||||||||||
| 84 | |||||||||||
| 85 | =head1 SEE ALSO | ||||||||||
| 86 | |||||||||||
| 87 | L<Catmandu::Exporter> | ||||||||||
| 88 | |||||||||||
| 89 | =cut | ||||||||||