[Rt-commit] r2550 - in experiments/Bamboo: . ex/trivial/lib
ex/trivial/t
jesse at bestpractical.com
jesse at bestpractical.com
Fri Apr 1 03:52:32 EST 2005
Author: jesse
Date: Fri Apr 1 03:52:32 2005
New Revision: 2550
Modified:
experiments/Bamboo/ (props changed)
experiments/Bamboo/ex/trivial/lib/Counter.pm
experiments/Bamboo/ex/trivial/t/1counter.t
Log:
r10807 at hualien: jesse | 2005-04-01 16:50:38 +0800
* hacky persistent counter
Modified: experiments/Bamboo/ex/trivial/lib/Counter.pm
==============================================================================
--- experiments/Bamboo/ex/trivial/lib/Counter.pm (original)
+++ experiments/Bamboo/ex/trivial/lib/Counter.pm Fri Apr 1 03:52:32 2005
@@ -1,20 +1,23 @@
use strict;
use warnings;
+use YAML;
+
package Counter;
sub new {
my $self = {};
bless $self;
- $self->{'counter'} = 0;
+ my $cnt;
+ eval {$cnt = YAML::LoadFile('/tmp/counter.cnt')};
+ $self->set($cnt||0);
return($self);
}
sub increment {
my $self = shift;
- $self->{counter}++;
-
+ $self->set($self->value + 1);
}
@@ -27,6 +30,7 @@
my $self = shift;
my $new = shift;
$self->{'counter'} = $new;
+ YAML::DumpFile('/tmp/counter.cnt', $self->{'counter'}) || die $@;
}
Modified: experiments/Bamboo/ex/trivial/t/1counter.t
==============================================================================
--- experiments/Bamboo/ex/trivial/t/1counter.t (original)
+++ experiments/Bamboo/ex/trivial/t/1counter.t Fri Apr 1 03:52:32 2005
@@ -5,6 +5,7 @@
use Test::More qw/no_plan/;
use_ok('Counter');
+
my $counter = Counter->new();
isa_ok($counter,'Counter');
@@ -13,14 +14,15 @@
}
+$counter->set(0);
+
is ($counter->value,0);
$counter->increment;
is($counter->value,1);
+$counter->set(5 + $counter->value);
-$counter->set(5);
-
-is($counter->value,5);
+is($counter->value,6);
$counter->increment;
-is($counter->value,6);
+is($counter->value,7);
More information about the Rt-commit
mailing list