Go back to requiring only Perl 5.6+ for users
[autoconf.git] / lib / Autom4te / C4che.pm
blob523f15bffef725ab11a8b02d8bc2685087cd8fb4
1 # autoconf -- create 'configure' using m4 macros
2 # Copyright (C) 2003, 2006, 2009-2017, 2020-2023 Free Software
3 # Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18 package Autom4te::C4che;
20 =head1 NAME
22 Autom4te::C4che - a single m4 run request
24 =head1 SYNOPSIS
26 use Autom4te::C4che;
28 =head1 DESCRIPTION
30 This Perl module handles the cache of M4 runs used by autom4te.
32 =cut
34 use 5.006;
35 use strict;
36 use warnings FATAL => 'all';
38 use Carp;
39 use Data::Dumper;
41 use Autom4te::Request;
43 =over 4
45 =item @request
47 List of requests.
49 Must be a package global so it can be accessed by code evaluated via
50 'eval', below.
52 =cut
54 our @request;
56 =item C<$req = Autom4te::C4che-E<gt>retrieve (%attr)>
58 Find a request with the same path and input.
60 =cut
62 sub retrieve($%)
64 my ($self, %attr) = @_;
66 foreach (@request)
68 # Same path.
69 next
70 if join ("\n", @{$_->path}) ne join ("\n", @{$attr{path}});
72 # Same inputs.
73 next
74 if join ("\n", @{$_->input}) ne join ("\n", @{$attr{input}});
76 # Found it.
77 return $_;
80 return undef;
83 =item C<$req = Autom4te::C4che-E<gt>register (%attr)>
85 Create and register a request for these path and input.
87 =cut
89 # $REQUEST-OBJ
90 # register ($SELF, %ATTR)
91 # -----------------------
92 # NEW should not be called directly.
93 # Private.
94 sub register ($%)
96 my ($self, %attr) = @_;
98 # path and input are the only ID for a request object.
99 my $obj = new Autom4te::Request ('path' => $attr{path},
100 'input' => $attr{input});
101 push @request, $obj;
103 # Assign an id for cache file.
104 $obj->id ("$#request");
106 return $obj;
110 =item C<$req = Autom4te::C4che-E<gt>request (%request)>
112 Get (retrieve or create) a request for the path C<$request{path}> and
113 the input C<$request{input}>.
115 =cut
117 # $REQUEST-OBJ
118 # request($SELF, %REQUEST)
119 # ------------------------
120 sub request ($%)
122 my ($self, %request) = @_;
124 my $req =
125 Autom4te::C4che->retrieve (%request)
126 || Autom4te::C4che->register (%request);
128 # If there are new traces to produce, then we are not valid.
129 foreach (@{$request{'macro'}})
131 if (! exists ${$req->macro}{$_})
133 ${$req->macro}{$_} = 1;
134 $req->valid (0);
138 # It would be great to have $REQ check that it is up to date wrt
139 # its dependencies, but that requires getting traces (to fetch the
140 # included files), which is out of the scope of Request (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)->Sortkeys(1);
162 $res = $marshall->Dump . "\n";
164 return $res;
168 =item C<Autom4te::C4che-E<gt>save ($file, $version)>
170 Save the cache in the C<$file> file object.
172 =cut
174 # SAVE ($FILE, $VERSION)
175 # ----------------------
176 sub save ($$)
178 my ($self, $file, $version) = @_;
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 by Autom4te $version.\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>good_version ($file, $version)>
196 Succeed if the cache from the C<$file> file object is of the given version.
198 =cut
200 # GOOD_VERSION ($FILE, $VERSION)
201 # ------------------------------
202 sub good_version ($$)
204 my ($self, $file, $version) = @_;
205 my ($line) = $file->getline;
206 return defined ($line) && $line eq "# This file was generated by Autom4te $version.\n";
209 =item C<Autom4te::C4che-E<gt>load ($file)>
211 Load the cache from the C<$file> file object.
213 =cut
215 # LOAD ($FILE)
216 # ------------
217 sub load ($$)
219 my ($self, $file) = @_;
220 my $fname = $file->name;
222 confess "cannot load a single request\n"
223 if ref ($self);
225 my $contents = join "", $file->getlines;
227 eval $contents;
229 confess "cannot eval $fname: $@\n" if $@;
233 =head1 SEE ALSO
235 L<Autom4te::Request>
237 =head1 HISTORY
239 Written by Akim Demaille E<lt>F<akim@freefriends.org>E<gt>.
241 =cut
243 1; # for require
245 ### Setup "GNU" style for perl-mode and cperl-mode.
246 ## Local Variables:
247 ## perl-indent-level: 2
248 ## perl-continued-statement-offset: 2
249 ## perl-continued-brace-offset: 0
250 ## perl-brace-offset: 0
251 ## perl-brace-imaginary-offset: 0
252 ## perl-label-offset: -2
253 ## cperl-indent-level: 2
254 ## cperl-brace-offset: 0
255 ## cperl-continued-brace-offset: 0
256 ## cperl-label-offset: -2
257 ## cperl-extra-newline-before-brace: t
258 ## cperl-merge-trailing-else: nil
259 ## cperl-continued-statement-offset: 2
260 ## End: