| 1 | # ----------------------------------------------------------------------------- | ||||||||||
| 2 | |||||||||||
| 3 | =encoding utf8 | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Quiq::LockedCounter - Persistenter Zähler mit Lock | ||||||||||
| 8 | |||||||||||
| 9 | =head1 BASE CLASS | ||||||||||
| 10 | |||||||||||
| 11 | L<Quiq::Object> | ||||||||||
| 12 | |||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||
| 14 | |||||||||||
| 15 | use Quiq::LockedCounter; | ||||||||||
| 16 | |||||||||||
| 17 | $cnt = Quiq::LockedCounter->new($file)->increment; | ||||||||||
| 18 | ... | ||||||||||
| 19 | $n = $cnt->count; | ||||||||||
| 20 | |||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||
| 22 | |||||||||||
| 23 | Die Klasse realisiert einen Zähler mit Exklusiv-Lock. Der | ||||||||||
| 24 | Zählerstand wird in einer Datei gespeichert. Die Datei wird | ||||||||||
| 25 | gelockt. Der Lock wird bis zur Destrukturierung des Objekts | ||||||||||
| 26 | gehalten. | ||||||||||
| 27 | |||||||||||
| 28 | =cut | ||||||||||
| 29 | |||||||||||
| 30 | # ----------------------------------------------------------------------------- | ||||||||||
| 31 | |||||||||||
| 32 | package Quiq::LockedCounter; | ||||||||||
| 33 | 3 | ▶ | use base qw/Quiq::Object/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | 3 | ▶ | use v5.10; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | 3 | our $VERSION = '1.240'; | |||||||||
| 40 | |||||||||||
| 41 | 3 | ▶ | use Quiq::FileHandle; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | |||||||||||
| 43 | # ----------------------------------------------------------------------------- | ||||||||||
| 44 | |||||||||||
| 45 | =head1 METHODS | ||||||||||
| 46 | |||||||||||
| 47 | =head2 Konstruktor | ||||||||||
| 48 | |||||||||||
| 49 | =head3 new() - Instantiiere Counter | ||||||||||
| 50 | |||||||||||
| 51 | =head4 Synopsis | ||||||||||
| 52 | |||||||||||
| 53 | $ctr = Quiq::LockedCounter->new($file); | ||||||||||
| 54 | |||||||||||
| 55 | =head4 Returns | ||||||||||
| 56 | |||||||||||
| 57 | Objekt | ||||||||||
| 58 | |||||||||||
| 59 | =cut | ||||||||||
| 60 | |||||||||||
| 61 | # ----------------------------------------------------------------------------- | ||||||||||
| 62 | |||||||||||
| 63 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 64 | 1 | my ($class,$file) = @_; | |||||||||
| 65 | |||||||||||
| 66 | 1 | my $fh = Quiq::FileHandle->open('+>>',$file,-lock=>'EX'); | |||||||||
| 67 | 1 | $fh->autoFlush; | |||||||||
| 68 | 1 | $fh->seek(0); | |||||||||
| 69 | 1 | my $count = <$fh>; | |||||||||
| 70 | 1 | ▶ | if ($count) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 0 | chomp $count; | |||||||||
| 72 | } | ||||||||||
| 73 | else { | ||||||||||
| 74 | 1 | $fh->truncate; | |||||||||
| 75 | 1 | $fh->print("0\n"); | |||||||||
| 76 | 1 | $count = 0; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 1 | return bless [$file,$fh,$count],$class; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | # ----------------------------------------------------------------------------- | ||||||||||
| 83 | |||||||||||
| 84 | =head2 Operationen | ||||||||||
| 85 | |||||||||||
| 86 | =head3 count() - Liefere Zählerstand | ||||||||||
| 87 | |||||||||||
| 88 | =head4 Synopsis | ||||||||||
| 89 | |||||||||||
| 90 | $n = $ctr->count; | ||||||||||
| 91 | |||||||||||
| 92 | =head4 Returns | ||||||||||
| 93 | |||||||||||
| 94 | Zählerstand (Integer) | ||||||||||
| 95 | |||||||||||
| 96 | =cut | ||||||||||
| 97 | |||||||||||
| 98 | # ----------------------------------------------------------------------------- | ||||||||||
| 99 | |||||||||||
| 100 | 4 | ▶ | sub count { | ||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 101 | 4 | return shift->[2]; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | # ----------------------------------------------------------------------------- | ||||||||||
| 105 | |||||||||||
| 106 | =head3 file() - Liefere Dateipfad | ||||||||||
| 107 | |||||||||||
| 108 | =head4 Synopsis | ||||||||||
| 109 | |||||||||||
| 110 | $file = $ctr->file; | ||||||||||
| 111 | |||||||||||
| 112 | =head4 Returns | ||||||||||
| 113 | |||||||||||
| 114 | Dateipfad (String) | ||||||||||
| 115 | |||||||||||
| 116 | =cut | ||||||||||
| 117 | |||||||||||
| 118 | # ----------------------------------------------------------------------------- | ||||||||||
| 119 | |||||||||||
| 120 | 1 | ▶ | sub file { | ||||||||
|
Subroutine
file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 121 | 1 | return shift->[0]; | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | # ----------------------------------------------------------------------------- | ||||||||||
| 125 | |||||||||||
| 126 | =head3 increment() - Inkrementiere Zählerstand | ||||||||||
| 127 | |||||||||||
| 128 | =head4 Synopsis | ||||||||||
| 129 | |||||||||||
| 130 | $ctr = $ctr->increment; | ||||||||||
| 131 | |||||||||||
| 132 | =head4 Returns | ||||||||||
| 133 | |||||||||||
| 134 | Objekt | ||||||||||
| 135 | |||||||||||
| 136 | =cut | ||||||||||
| 137 | |||||||||||
| 138 | # ----------------------------------------------------------------------------- | ||||||||||
| 139 | |||||||||||
| 140 | 3 | ▶ | sub increment { | ||||||||
|
Subroutine
increment: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 141 | 3 | my $self = shift; | |||||||||
| 142 | |||||||||||
| 143 | 3 | my $fh = $self->[1]; | |||||||||
| 144 | 3 | $fh->truncate; | |||||||||
| 145 | 3 | $fh->print(++$self->[2],"\n"); | |||||||||
| 146 | |||||||||||
| 147 | 3 | return $self; | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | # ----------------------------------------------------------------------------- | ||||||||||
| 151 | |||||||||||
| 152 | =head1 VERSION | ||||||||||
| 153 | |||||||||||
| 154 | 1.240 | ||||||||||
| 155 | |||||||||||
| 156 | =head1 AUTHOR | ||||||||||
| 157 | |||||||||||
| 158 | Frank Seitz, L<http://fseitz.de/> | ||||||||||
| 159 | |||||||||||
| 160 | =head1 COPYRIGHT | ||||||||||
| 161 | |||||||||||
| 162 | Copyright (C) 2026 Frank Seitz | ||||||||||
| 163 | |||||||||||
| 164 | =head1 LICENSE | ||||||||||
| 165 | |||||||||||
| 166 | This code is free software; you can redistribute it and/or modify | ||||||||||
| 167 | it under the same terms as Perl itself. | ||||||||||
| 168 | |||||||||||
| 169 | =cut | ||||||||||
| 170 | |||||||||||
| 171 | # ----------------------------------------------------------------------------- | ||||||||||
| 172 | |||||||||||
| 173 | 3 | 1; | |||||||||
| 174 | |||||||||||
| 175 | # eof | ||||||||||