File Coverage

blib/lib/Data/Keys/E/UniqSet.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package Data::Keys::E::UniqSet;
2              
3             =head1 NAME
4              
5             Data::Keys::E::UniqSet - a key can be set only once
6              
7             =head1 DESCRIPTION
8              
9             One key can be set only once. Second set attempt on a key will throw an
10             exception.
11              
12             =cut
13              
14 1     1   741 use warnings;
  1         1  
  1         25  
15 1     1   4 use strict;
  1         1  
  1         26  
16              
17             our $VERSION = '0.03';
18              
19 1     1   4 use Moose::Role;
  1         1  
  1         5  
20 1     1   3163 use Fcntl qw(:DEFAULT);
  1         2  
  1         331  
21              
22             requires('set', 'lock_ex', 'unlock');
23              
24             around 'set' => sub {
25             my $set = shift;
26             my $self = shift;
27             my $key = shift;
28             my $value = shift;
29            
30             $self->lock_ex($key);
31              
32             # pass through in case of delete
33             $self->$set($key, undef)
34             if not defined $value;
35            
36             die '"'.$key.'" already exists'
37             if $self->get($key);
38            
39             # call set
40             my $ret = $self->$set($key, $value);
41            
42             $self->unlock($key);
43            
44             return $ret;
45             };
46              
47             1;
48              
49              
50             __END__
51              
52             =head1 AUTHOR
53              
54             Jozef Kutej
55              
56             =cut