=pod Data::Count; Data file for Counter. Copyright wakaba 2001, GNU GPL2. Change: 2001-08-07 wakaba * (_init(), namelist(), list()): New. 2001-06-05 wakaba - New File. Taken from Suika::CGI::Counter. =cut package Data::Count; $VERSION = '1.01'; sub open { my $class = shift; my $file = shift; my $name = shift || 'default'; $name =~ tr/\x0d\x0a\x1f//d; my $self = bless {file => $file, name => $name, error => \&error, count => {}}, $class; $self->_init(); $self; } sub error {} sub _init { my $self = shift; open COUNT, $self->{file} or &{$self->{error}}('open', file => $self->{file}); while () { if (/^([^\x1F\x0D\x0A]+)\x1f(\d+)/) { $self->{count}->{$1} = $2; } } close COUNT; $self; } sub namelist { my $self = shift; keys %{$self->{count}}; } sub list { my $self = shift; %{$self->{count}}; } sub get { my $self = shift; my ($id) = shift || $self->{name}; $id =~ tr/\x0d\x0a\x1f//d; return $self->{count}->{$id} if $self->{count}->{$id}; my ($ret); open COUNT, $self->{file} or &{$self->{error}}('open', file => $self->{file}); while () { if (/^${id}\x1f(\d+)/) { $ret = $1; last; } } close COUNT; $self->{count}->{$id} = $ret; $ret; } sub up { my $self = shift; my ($f, @COUNT) = 0; my ($id) = shift || $self->{name}; $id =~ tr/\x0d\x0a\x1f//d; if (open COUNT, $self->{file}) { @COUNT = ; close COUNT} my $ret; open COUNT, '> '.$self->{file} or &{$self->{error}}('write', file => $self->{file}); for (@COUNT) { if (/^\Q${id}\E\x1f(\d+)/) { $_ = $id."\x1f".($1+1)."\n"; $ret = $1+1; $f = 1; } print COUNT $_; } unless ($f) { print COUNT $id."\x1f1\n"; $ret = 1; } close COUNT; $self->{count}->{$id} = $ret; $self; } 1;