File: Counter.pm

package info (click to toggle)
libcatmandu-perl 1.2024-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,552 kB
  • sloc: perl: 17,037; makefile: 24; sh: 1
file content (92 lines) | stat: -rw-r--r-- 1,241 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package Catmandu::Counter;

use Catmandu::Sane;

our $VERSION = '1.2024';

use Moo::Role;
use namespace::clean;

has count => (is => 'rwp', default => sub {0});

sub inc_count {
    my ($self, $n) = @_;
    $n //= 1;
    $self->_set_count($self->count + $n);
}

sub dec_count {
    my ($self, $n) = @_;
    $n //= 1;
    my $count = $self->count - $n;
    $self->_set_count($count > 0 ? $count : 0);
}

sub reset_count {
    my $self = $_[0];
    $self->_set_count(0);
}

1;

__END__

=pod

=head1 NAME

Catmandu::Counter - A Base class for modules who need to count things

=head1 SYNOPSIS

    package MyPackage;

    use Moo;

    with 'Catmandu::Counter';

    sub calculate {
        my ($self) = @_;
        $self->inc_count;
        #...do stuff
    }

    package main;

    my $x = MyPackage->new;

    $x->calculate;
    $x->calculate;
    $x->calculate;

    print "Executed calculate %d times\n" , $x->count;

=head1 ATTRIBUTES

=head2 count

The current value of the counter.

=head1 METHODS

=head2 inc_count()

=head2 inc_count(NUMBER)

Increment the counter.

=head2 dec_count()

=head2 dec_count(NUMBER)

Decrement the counter.

=head2 reset_count()

Reset the counter to zero.

=head1 SEE ALSO

L<Catmandu::Exporter>

=cut