File Coverage

blib/lib/Tie/Hash/Cannabinol.pm
Criterion Covered Total %
statement 25 25 100.0
branch 4 4 100.0
condition n/a
subroutine 9 9 100.0
pod n/a
total 38 38 100.0


line stmt bran cond sub pod time code
1             # $Id: Cannabinol.pm 28 2008-03-08 12:01:49Z dave $
2              
3             =head1 NAME
4              
5             Tie::Hash::Cannabinol - Perl extension for creating hashes that forget things
6              
7             =head1 SYNOPSIS
8              
9             use Tie::Hash::Cannabinol;
10              
11             my %hash;
12             tie %hash, 'Tie::Hash::Cannabinol';
13              
14             or
15              
16             my %hash : Stoned;
17              
18             # % hash can now be treated exactly like a normal hash - but don't trust
19             # anything it tells you.
20              
21             =head1 DESCRIPTION
22              
23             Tie::Hash::Cannabinol is a completely useless demonstration of how to use
24             Tie::StdHash to pervert the behaviour of Perl hashes. Once a hash has been
25             Cd to Tie::Hash::Cannabinol, there is a 25% chance that it will forget
26             anything that you tell it immediately and a further 25% chance that it
27             won't be able to retrieve any information you ask it for. Any information
28             that it does return will be pulled at random from its keys.
29              
30             Oh, and the return value from C isn't to be trusted either :)
31              
32             =cut
33              
34             package Tie::Hash::Cannabinol;
35              
36 2     2   61847 use 5.006;
  2         10  
  2         102  
37 2     2   14 use strict;
  2         4  
  2         100  
38 2     2   12 use warnings;
  2         3  
  2         99  
39 2     2   20 use vars qw($VERSION @ISA);
  2         5  
  2         145  
40 2     2   2727 use Tie::Hash;
  2         2690  
  2         90  
41 2     2   2386 use Attribute::Handlers autotie => { "__CALLER__::Stoned" => __PACKAGE__ };
  2         11666  
  2         43  
42              
43             $VERSION = sprintf "%d", '$Revision: 28 $ ' =~ /(\d+)/;
44             @ISA = qw(Tie::StdHash);
45              
46             =head2 STORE
47              
48             Stores data in the hash 3 times out of 4.
49              
50             =cut
51              
52             sub STORE {
53 8     8   3847 my ($self, $key, $val) = @_;
54              
55 8 100       77 return if rand > .75;
56              
57 5         37 $self->{$key} = $val;
58             }
59              
60             =head2 FETCH
61              
62             Fetchs I from the hash 3 times out of 4.
63              
64             =cut
65              
66             sub FETCH {
67 9072     9072   104111 my ($self, $key) = @_;
68              
69 9072 100       21101 return if rand > .75;
70              
71 6753         28583 return $self->{(keys %$self)[rand keys %$self]};
72             }
73              
74             =head2 EXISTS
75              
76             Gives very dodgy information about the existance of keys in the hash.
77              
78             =cut
79              
80             sub EXISTS {
81 6001     6001   30972 return rand > .5;
82             }
83              
84             1;
85             __END__