| 1 | package Tie::Filter; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use 5.008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 3 | 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<FETCH> and C<STORE> 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<undef>. 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<undef> 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 | 1 | ▶ | sub TIESCALAR { | ||||||||
|
Subroutine
TIESCALAR: called CC=1 SCAR=0.0
| |||||||||||
| 81 | 1 | my $class = shift; | |||||||||
| 82 | 1 | require Tie::Filter::Scalar; | |||||||||
| 83 | 1 | return Tie::Filter::Scalar->TIESCALAR(@_); | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 1 | ▶ | sub TIEARRAY { | ||||||||
|
Subroutine
TIEARRAY: called CC=1 SCAR=0.0
| |||||||||||
| 87 | 1 | my $class = shift; | |||||||||
| 88 | 1 | require Tie::Filter::Array; | |||||||||
| 89 | 1 | return Tie::Filter::Array->TIEARRAY(@_); | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 1 | ▶ | sub TIEHASH { | ||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||
| 93 | 1 | my $class = shift; | |||||||||
| 94 | 1 | require Tie::Filter::Hash; | |||||||||
| 95 | 1 | return Tie::Filter::Hash->TIEHASH(@_); | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 32 | ▶ | sub _filter { | ||||||||
|
Subroutine
_filter: called CC=2 SCAR=7.2
| |||||||||||
| 99 | 32 | my $code = shift; | |||||||||
| 100 | 32 | ▶ | if (defined $code) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 32 | local $_ = shift; | |||||||||
| 102 | 32 | &$code; | |||||||||
| 103 | 32 | return $_; | |||||||||
| 104 | } else { | ||||||||||
| 105 | 0 | return shift; | |||||||||
| 106 | } | ||||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | =head1 SEE ALSO | ||||||||||
| 110 | |||||||||||
| 111 | L<perltie>, L<Tie::Filter> | ||||||||||
| 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<Tie::Filter::Handle> | ||||||||||
| 120 | package for consideration. Send any ideas or comments to my email address below. | ||||||||||
| 121 | |||||||||||
| 122 | =head1 AUTHOR | ||||||||||
| 123 | |||||||||||
| 124 | Andrew Sterling Hanenkamp, <sterling@hanenkamp.com> | ||||||||||
| 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 | |||||||||||