File Coverage

blib/lib/Tie/ExecHash.pm
Criterion Covered Total %
statement 26 27 96.3
branch 7 8 87.5
condition 5 9 55.5
subroutine 7 7 100.0
pod n/a
total 45 51 88.2


line stmt bran cond sub pod time code
1             # ABSTRACT: Give special powers to only some keys in a hash
2             package Tie::ExecHash;
3             {
4             $Tie::ExecHash::VERSION = '0.91';
5             }
6 1     1   35770 use Tie::Hash;
  1         1184  
  1         33  
7 1     1   8 use strict;
  1         2  
  1         51  
8 1     1   6 use warnings;
  1         2  
  1         47  
9              
10 1     1   382 BEGIN { our @ISA = qw( Tie::ExtraHash ); }
11              
12             sub STORE {
13 3 100 66 3   1108 if ( ref $_[2] eq 'ARRAY'
  1 100 66     92  
      33        
14             and @{ $_[2] } == 2
15             and ref $_[2][0] eq 'CODE'
16             and ref $_[2][1] eq 'CODE' )
17             {
18 1         5 $_[0][1]{'set'}{ $_[1] } = $_[2][0];
19 1         5 $_[0][1]{'get'}{ $_[1] } = $_[2][1];
20 1         7 $_[0]->SUPER::STORE( $_[1], q{} );
21             }
22             elsif ( exists $_[0][1]{'set'}{ $_[1] } ) {
23 1         2 return &{ $_[0][1]{'set'}{ $_[1] } }( $_[2] );
  1         4  
24             }
25             else {
26 1         9 shift->SUPER::STORE(@_);
27             }
28             }
29              
30             sub FETCH {
31 2 100   2   77 if ( exists $_[0][1]{'get'}{ $_[1] } ) {
32 1         3 return &{ $_[0][1]{'get'}{ $_[1] } }();
  1         6  
33             }
34             else {
35 1         7 shift->SUPER::FETCH(@_);
36             }
37             }
38              
39             sub DELETE {
40 1 50   1   411 if ( exists $_[0][1]{'set'}{ $_[1] } ) {
41 1         10 $_[0]->SUPER::DELETE( $_[1] );
42 1         7 return &{ $_[0][1]{'set'}{ $_[1] } }();
  1         4  
43             }
44             else {
45 0           shift->SUPER::DELETE(@_);
46             }
47             }
48              
49              
50             1;
51              
52              
53             =pod
54              
55             =head1 NAME
56              
57             Tie::ExecHash - Give special powers to only some keys in a hash
58              
59             =head1 VERSION
60              
61             version 0.91
62              
63             =head1 SYNOPSIS
64              
65             use Tie::ExecHash;
66              
67             my %foo = ();
68             tie( %foo, 'Tie::ExecHash');
69              
70             $foo{'bar'} = 1;
71             print "$foo{'bar'}\n"; # 1
72              
73             my $baz = "red";
74              
75             $foo{'bar'} = [ sub { $baz = $_[0] }, sub { $baz } ];
76              
77             print "$foo{'bar'}\n"; # red
78              
79             $foo{'bar'} = "a suffusion of yellow";
80              
81             print "$baz\n"; # a suffusion of yellow
82              
83             =head1 DESCRIPTION
84              
85             What this does is allow you to have some hash values act like they are tied
86             scalars without actually having to go through the trouble of making them
87             really be tied scalars.
88              
89             By default the tied hash works exactly like a normal hash. Its behavior
90             changes when you assign a value of an arrayref with exactly two code blocks
91             in it. When you do this it uses the first as the get routine and the second
92             as the set routine. Any future gets or sets to this key will be mediated
93             via these subroutines.
94              
95             =head1 SEE ALSO
96              
97             L
98              
99             L
100              
101             =head1 HISTORY
102              
103             This was originally written as part of the development of "friv", the command
104             line client for the Volity project. L
105              
106             =head1 AUTHOR
107              
108             Becca
109              
110             =head1 COPYRIGHT AND LICENSE
111              
112             This software is copyright (c) 2011 by Rebecca Turner.
113              
114             This is free software; you can redistribute it and/or modify it under
115             the same terms as the Perl 5 programming language system itself.
116              
117             =cut
118              
119              
120             __END__