File-CounterFile

 view release on metacpan or  search on metacpan

CounterFile.pm  view on Meta::CPAN

	seek(F, 0, SEEK_SET);
	print F $MAGIC;
	print F "$initial\n";
	$value = $initial;
    }
    close(F) || croak("Can't close $file: $!");

    bless { file    => $file,  # the filename for the counter
	   'value'  => $value, # the current value
	    updated => 0,      # flag indicating if value has changed
	    # handle => XXX,   # file handle symbol. Only present when locked
	  };
}


sub locked
{
    exists shift->{handle};
}


sub lock
{
    my($self) = @_;
    $self->unlock if $self->locked;

    my $fh = gensym();
    my $file = $self->{file};

    open($fh, "+<$file") or croak "Can't open $file: $!";
    flock($fh, LOCK_EX) or croak "Can't flock: $!";  # 2 = exlusive lock

    local($/) = "\n";
    my $magic = <$fh>;
    if ($magic ne $MAGIC) {

CounterFile.pm  view on Meta::CPAN


    $self->{handle}  = $fh;
    $self->{updated} = 0;
    $self;
}


sub unlock
{
    my($self) = @_;
    return unless $self->locked;

    my $fh = $self->{handle};

    if ($self->{updated}) {
	# write back new value
	local($\) = undef;
	seek($fh, 0, SEEK_SET) or croak "Can't seek to beginning: $!";
	print $fh $MAGIC;
	print $fh "$self->{'value'}\n";
    }

CounterFile.pm  view on Meta::CPAN

    close($fh) or warn "Can't close: $!";
    delete $self->{handle};
    $self;
}


sub inc
{
    my($self) = @_;

    if ($self->locked) {
	$self->{'value'}++;
	$self->{updated} = 1;
    } else {
	$self->lock;
	$self->{'value'}++;
	$self->{updated} = 1;
	$self->unlock;
    }
    $self->{'value'}; # return value
}


sub dec
{
    my($self) = @_;

    if ($self->locked) {
	unless ($self->{'value'} =~ /^\d+$/) {
	    $self->unlock;
	    croak "Autodecrement is not magical in perl";
	}
	$self->{'value'}--;
	$self->{updated} = 1;
    }
    else {
	$self->lock;
	unless ($self->{'value'} =~ /^\d+$/) {

CounterFile.pm  view on Meta::CPAN

	$self->unlock;
    }
    $self->{'value'}; # return value
}


sub value
{
    my($self) = @_;
    my $value;
    if ($self->locked) {
	$value = $self->{'value'};
    }
    else {
	$self->lock;
	$value = $self->{'value'};
	$self->unlock;
    }
    $value;
}

CounterFile.pm  view on Meta::CPAN

when the file is created (i.e. it does not exist before the call).

When you call the C<inc()> method, you increment the counter value by
one. When you call C<dec()>, the counter value is decremented.  In both
cases the new value is returned.  The C<dec()> method only works for
numerical counters (digits only).

You can peek at the value of the counter (without incrementing it) by
using the C<value()> method.

The counter can be locked and unlocked with the C<lock()> and
C<unlock()> methods.  Incrementing and value retrieval are faster when
the counter is locked, because we do not have to update the counter
file all the time.  You can query whether the counter is locked with
the C<locked()> method.

There is also an operator overloading interface to the
File::CounterFile object.  This means that you can use the C<++>
operator for incrementing and the C<--> operator for decrementing the counter,
and you can interpolate counters directly into strings.

=head1 COPYRIGHT

Copyright (c) 1995-1998,2002,2003 Gisle Aas. All rights reserved.

README  view on Meta::CPAN

    is created (i.e. it does not exist before the call).

    When you call the "inc()" method, you increment the counter value by
    one. When you call "dec()" the counter value is decrementd. In both
    cases the new value is returned. The "dec()" method only works for
    numerical counters (digits only).

    You can peek at the value of the counter (without incrementing it) by
    using the "value()" method.

    The counter can be locked and unlocked with the "lock()" and "unlock()"
    methods. Incrementing and value retrieval is faster when the counter is
    locked, because we do not have to update the counter file all the time.
    You can query whether the counter is locked with the "locked()" method.

    There is also an operator overloading interface to the File::CounterFile
    object. This means that you might use the "++" operator for incrementing
    the counter, "--" operator for decrementing and you can interpolate
    counters diretly into strings.

COPYRIGHT
    Copyright (c) 1995-1998,2002,2003 Gisle Aas. All rights reserved.

    This library is free software; you can redistribute it and/or modify it



( run in 0.889 second using v1.01-cache-2.11-cpan-49f99fa48dc )