File Coverage

blib/lib/Cache/CacheUtils.pm
Criterion Covered Total %
statement 38 41 92.7
branch 8 12 66.7
condition 2 3 66.7
subroutine 14 14 100.0
pod 0 6 0.0
total 62 76 81.6


line stmt bran cond sub pod time code
1             ######################################################################
2             # $Id: CacheUtils.pm,v 1.39 2003/04/15 14:46:19 dclinton Exp $
3             # Copyright (C) 2001-2003 DeWitt Clinton All Rights Reserved
4             #
5             # Software distributed under the License is distributed on an "AS
6             # IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed or
7             # implied. See the License for the specific language governing
8             # rights and limitations under the License.
9             ######################################################################
10              
11             package Cache::CacheUtils;
12              
13 4     4   60 use strict;
  4         57  
  4         59  
14 4     4   59 use vars qw( @ISA @EXPORT_OK );
  4         36  
  4         56  
15 4     4   68 use Cache::Cache;
  4         158  
  4         86  
16 4     4   64 use Error;
  4         38  
  4         57  
17 4     4   71 use Exporter;
  4         73  
  4         52  
18 4     4   59 use File::Spec;
  4         39  
  4         83  
19 4     4   4431 use Storable qw( nfreeze thaw dclone );
  4         59  
  4         83  
20              
21             @ISA = qw( Exporter );
22              
23             @EXPORT_OK = qw( Assert_Defined
24             Build_Path
25             Clone_Data
26             Freeze_Data
27             Static_Params
28             Thaw_Data );
29              
30 4     4   68 use vars ( @EXPORT_OK );
  4         35  
  4         58  
31              
32              
33             # throw an Exception if the Assertion fails
34              
35             sub Assert_Defined
36             {
37 10513 50   10513 0 301456   if ( not defined $_[0] )
38               {
39 0         0     my ( $package, $filename, $line ) = caller( );
40 0         0     throw Error::Simple( "Assert_Defined failed: $package line $line\n" );
41               }
42             }
43              
44              
45             # Take a list of directory components and create a valid path
46              
47             sub Build_Path
48             {
49 1120     1120 0 25147   my ( @p_elements ) = @_;
50              
51             # TODO: add this to Untaint_Path or something
52             # ( $p_unique_key !~ m|[0-9][a-f][A-F]| ) or
53             # throw Error::Simple( "key '$p_unique_key' contains illegal characters'" );
54              
55 1120 50       21632   if ( grep ( /\.\./, @p_elements ) )
56               {
57 0         0     throw Error::Simple( "Illegal path characters '..'" );
58               }
59              
60 1120         29152   return File::Spec->catfile( @p_elements );
61             }
62              
63              
64             # use Storable to clone an object
65              
66             sub Clone_Data
67             {
68 289     289 0 4940   my ( $p_object ) = @_;
69              
70 289 100       64019   return defined $p_object ? dclone( $p_object ) : undef;
71             }
72              
73              
74             # use Storable to freeze an object
75              
76             sub Freeze_Data
77             {
78 86     86 0 842   my ( $p_object ) = @_;
79              
80 86 50       1177   return defined $p_object ? nfreeze( $p_object ) : undef;
81             }
82              
83              
84             # Take a parameter list and automatically shift it such that if
85             # the method was called as a static method, then $self will be
86             # undefined. This allows the use to write
87             #
88             # sub Static_Method
89             # {
90             # my ( $parameter ) = Static_Params( @_ );
91             # }
92             #
93             # and not worry about whether it is called as:
94             #
95             # Class->Static_Method( $param );
96             #
97             # or
98             #
99             # Class::Static_Method( $param );
100              
101              
102             sub Static_Params
103             {
104 191     191 0 2045   my $type = ref $_[0];
105              
106 191 100 66     3294   if ( $type and ( $type !~ /^(SCALAR|ARRAY|HASH|CODE|REF|GLOB|LVALUE)$/ ) )
107               {
108 26         8611     shift( @_ );
109               }
110              
111 191         2366   return @_;
112             }
113              
114              
115             # use Storable to thaw an object
116              
117             sub Thaw_Data
118             {
119 151     151 0 1467   my ( $p_frozen_object ) = @_;
120              
121 151 50       4153   return defined $p_frozen_object ? thaw( $p_frozen_object ) : undef;
122             }
123              
124              
125             1;
126              
127              
128             __END__
129            
130             =pod
131            
132             =head1 NAME
133            
134             Cache::CacheUtils -- miscellaneous utility routines
135            
136             =head1 DESCRIPTION
137            
138             The CacheUtils package is a collection of static methods that provide
139             functionality useful to many different classes.
140            
141             =head1 AUTHOR
142            
143             Original author: DeWitt Clinton <dewitt@unto.net>
144            
145             Last author: $Author: dclinton $
146            
147             Copyright (C) 2001-2003 DeWitt Clinton
148            
149             =cut
150            
151