5 $version = '$Id: Refs.pm,v 1.5 1995/12/03 21:28:36 david Exp $';
15 To create a new reference-manager object using the file "foo.refs":
17 my $refs = new SGMLS::Refs("foo.refs");
19 To create a new reference-manager object using the file "foo.refs" and
20 logging changes to the file "foo.log":
22 my $refs = new SGMLS::Refs("foo.refs","foo.log");
24 To record a reference:
26 $refs->put("document title",$title);
28 To retrieve a reference:
30 $title = $refs->get("document title");
32 To return the number of references changed since the last run:
34 $num = $refs->changed;
36 To print a LaTeX-like warning if any references have changed:
42 This library can be used together with the B<SGMLS> package to keep
43 track of forward references from one run to another, like the B<LaTeX>
44 C<.aux> files. Each reference manager is an object which reads and
45 then rewrites a file of perl source, with the file name provided by
50 # Start up the reference manager before the parse.
51 sgml('start', sub { $refs = new SGMLS::Refs("foo.refs"); });
53 # Warn about any changed references at the end.
54 sgml('end', sub { $refs->warn; });
56 # Look up the title from the last parse, if available.
59 my $id = $element->attribute(ID)->value;
60 my $title = $refs->get("title:$id") || "[no title available]";
62 $current_div_id = $id;
64 output "\\section{$title}\n\n";
68 # Save the title for the next parse.
69 sgml('<head>', sub { push_output('string'); });
71 my $title = pop_output();
72 my $id = $current_div_id;
74 $refs->put("title:$id",$title);
78 =head1 AUTHOR AND COPYRIGHT
80 Copyright 1994 and 1995 by David Megginson,
81 C<dmeggins@aix1.uottawa.ca>. Distributed under the terms of the Gnu
82 General Public License (version 2, 1991) -- see the file C<COPYING>
83 which is included in the B<SGMLS.pm> distribution.
88 L<SGMLS>, L<SGMLS::Output>.
93 # Create a new instance of a reference manager. The first argument is
94 # the filename for the database, and the second (if present) is a
95 # filename for logging changes.
98 my ($class,$filename,$logname) = (@_);
100 my $handle = generate_handle();
101 my $loghandle = generate_handle() if $logname;
102 my $oldRS = $/; # Save old record separator.
104 # Read the current contents of the reference file (if any).
105 if (open($handle,"<$filename")) {
107 $self->{'refs'} = eval <$handle> || {};
110 $self->{'refs'} = {};
113 # Open the reference file.
114 open($handle,">$filename") || croak $@;
116 # Open the log file, if any.
118 open($loghandle,">$logname") || croak $@;
121 # Note pertinent information.
122 $self->{'change_count'} = 0;
123 $self->{'handle'} = $handle;
124 $self->{'loghandle'} = $loghandle;
125 $self->{'filename'} = $filename;
126 $self->{'logname'} = $logname;
128 $/ = $oldRS; # Restore old record separator.
133 # Set a reference's value. If the value is unchanged, don't do anything;
134 # otherwise, note the change by counting it and (optionally) logging it
135 # to the file handle provided when the object was created.
138 my ($self,$key,$value) = (@_);
139 my $loghandle = $self->{'loghandle'};
140 my $oldvalue = $self->{'refs'}->{$key};
142 if ($oldvalue ne $value) {
143 $self->{'change_count'}++;
145 print $loghandle "\"$key\" changed from " .
147 "\"$oldvalue\" to \"$value\".\n";
149 $self->{'refs'}->{$key} = $value;
156 # Grab the value of a reference.
159 my ($self,$key) = (@_);
161 return $self->{'refs'}->{$key};
165 # Return the number of changed references.
169 return $self->{'changed_count'};
173 # Print a warning if any references have
174 # changed (a la LaTeX -- so that the user knows that another pass is
175 # necessary). Return 1 if a warning has been printed, or 0 if it
180 my $count = $self->{'change_count'};
181 my $filename = $self->{'filename'};
182 my $plural = "references have";
184 $plural = "reference has" if $count == 1;
186 warn "SGMLS::Refs ($filename): $count $plural changed.\n";
194 my $handle = $self->{'handle'};
196 close $self->{'loghandle'};
199 foreach $key (keys %{$self->{'refs'}}) {
200 my $value = $self->{'refs'}->{$key};
203 $value =~ s/\\/\\\\/g;
205 print $handle " '$key' => '$value',\n";
207 print $handle " '' => ''\n}\n";
211 sub generate_handle {
212 return "Handle" . $handle_counter++;