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
11package Cache::CacheUtils;
12
13use strict;
14use vars qw( @ISA @EXPORT_OK );
15use Cache::Cache;
16use Error;
17use Exporter;
18use File::Spec;
19use Storable qw( nfreeze thaw dclone );
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# throw an Exception if the Assertion fails
31
32sub Assert_Defined
33{
34  if ( not defined $_[0] )
35  {
36    my ( $package, $filename, $line ) = caller( );
37    throw Error::Simple( "Assert_Defined failed: $package line $line\n" );
38  }
39}
40
41
42# Take a list of directory components and create a valid path
43
44sub Build_Path
45{
46  my ( @p_elements ) = @_;
47
48  # TODO: add this to Untaint_Path or something
49  #  ( $p_unique_key !~ m|[0-9][a-f][A-F]| ) or
50  #  throw Error::Simple( "key '$p_unique_key' contains illegal characters'" );
51
52  if ( grep ( /\.\./, @p_elements ) )
53  {
54    throw Error::Simple( "Illegal path characters '..'" );
55  }
56
57  return File::Spec->catfile( @p_elements );
58}
59
60
61# use Storable to clone an object
62
63sub Clone_Data
64{
65  my ( $p_object  ) = @_;
66
67  return defined $p_object ? dclone( $p_object ) : undef;
68}
69
70
71# use Storable to freeze an object
72
73sub Freeze_Data
74{
75  my ( $p_object  ) = @_;
76
77  return defined $p_object ? nfreeze( $p_object ) : undef;
78}
79
80
81# Take a parameter list and automatically shift it such that if
82# the method was called as a static method, then $self will be
83# undefined.  This allows the use to write
84#
85#   sub Static_Method
86#   {
87#     my ( $parameter ) = Static_Params( @_ );
88#   }
89#
90# and not worry about whether it is called as:
91#
92#   Class->Static_Method( $param );
93#
94# or
95#
96#   Class::Static_Method( $param );
97
98
99sub Static_Params
100{
101  my $type = ref $_[0];
102
103  if ( $type and ( $type !~ /^(SCALAR|ARRAY|HASH|CODE|REF|GLOB|LVALUE)$/ ) )
104  {
105    shift( @_ );
106  }
107
108  return @_;
109}
110
111
112# use Storable to thaw an object
113
114sub Thaw_Data
115{
116  my ( $p_frozen_object ) = @_;
117
118  return defined $p_frozen_object ? thaw( $p_frozen_object ) : undef;
119}
120
121
1221;
123
124
125__END__
126
127=pod
128
129=head1 NAME
130
131Cache::CacheUtils -- miscellaneous utility routines
132
133=head1 DESCRIPTION
134
135The CacheUtils package is a collection of static methods that provide
136functionality useful to many different classes.
137
138=head1 AUTHOR
139
140Original author: DeWitt Clinton <dewitt@unto.net>
141
142Last author:     $Author: dclinton $
143
144Copyright (C) 2001-2003 DeWitt Clinton
145
146=cut
147
148