File Coverage

blib/lib/Tie/Filter.pm
Criterion Covered Total %
statement 23 24 95.8
branch 1 2 50.0
condition n/a
subroutine 7 7 100.0
pod n/a
total 31 33 93.9


line stmt bran cond sub pod time code
1             package Tie::Filter;
2              
3 3     3   330860 use 5.008;
  3         21  
  3         151  
4 3     3   20 use strict;
  3         5  
  3         113  
5 3     3   64 use warnings;
  3         10  
  3         901  
6              
7             our $VERSION = '1.02';
8              
9             =head1 NAME
10              
11             Tie::Filter - Tie a facade around a scalar, array, or hash
12              
13             =head1 SYNOPSIS
14              
15             use Tie::Filter;
16              
17             # SCALARS
18             my $wrapped;
19             tie $scalar, 'Tie::Filter', \$wrapped,
20             FETCH => sub { $_ = lc },
21             STORE => sub { $_ = uc };
22              
23             # ARRAYS
24             my @wrapped;
25             tie @array, 'Tie::Filter', \@wrapped,
26             FETCH => sub { $_ = uc },
27             STORE => sub { $_ = lc };
28              
29             # HASHES
30             my %wrapped;
31             tie %hash, 'Tie::Filter', \%wrapped,
32             FETCHKEY => sub { $_ = lc },
33             STOREKEY => sub { $_ = uc },
34             FETCHVALUE => sub { $_ = uc },
35             STOREVALUE => sub { $_ = lc };
36              
37             =head1 DESCRIPTION
38              
39             This module ties a facade around a scalar, array, or hash. The facade then
40             filters data as it is being fetched from or stored to the internal object.
41             For scalars, it provides an API for filtering values stored to and fetched
42             from an internal scalar. For arrays, it provides an API for filtering
43             elements stored in an internal array. For hashes, it provides an API for
44             filtering the keys and values stored in the internal hash.
45              
46             This is meant to provide an easy form of syntactic sugar to be built upon
47             other objects. The original purpose of this library was to provide a drop in
48             replacement for DBM filters for general hashes. It is capable of much more.
49              
50             =head2 WRITING FETCH AND STORE
51              
52             The only work that needs to be done is to provide the methods for fetching and
53             storing keys and values. Fetching occurs when taking internal data and using it
54             externally and storing occurs when taking external data and using it internally.
55             (These terms are not directly related to the C and C methods of a
56             tied object.)
57              
58             Each fetch and store method will have the filtered variable set in C<$_>. The
59             method should then modify this variable in place to perform filtering. The
60             return value of the method is ignored.
61              
62             =head1 CAVEATS
63              
64             Be careful that your FETCH/STORE methods will properly handle all possible
65             inputs; be especially careful of C. For example, this might seem safe:
66              
67             # THIS CAUSES AN ERROR
68             use strict;
69             tie %hash, 'Tie::Filter', %wrapped,
70             STOREVALUE => sub { $_ = join ':', @{ $_ } },
71             FETCHVALUE => sub { $_ = [ split /:/, $_ ] };
72             $hash{foo} = undef; # Can't use an undefined value as an ARRAY reference
73              
74             This is also dangerous when using C as elements of an array which will
75             be lost or changed to an empty string upon retrieval depending on the contents
76             of the rest of the array.
77              
78             =cut
79              
80             sub TIESCALAR {
81 1     1   20 my $class = shift;
82 1         731 require Tie::Filter::Scalar;
83 1         7 return Tie::Filter::Scalar->TIESCALAR(@_);
84             }
85              
86             sub TIEARRAY {
87 1     1   23 my $class = shift;
88 1         1475 require Tie::Filter::Array;
89 1         10 return Tie::Filter::Array->TIEARRAY(@_);
90             }
91              
92             sub TIEHASH {
93 1     1   26 my $class = shift;
94 1         872 require Tie::Filter::Hash;
95 1         10 return Tie::Filter::Hash->TIEHASH(@_);
96             }
97              
98             sub _filter {
99 32     32   49 my $code = shift;
100 32 50       67 if (defined $code) {
101 32         55 local $_ = shift;
102 32         62 &$code;
103 32         213 return $_;
104             } else {
105 0           return shift;
106             }
107             }
108              
109             =head1 SEE ALSO
110              
111             L, L
112              
113             =head1 TO DO
114              
115             I would like to add support for creating filters around file handles with a
116             similar interface, but this is a much more complicated problem then the creation
117             of a facade around scalars, arrays, and hashes.
118              
119             Anyone who is interested is welcome to contribute a C
120             package for consideration. Send any ideas or comments to my email address below.
121              
122             =head1 AUTHOR
123              
124             Andrew Sterling Hanenkamp,
125              
126             =head1 LICENSE AND COPYRIGHT
127              
128             Copyright 2003 Andrew Sterling Hanenkamp. All Rights Reserved. This library is
129             free software; you can redistribute it and/or modify it under the same terms as
130             Perl itself.
131              
132             =cut
133              
134