* lib/Autom4te/Request.pm, lib/Autom4te/C4che.pm: New.
[autoconf.git] / lib / Autom4te / C4che.pm
blobef6f0c303329206d06d52a183d51e6d18206a39d
1 # autoconf -- create `configure' using m4 macros
2 # Copyright (C) 2003 Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
7 # any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 # 02111-1307, USA.
19 package Autom4te::C4che;
21 =head1 NAME
23 Autom4te::C4che - a single m4 run request
25 =head1 SYNOPSIS
27 use Autom4te::C4che;
29 =head1 DESCRIPTION
31 This Perl module handles the cache of M4 runs used by autom4te.
33 =cut
35 use Data::Dumper;
36 use Autom4te::Request;
37 use Carp;
38 use strict;
40 =over 4
42 =item @request
44 List of requests.
46 We cannot declare it "my" as the loading, performed via "do", would
47 refer to another scope, and @request would not be updated. It used to
48 work with "my" vars, and I do not know whether the current behavior
49 (5.6) is wanted or not.
51 =cut
53 use vars qw(@request);
55 =item C<$req = Autom4te::C4che-E<gt>retrieve (%attr)>
57 Find a request with the same path and input.
59 =cut
61 sub retrieve($%)
63 my ($self, %attr) = @_;
65 foreach (@request)
67 # Same path.
68 next
69 if join ("\n", @{$_->path}) ne join ("\n", @{$attr{path}});
71 # Same inputs.
72 next
73 if join ("\n", @{$_->input}) ne join ("\n", @{$attr{input}});
75 # Found it.
76 return $_;
79 return undef;
82 =item C<$req = Autom4te::C4che-E<gt>register (%attr)>
84 Create and register a request for these path and input.
86 =cut
88 # $REQUEST-OBJ
89 # register ($SELF, %ATTR)
90 # -----------------------
91 # NEW should not be called directly.
92 # Private.
93 sub register ($%)
95 my ($self, %attr) = @_;
97 # path and input are the only ID for a request object.
98 my $obj = new Autom4te::Request ('path' => $attr{path},
99 'input' => $attr{input});
100 push @request, $obj;
102 # Assign an id for cache file.
103 $obj->id ("$#request");
105 return $obj;
109 =item C<$req = Autom4te::C4che-E<gt>request (%request)>
111 Get (retrieve or create) a request for the path C<$request{path}> and
112 the input C<$request{input}>.
114 =cut
116 # $REQUEST-OBJ
117 # request($SELF, %REQUEST)
118 # ------------------------
119 sub request ($%)
121 my ($self, %request) = @_;
123 my $req =
124 Autom4te::C4che->retrieve (%request)
125 || Autom4te::C4che->register (%request);
127 # If there are new traces to produce, then we are not valid.
128 foreach (@{$request{'macro'}})
130 if (! exists ${$req->macro}{$_})
132 ${$req->macro}{$_} = 1;
133 $req->valid (0);
137 # It would be great to have $REQ check that it up to date wrt its
138 # dependencies, but that requires getting traces (to fetch the
139 # included files), which is out of the scope of Request
140 # (currently?).
142 return $req;
146 =item C<$string = Autom4te::C4che-E<gt>marshall ()>
148 Serialize all the current requests.
150 =cut
153 # marshall($SELF)
154 # ---------------
155 sub marshall ($)
157 my ($caller) = @_;
158 my $res = '';
160 my $marshall = Data::Dumper->new ([\@request], [qw (*request)]);
161 $marshall->Indent(2)->Terse(0);
162 $res = $marshall->Dump . "\n";
164 return $res;
168 =item C<Autom4te::C4che-E<gt>save ($file)>
170 Save the cache in the C<$file> file object.
172 =cut
174 # SAVE ($FILE)
175 # ------------
176 sub save ($$)
178 my ($self, $file) = @_;
180 confess "cannot save a single request\n"
181 if ref ($self);
183 $file->seek (0, 0);
184 $file->truncate (0);
185 print $file
186 "# This file was generated.\n",
187 "# It contains the lists of macros which have been traced.\n",
188 "# It can be safely removed.\n",
189 "\n",
190 $self->marshall;
194 =item C<Autom4te::C4che-E<gt>load ($file)>
196 Load the cache from the C<$file> file object.
198 =cut
200 # LOAD ($FILE)
201 # ------------
202 sub load ($$)
204 my ($self, $file) = @_;
205 my $fname = $file->name;
207 confess "cannot load a single request\n"
208 if ref ($self);
210 my $contents = join "", $file->getlines;
212 eval $contents;
214 confess "cannot eval $fname: $@\n" if $@;
218 =head1 SEE ALSO
220 L<Autom4te::Request>
222 =head1 HISTORY
224 Written by Akim Demaille E<lt>F<akim@freefriends.org>E<gt>.
226 =cut
228 1; # for require
230 ### Setup "GNU" style for perl-mode and cperl-mode.
231 ## Local Variables:
232 ## perl-indent-level: 2
233 ## perl-continued-statement-offset: 2
234 ## perl-continued-brace-offset: 0
235 ## perl-brace-offset: 0
236 ## perl-brace-imaginary-offset: 0
237 ## perl-label-offset: -2
238 ## cperl-indent-level: 2
239 ## cperl-brace-offset: 0
240 ## cperl-continued-brace-offset: 0
241 ## cperl-label-offset: -2
242 ## cperl-extra-newline-before-brace: t
243 ## cperl-merge-trailing-else: nil
244 ## cperl-continued-statement-offset: 2
245 ## End: