ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Cache/CacheUtilities.pm
Revision: 1.7
Committed: Mon Sep 11 13:48:33 2006 UTC (18 years, 7 months ago) by sashby
Content type: text/plain
Branch: MAIN
CVS Tags: V1_0_3-p4, V1_0_3-p3, V1_0_3-p2, before110xmlBRmerge, V1_0_3-p1, V1_0_3
Branch point for: v103_with_xml
Changes since 1.6: +4 -4 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 sashby 1.2 #____________________________________________________________________
2     # File: CacheUtilities.pm
3     #____________________________________________________________________
4     #
5     # Author: Shaun Ashby <Shaun.Ashby@cern.ch>
6     # Update: 2003-10-30 11:51:58+0100
7 sashby 1.7 # Revision: $Id: CacheUtilities.pm,v 1.6.2.3 2006/09/11 11:19:58 sashby Exp $
8 sashby 1.2 #
9     # Copyright: 2003 (C) Shaun Ashby
10     #
11     #--------------------------------------------------------------------
12 sashby 1.5
13     =head1 NAME
14    
15     Cache::CacheUtilities - Utilities for reading and writing of cache files.
16    
17     =head1 SYNOPSIS
18    
19     Reading:
20    
21     print "Reading cached data","\n",if ($ENV{SCRAM_DEBUG});
22     $cacheobject=&Cache::CacheUtilities::read($cachename);
23    
24     Writing:
25    
26     &Cache::CacheUtilities::write($cacheobject,$cachename);
27    
28     =head1 DESCRIPTION
29    
30     Functions for reading and writing of cache files. This uses Data::Dumper to
31     write out Perl data structures to files. For reading, the complete data structure
32     is read from the cache file and stored in a variable which is then evalled to
33     restore the original object.
34    
35     =head1 METHODS
36    
37     =over
38    
39     =cut
40    
41 sashby 1.2 package Cache::CacheUtilities;
42     require 5.004;
43    
44     use IO::File;
45     use English;
46     use Exporter;
47    
48     @ISA=qw(Exporter);
49    
50     #
51     # Common functions for interacting with caches:
52     #
53    
54 sashby 1.5 =item C<read($cachefilename)>
55    
56     Read the cache file $cachefilename and return a Perl object.
57    
58     =cut
59    
60 sashby 1.2 sub read()
61     {
62     my ($cachefilename) = @_;
63     my $cachefh = IO::File->new($cachefilename, O_RDONLY)
64     || die "Unable to read cached data file $cachefilename: ",$ERRNO,"\n";
65     my @cacheitems = <$cachefh>;
66     close $cachefh;
67    
68     # Copy the new cache object to self and return:
69     $cache = eval "@cacheitems";
70     die "Cache load error: ",$EVAL_ERROR,"\n", if ($EVAL_ERROR);
71     return $cache;
72     }
73    
74 sashby 1.6 =item C<write($cacheobject,$cachefilename)>
75 sashby 1.5
76     Dump the Perl object $cacheobject to a file $cachefilename.
77    
78     =cut
79    
80 sashby 1.2 sub write()
81     {
82     my ($cacheobject,$cachefilename) = @_;
83 sashby 1.7
84 sashby 1.2 use Data::Dumper;
85     use File::Copy;
86    
87 sashby 1.7 print "[ CacheUtilities::write() ] Writing cache ",$cachefilename,"\n", if ($ENV{SCRAM_DEBUG});
88 sashby 1.3
89 sashby 1.7 # Move the cache file to make a backup:
90 sashby 1.2 move($cachefilename,$cachefilename.".bak") if ( -r $cachefilename);
91     # Dump the cache to file:
92     my $cachefh = IO::File->new($cachefilename, O_WRONLY|O_CREAT)
93     or die "Couldn't write to $cachefilename: ",$ERRNO,"\n";
94    
95     # Name that should replace "VAR1" in the dumped
96     # representation of the cache object:
97     $Data::Dumper::Varname='cache';
98     $Data::Dumper::Purity = 1;
99     print $cachefh Dumper($cacheobject);
100     close $cachefh;
101    
102 sashby 1.3 # Change the permissions to -rw-r--r--:
103     my $filemode = 0644;
104     chmod $filemode, $cachefilename;
105    
106 sashby 1.2 return;
107     }
108    
109     1;
110 sashby 1.5
111    
112     =back
113    
114     =head1 AUTHOR/MAINTAINER
115    
116     Shaun Ashby
117    
118     =cut