| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #!/usr/bin/perl | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | =head1 NAME | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | Log::Message::JSON::Hash - L wrapper supporting storing cache | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | use Log::Message::JSON::Hash; | 
| 10 |  |  |  |  |  |  | use JSON; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | tie my %hash, "Log::Message::JSON::Hash"; | 
| 13 |  |  |  |  |  |  | # fill %hash... | 
| 14 |  |  |  |  |  |  | tied(%hash)->cache = encode_json(\%hash); | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # ... | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | print tied(%hash)->cache; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | This class is a proxy to L. It's a valid class to tie hash to, | 
| 23 |  |  |  |  |  |  | and above this the class adds possibility of storing a cache. | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | The cache is cleared on every destructive operation (storing an element, | 
| 26 |  |  |  |  |  |  | deleting an element and clearing whole hash). | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | =cut | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | #----------------------------------------------------------------------------- | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | package Log::Message::JSON::Hash; | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 7 |  |  | 7 |  | 49 | use warnings; | 
|  | 7 |  |  |  |  | 17 |  | 
|  | 7 |  |  |  |  | 347 |  | 
| 35 | 7 |  |  | 7 |  | 48 | use strict; | 
|  | 7 |  |  |  |  | 17 |  | 
|  | 7 |  |  |  |  | 253 |  | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 7 |  |  | 7 |  | 11364 | use Tie::IxHash; | 
|  | 7 |  |  |  |  | 77130 |  | 
|  | 7 |  |  |  |  | 277 |  | 
| 38 | 7 |  |  | 7 |  | 71 | use Carp; | 
|  | 7 |  |  |  |  | 20 |  | 
|  | 7 |  |  |  |  | 5672 |  | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | #----------------------------------------------------------------------------- | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | =head1 API | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | =head2 Own Methods | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | =over | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =cut | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | #----------------------------------------------------------------------------- | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =item C<< new() >> | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | Constructor. | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | =cut | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | sub new { | 
| 59 | 6 |  |  | 6 | 1 | 13 | my ($class, @args) = @_; | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 6 |  |  |  |  | 35 | my $tied = tie my %val, "Tie::IxHash"; | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 6 |  |  |  |  | 113 | my $self = bless { | 
| 64 |  |  |  |  |  |  | tied_object => $tied, | 
| 65 |  |  |  |  |  |  | cache => undef, | 
| 66 |  |  |  |  |  |  | }, $class; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 6 |  |  |  |  | 46 | return $self; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | =item C | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | =item C | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | Get or set cache for this object. | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | Cache will be cleared on any destructive operation performed on this object. | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =cut | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | sub cache { | 
| 82 | 3 |  |  | 3 | 1 | 5 | my ($self, $cache) = @_; | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 3 | 100 |  |  |  | 38 | if (defined $cache) { | 
| 85 | 1 |  |  |  |  | 2 | $self->{cache} = $cache; | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 3 |  |  |  |  | 14 | return $self->{cache}; | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | =back | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | =cut | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | #----------------------------------------------------------------------------- | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | =head2 Methods Satisfying C API | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | All the rest of methods are defined to satisfy API for C function. They | 
| 100 |  |  |  |  |  |  | call appropriate methods of underlying L object. | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | =cut | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | #----------------------------------------------------------------------------- | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | =begin InternalDocs | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | =head3 Creating and destroying objects | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =over | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | =cut | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | #----------------------------------------------------------------------------- | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | sub TIEHASH { | 
| 117 | 6 |  |  | 6 |  | 11 | my ($class, @args) = @_; | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 6 |  |  |  |  | 31 | return $class->new(@args); | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | sub UNTIE { | 
| 123 | 0 |  |  | 0 |  | 0 | my ($self, @args) = @_; | 
| 124 |  |  |  |  |  |  |  | 
| 125 | 0 |  |  |  |  | 0 | $self->{tied_object}->UNTIE(@args); | 
| 126 | 0 |  |  |  |  | 0 | $self->{cache} = undef; | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | =back | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | =cut | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | #----------------------------------------------------------------------------- | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | =head3 Altering object's data | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =over | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | =cut | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | #----------------------------------------------------------------------------- | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | sub STORE { | 
| 144 | 9 |  |  | 9 |  | 205 | my ($self, @args) = @_; | 
| 145 |  |  |  |  |  |  |  | 
| 146 | 9 | 50 |  |  |  | 62 | $self->{cache} = undef if defined $self->{cache}; | 
| 147 |  |  |  |  |  |  |  | 
| 148 | 9 |  |  |  |  | 37 | $self->{tied_object}->STORE(@args); | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | sub DELETE { | 
| 152 | 0 |  |  | 0 |  | 0 | my ($self, @args) = @_; | 
| 153 |  |  |  |  |  |  |  | 
| 154 | 0 | 0 |  |  |  | 0 | $self->{cache} = undef if defined $self->{cache}; | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 0 |  |  |  |  | 0 | $self->{tied_object}->DELETE(@args); | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | sub CLEAR { | 
| 160 | 6 |  |  | 6 |  | 15 | my ($self, @args) = @_; | 
| 161 |  |  |  |  |  |  |  | 
| 162 | 6 | 50 |  |  |  | 27 | $self->{cache} = undef if defined $self->{cache}; | 
| 163 |  |  |  |  |  |  |  | 
| 164 | 6 |  |  |  |  | 40 | $self->{tied_object}->CLEAR(@args); | 
| 165 |  |  |  |  |  |  | } | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | =back | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | =cut | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | #----------------------------------------------------------------------------- | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | =head3 Reading object's data | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | =over | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | =cut | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | #----------------------------------------------------------------------------- | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | sub FETCH { | 
| 182 | 4 |  |  | 4 |  | 8 | my ($self, @args) = @_; | 
| 183 |  |  |  |  |  |  |  | 
| 184 | 4 |  |  |  |  | 15 | $self->{tied_object}->FETCH(@args); | 
| 185 |  |  |  |  |  |  | } | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | sub EXISTS { | 
| 188 | 0 |  |  | 0 |  | 0 | my ($self, @args) = @_; | 
| 189 |  |  |  |  |  |  |  | 
| 190 | 0 |  |  |  |  | 0 | $self->{tied_object}->EXISTS(@args); | 
| 191 |  |  |  |  |  |  | } | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | sub FIRSTKEY { | 
| 194 | 1 |  |  | 1 |  | 2 | my ($self, @args) = @_; | 
| 195 |  |  |  |  |  |  |  | 
| 196 | 1 |  |  |  |  | 5 | $self->{tied_object}->FIRSTKEY(@args); | 
| 197 |  |  |  |  |  |  | } | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | sub NEXTKEY { | 
| 200 | 4 |  |  | 4 |  | 47 | my ($self, @args) = @_; | 
| 201 |  |  |  |  |  |  |  | 
| 202 | 4 |  |  |  |  | 13 | $self->{tied_object}->NEXTKEY(@args); | 
| 203 |  |  |  |  |  |  | } | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | sub SCALAR { | 
| 206 | 0 |  |  | 0 |  |  | my ($self, @args) = @_; | 
| 207 |  |  |  |  |  |  |  | 
| 208 | 0 |  |  |  |  |  | $self->{tied_object}->SCALAR(@args); | 
| 209 |  |  |  |  |  |  | } | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | =back | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | =end InternalDocs | 
| 214 |  |  |  |  |  |  |  | 
| 215 |  |  |  |  |  |  | =cut | 
| 216 |  |  |  |  |  |  |  | 
| 217 |  |  |  |  |  |  | #----------------------------------------------------------------------------- | 
| 218 |  |  |  |  |  |  |  | 
| 219 |  |  |  |  |  |  | =head1 AUTHOR | 
| 220 |  |  |  |  |  |  |  | 
| 221 |  |  |  |  |  |  | Stanislaw Klekot, C<<  >> | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | =head1 LICENSE AND COPYRIGHT | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  | Copyright 2013 Stanislaw Klekot. | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it | 
| 228 |  |  |  |  |  |  | under the terms of either: the GNU General Public License as published | 
| 229 |  |  |  |  |  |  | by the Free Software Foundation; or the Artistic License. | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | See http://dev.perl.org/licenses/ for more information. | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | L, L, C in L | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | =cut | 
| 238 |  |  |  |  |  |  |  | 
| 239 |  |  |  |  |  |  | #----------------------------------------------------------------------------- | 
| 240 |  |  |  |  |  |  | 1; | 
| 241 |  |  |  |  |  |  | # vim:ft=perl |