home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / CacheUtils.pm < prev    next >
Encoding:
Perl POD Document  |  2003-04-15  |  2.9 KB  |  151 lines

  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. use strict;
  14. use vars qw( @ISA @EXPORT_OK );
  15. use Cache::Cache;
  16. use Error;
  17. use Exporter;
  18. use File::Spec;
  19. use 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. use vars ( @EXPORT_OK );
  31.  
  32.  
  33. # throw an Exception if the Assertion fails
  34.  
  35. sub Assert_Defined
  36. {
  37.   if ( not defined $_[0] )
  38.   {
  39.     my ( $package, $filename, $line ) = caller( );
  40.     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.   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.   if ( grep ( /\.\./, @p_elements ) )
  56.   {
  57.     throw Error::Simple( "Illegal path characters '..'" );
  58.   }
  59.  
  60.   return File::Spec->catfile( @p_elements );
  61. }
  62.  
  63.  
  64. # use Storable to clone an object
  65.  
  66. sub Clone_Data
  67. {
  68.   my ( $p_object  ) = @_;
  69.  
  70.   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.   my ( $p_object  ) = @_;
  79.  
  80.   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.   my $type = ref $_[0];
  105.  
  106.   if ( $type and ( $type !~ /^(SCALAR|ARRAY|HASH|CODE|REF|GLOB|LVALUE)$/ ) )
  107.   {
  108.     shift( @_ );
  109.   }
  110.  
  111.   return @_;
  112. }
  113.  
  114.  
  115. # use Storable to thaw an object
  116.  
  117. sub Thaw_Data
  118. {
  119.   my ( $p_frozen_object ) = @_;
  120.  
  121.   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.