blib/lib/Text/BibTeX/Structure.pm

filter: Statementstmt 46.0% 82 / 178 Branchbran 21.6% 16 / 74 Conditioncond 18.1% 6 / 33 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 41.9% 13 / 31 Podpod 100% 16 / 16 totaltotal 37.7% 133 / 352 CC 53 SCAR 45.9
CC
53
Cov
36%
check_field_constraints
380.0
check_required_fields
72.0
full_check
20.0
CRAP 98.0
↑ Summary blib/lib/Text/BibTeX/Value.pm »
1 # ----------------------------------------------------------------------
2 # NAME : BibTeX/Structure.pm
3 # CLASSES : Text::BibTeX::Structure, Text::BibTeX::StructuredEntry
4 # RELATIONS :
5 # DESCRIPTION: Provides the two base classes needed to implement
6 # Text::BibTeX structure modules.
7 # CREATED : in original form: Apr 1997
8 # completely redone: Oct 1997
9 # MODIFIED :
10 # VERSION : $Id$
11 # COPYRIGHT : Copyright (c) 1997-2000 by Gregory P. Ward. All rights
12 # reserved.
13 #
14 # This file is part of the Text::BibTeX library. This
15 # library is free software; you may redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 # ----------------------------------------------------------------------
18  
19 package Text::BibTeX::Structure;
20  
21 1 require 5.004; # for 'isa' and 'can'
22  
23 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 our $VERSION = '1.00';
27  
28 1 ▶use Text::BibTeX ('check_class');
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 =head1 NAME
31  
32 Text::BibTeX::Structure - provides base classes for user structure modules
33  
34 =head1 SYNOPSIS
35  
36 # Define a 'Foo' structure for BibTeX databases: first, the
37 # structure class:
38  
39 package Text::BibTeX::FooStructure;
40 @ISA = ('Text::BibTeX::Structure');
41  
42 sub known_option
43 {
44 my ($self, $option) = @_;
45  
46 ...
47 }
48  
49 sub default_option
50 {
51 my ($self, $option) = @_;
52  
53 ...
54 }
55  
56 sub describe_entry
57 {
58 my $self = shift;
59  
60 $self->set_fields ($type,
61 \@required_fields,
62 \@optional_fields,
63 [$constraint_1, $constraint_2, ...]);
64 ...
65 }
66  
67  
68 # Now, the structured entry class
69  
70 package Text::BibTeX::FooEntry;
71 @ISA = ('Text::BibTeX::StructuredEntry');
72  
73 # define whatever methods you like
74  
75 =head1 DESCRIPTION
76  
77 The module C<Text::BibTeX::Structure> provides two classes that form the
78 basis of the B<btOOL> "structure module" system. This system is how
79 database structures are defined and imposed on BibTeX files, and
80 provides an elegant synthesis of object-oriented techniques with
81 BibTeX-style database structures. Nothing described here is
82 particularly deep or subtle; anyone familiar with object-oriented
83 programming should be able to follow it. However, a fair bit of jargon
84 in invented and tossed around, so pay attention.
85  
86 A I<database structure>, in B<btOOL> parlance, is just a set of allowed
87 entry types and the rules for fields in each of those entry types.
88 Currently, there are three kinds of rules that apply to fields: some
89 fields are I<required>, meaning they must be present in every entry for
90 a given type; some are I<optional>, meaning they may be present, and
91 will be used if they are; other fields are members of I<constraint
92 sets>, which are explained in L<"Field lists and constraint sets">
93 below.
94  
95 A B<btOOL> structure is implemented with two classes: the I<structure
96 class> and the I<structured entry class>. The former defines everything
97 that applies to the structure as a whole (allowed types and field
98 rules). The latter provides methods that operate on individual entries
99 which conform (or are supposed to conform) to the structure. The two
100 classes provided by the C<Text::BibTeX::Structure> module are
101 C<Text::BibTeX::Structure> and C<Text::BibTeX::StructuredEntry>; these
102 serve as base classes for, respectively, all structure classes and all
103 structured entry classes. One canonical structure is provided as an
104 example with B<btOOL>: the C<Bib> structure, which (via the
105 C<BibStructure> and C<BibEntry> classes) provides the same functionality
106 as the standard style files of BibTeX 0.99. It is hoped that other
107 programmers will write new bibliography-related structures, possibly
108 deriving from the C<Bib> structure, to emulate some of the functionality
109 that is available through third-party BibTeX style files.
110  
111 The purpose of this manual page is to describe the whole "structure
112 module" system. It is mainly for programmers wishing to implement a new
113 database structure for data files with BibTeX syntax; if you are
114 interested in the particular rules for the BibTeX-emulating C<Bib>
115 structure, see L<Text::BibTeX::Bib>.
116  
117 Please note that the C<Text::BibTeX> prefix is dropped from most module
118 and class names in this manual page, except where necessary.
119  
120 =head1 STRUCTURE CLASSES
121  
122 Structure classes have two roles: to define the list of allowed types
123 and field rules, and to handle I<structure options>.
124  
125 =head2 Field lists and constraint sets
126  
127 Field lists and constraint sets define the database structure for a
128 particular entry type: that is, they specify the rules which an entry
129 must follow to conform to the structure (assuming that entry is of an
130 allowed type). There are three components to the field rules for each
131 entry type: a list of required fields, a list of optional fields, and
132 I<field constraints>. Required and optional fields should be obvious to
133 anyone with BibTeX experience: all required fields must be present, and
134 any optional fields that are present have some meaning to the structure.
135 (One could conceive of a "strict" interpretation, where any field not
136 mentioned in the official definition is disallowed; this would be
137 contrary to the open spirit of BibTeX databases, but could be useful in
138 certain applications where a stricter level of control is desired.
139 Currently, B<btOOL> does not offer such an option.)
140  
141 Field constraints capture the "one or the other, but not both" type of
142 relationships present for some entry types in the BibTeX standard style
143 files. Most BibTeX documentation glosses over the distinction between
144 mutually constrained fields and required/optional fields. For instance,
145 one of the standard entry types is C<book>, and "C<author> or C<editor>"
146 is given in the list of required fields for that type. The meaning of
147 this is that an entry of type C<book> must have I<either> the C<author>
148 or C<editor> fields, but not both. Likewise, the "C<volume> or
149 C<number>" are listed under the "optional fields" heading for C<book>
150 entries; it would be more accurate to say that every C<book> entry may
151 have one or the other, or neither, of C<volume> or C<number>---but not
152 both.
153  
154 B<btOOL> attempts to clarify this situation by creating a third category
155 of fields, those that are mutually constrained. For instance, neither
156 C<author> nor C<editor> appears in the list of required fields for
157 the C<inbook> type according to B<btOOL>; rather, a field constraint is
158 created to express this relationship:
159  
160 [1, 1, ['author', 'editor']]
161  
162 That is, a field constraint is a reference to a three-element list. The
163 last element is a reference to the I<constraint set>, the list of fields
164 to which the constraint applies. (Calling this a set is a bit
165 inaccurate, as there are conditions in which the order of fields
166 matters---see the C<check_field_constraints> method in L<"METHODS 2:
167 BASE STRUCTURED ENTRY CLASS">.) The first two elements are the minimum
168 and maximum number of fields from the constraint set that must be
169 present for an entry to conform to the constraint. This constraint thus
170 expresses that there must be exactly one (>= 1 and <= 1) of the fields
171 C<author> and C<editor> in a C<book> entry.
172  
173 The "either one or neither, but not both" constraint that applies to the
174 C<volume> and C<number> fields for C<book> entries is expressed slightly
175 differently:
176  
177 [0, 1, ['volume', 'number']]
178  
179 That is, either 0 or 1, but not the full 2, of C<volume> and C<number>
180 may be present.
181  
182 It is important to note that checking and enforcing field constraints is
183 based purely on counting which fields from a set are actually present;
184 this mechanism can't capture "x must be present if y is" relationships.
185  
186 The requirements imposed on the actual structure class are simple: it
187 must provide a method C<describe_entry> which sets up a fancy data
188 structure describing the allowed entry types and all the field rules for
189 those types. The C<Structure> class provides methods (inherited by a
190 particular structure class) to help particular structure classes create
191 this data structure in a consistent, controlled way. For instance, the
192 C<describe_structure> method in the BibTeX 0.99-emulating
193 C<BibStructure> class is quite simple:
194  
195 sub describe_entry
196 {
197 my $self = shift;
198  
199 # series of 13 calls to $self->set_fields (one for each standard
200 # entry type)
201 }
202  
203 One of those calls to the C<set_fields> method defines the rules for
204 C<book> entries:
205  
206 $self->set_fields ('book',
207 [qw(title publisher year)],
208 [qw(series address edition month note)],
209 [1, 1, [qw(author editor)]],
210 [0, 1, [qw(volume number)]]);
211  
212 The first field list is the list of required fields, and the second is
213 the list of optional fields. Any number of field constraints may follow
214 the list of optional fields; in this case, there are two, one for each
215 of the constraints (C<author>/C<editor> and C<volume>/C<number>)
216 described above. At no point is a list of allowed types explicitly
217 supplied; rather, each call to C<set_fields> adds one more allowed type.
218  
219 New structure modules that derive from existing ones will probably use the
220 C<add_fields> method (and possibly C<add_constraints>) to augment an
221 existing entry type. Adding new types should be done with C<set_fields>,
222 though.
223  
224 =head2 Structure options
225  
226 The other responsibility of structure classes is to handle I<structure
227 options>. These are scalar values that let the user customize the
228 behaviour of both the structure class and the structured entry class.
229 For instance, one could have an option to enable "extended structure",
230 which might add on a bunch of new entry types and new fields. (In this
231 case, the C<describe_entry> method would have to pay attention to this
232 option and modify its behaviour accordingly.) Or, one could have
233 options to control how the structured entry class sorts or formats
234 entries (for bibliography structures such as C<Bib>).
235  
236 The easy way to handle structure options is to provide two methods,
237 C<known_option> and C<default_option>. These return, respectively,
238 whether a given option is supported, and what its default value is. (If
239 your structure doesn't support any options, you can just inherit these
240 methods from the C<Structure> class. The default C<known_option>
241 returns false for all options, and its companion C<default_option>
242 crashes with an "unknown option" error.)
243  
244 Once C<known_option> and C<default_option> are provided, the structure
245 class can sit back and inherit the more visible C<set_options> and
246 C<get_options> methods from the C<Structure> class. These are the
247 methods actually used to modify/query options, and will be used by
248 application programs to customize the structure module's behaviour, and
249 by the structure module itself to pay attention to the user's wishes.
250  
251 Options should generally have pure string values, so that the generic
252 set_options method doesn't have to parse user-supplied strings into some
253 complicated structure. However, C<set_options> will take any scalar
254 value, so if the structure module clearly documents its requirements,
255 the application program could supply a structure that meets its needs.
256 Keep in mind that this requires cooperation between the application and
257 the structure module; the intermediary code in
258 C<Text::BibTeX::Structure> knows nothing about the format or syntax of
259 your structure's options, and whatever scalar the application passes via
260 C<set_options> will be stored for your module to retrieve via
261 C<get_options>.
262  
263 As an example, the C<Bib> structure supports a number of "markup"
264 options that allow applications to control the markup language used for
265 formatting bibliographic entries. These options are naturally paired,
266 as formatting commands in markup languages generally have to be turned
267 on and off. The C<Bib> structure thus expects references to two-element
268 lists for markup options; to specify LaTeX 2e-style emphasis for book
269 titles, an application such as C<btformat> would set the C<btitle_mkup>
270 option as follows:
271  
272 $structure->set_options (btitle_mkup => ['\emph{', '}']);
273  
274 Other options for other structures might have a more complicated
275 structure, but it's up to the structure class to document and enforce
276 this.
277  
278 =head1 STRUCTURED ENTRY CLASSES
279  
280 A I<structured entry class> defines the behaviour of individual entries
281 under the regime of a particular database structure. This is the
282 I<raison d'E<ecirc>tre> for any database structure: the structure class
283 merely lays out the rules for entries to conform to the structure, but
284 the structured entry class provides the methods that actually operate on
285 individual entries. Because this is completely open-ended, the
286 requirements of a structured entry class are much less rigid than for a
287 structure class. In fact, all of the requirements of a structured entry
288 class can be met simply by inheriting from
289 C<Text::BibTeX::StructuredEntry>, the other class provided by the
290 C<Text::BibTeX::Structure> module. (For the record, those requirements
291 are: a structured entry class must provide the entry
292 parse/query/manipulate methods of the C<Entry> class, and it must
293 provide the C<check>, C<coerce>, and C<silently_coerce> methods of the
294 C<StructuredEntry> class. Since C<StructuredEntry> inherits from
295 C<Entry>, both of these requirements are met "for free" by structured
296 entry classes that inherit from C<Text::BibTeX::StructuredEntry>, so
297 naturally this is the recommended course of action!)
298  
299 There are deliberately no other methods required of structured entry
300 classes. A particular application (eg. C<btformat> for bibliography
301 structures) will require certain methods, but it's up to the application
302 and the structure module to work out the requirements through
303 documentation.
304  
305 =head1 CLASS INTERACTIONS
306  
307 Imposing a database structure on your entries sets off a chain reaction
308 of interactions between various classes in the C<Text::BibTeX> library
309 that should be transparent when all goes well. It could prove confusing
310 if things go wrong and you have to go wading through several levels of
311 application program, core C<Text::BibTeX> classes, and some structure
312 module.
313  
314 The justification for this complicated behaviour is that it allows you
315 to write programs that will use a particular structured module without
316 knowing the name of the structure when you write the program. Thus, the
317 user can supply a database structure, and ultimately the entry objects
318 you manipulate will be blessed into a class supplied by the structure
319 module. A short example will illustrate this.
320  
321 Typically, a C<Text::BibTeX>-based program is based around a kernel of
322 code like this:
323  
324 $bibfile = Text::BibTeX::File->new("foo.bib");
325 while ($entry = Text::BibTeX::Entry->new($bibfile))
326 {
327 # process $entry
328 }
329  
330 In this case, nothing fancy is happening behind the scenes: the
331 C<$bibfile> object is blessed into the C<Text::BibTeX::File> class, and
332 C<$entry> is blessed into C<Text::BibTeX::Entry>. This is the
333 conventional behaviour of Perl classes, but it is not the only possible
334 behaviour. Let us now suppose that C<$bibfile> is expected to conform
335 to a database structure specified by C<$structure> (presumably a
336 user-supplied value, and thus unknown at compile-time):
337  
338 $bibfile = Text::BibTeX::File->new("foo.bib");
339 $bibfile->set_structure ($structure);
340 while ($entry = Text::BibTeX::Entry->new($bibfile))
341 {
342 # process $entry
343 }
344  
345 A lot happens behind the scenes with the call to C<$bibfile>'s
346 C<set_structure> method. First, a new structure object is created from
347 C<$structure>. The structure name implies the name of a Perl
348 module---the structure module---which is C<require>'d by the
349 C<Structure> constructor. (The main consequence of this is that any
350 compile-time errors in your structure module will not be revealed until
351 a C<Text::BibTeX::File::set_structure> or
352 C<Text::BibTeX::Structure::new> call attempts to load it.)
353  
354 Recall that the first responsibility of a structure module is to define
355 a structure class. The "structure object" created by the
356 C<set_structure> method call is actually an object of this class; this
357 is the first bit of trickery---the structure object (buried behind the
358 scenes) is blessed into a class whose name is not known until run-time.
359  
360 Now, the behaviour of the C<Text::BibTeX::Entry::new> constructor
361 changes subtly: rather than returning an object blessed into the
362 C<Text::BibTeX::Entry> class as you might expect from the code, the
363 object is blessed into the structured entry class associated with
364 C<$structure>.
365  
366 For example, if the value of C<$structure> is C<"Foo">, that means the
367 user has supplied a module implementing the C<Foo> structure.
368 (Ordinarily, this module would be called C<Text::BibTeX::Foo>---but you
369 can customize this.) Calling the C<set_structure> method on C<$bibfile>
370 will attempt to create a new structure object via the
371 C<Text::BibTeX::Structure> constructor, which loads the structure module
372 C<Text::BibTeX::Foo>. Once this module is successfully loaded, the new
373 object is blessed into its structure class, which will presumably be
374 called C<Text::BibTeX::FooStructure> (again, this is customizable). The
375 new object is supplied with the user's structure options via the
376 C<set_options> method (usually inherited), and then it is asked to
377 describe the actual entry layout by calling its C<describe_entry>
378 method. This, in turn, will usually call the inherited C<set_fields>
379 method for each entry type in the database structure. When the
380 C<Structure> constructor is finished, the new structure object is stored
381 in the C<File> object (remember, we started all this by calling
382 C<set_structure> on a C<File> object) for future reference.
383  
384 Then, when a new C<Entry> object is created and parsed from that
385 particular C<File> object, some more trickery happens. Trivially, the
386 structure object stored in the C<File> object is also stored in the
387 C<Entry> object. (The idea is that entries could belong to a database
388 structure independently of any file, but usually they will just get the
389 structure that was assigned to their database file.) More importantly,
390 the new C<Entry> object is re-blessed into the structured entry class
391 supplied by the structure module---presumably, in this case,
392 C<Text::BibTeX::FooEntry> (also customizable).
393  
394 Once all this sleight-of-hand is accomplished, the application may treat
395 its entry objects as objects of the structured entry class for the
396 C<Foo> structure---they may call the check/coerce methods inherited from
397 C<Text::BibTeX::StructuredEntry>, and they may also call any methods
398 specific to entries for this particular database structure. What these
399 methods might be is up to the structure implementor to decide and
400 document; thus, applications may be specific to one particular database
401 structure, or they may work on all structures that supply certain
402 methods. The choice is up to the application developer, and the range
403 of options open to him depends on which methods structure implementors
404 provide.
405  
406 =head1 EXAMPLE
407  
408 For example code, please refer to the source of the C<Bib> module and
409 the C<btcheck>, C<btsort>, and C<btformat> applications supplied with
410 C<Text::BibTeX>.
411  
412 =head1 METHODS 1: BASE STRUCTURE CLASS
413  
414 The first class provided by the C<Text::BibTeX::Structure> module is
415 C<Text::BibTeX::Structure>. This class is intended to provide methods
416 that will be inherited by user-supplied structure classes; such classes
417 should not override any of the methods described here (except
418 C<known_option> and C<default_option>) without very good reason.
419 Furthermore, overriding the C<new> method would be useless, because in
420 general applications won't know the name of your structure class---they
421 can only call C<Text::BibTeX::Structure::new> (usually via
422 C<Text::BibTeX::File::set_structure>).
423  
424 Finally, there are three methods that structure classes should
425 implement: C<known_option>, C<default_option>, and C<describe_entry>.
426 The first two are described in L<"Structure options"> above, the latter
427 in L<"Field lists and constraint sets">. Note that C<describe_entry>
428 depends heavily on the C<set_fields>, C<add_fields>, and
429 C<add_constraints> methods described here.
430  
431 =head2 Constructor/simple query methods
432  
433 =over 4
434  
435 =item new (STRUCTURE, [OPTION =E<gt> VALUE, ...])
436  
437 Constructs a new structure object---I<not> a C<Text::BibTeX::Structure>
438 object, but rather an object blessed into the structure class associated
439 with STRUCTURE. More precisely:
440  
441 =over 4
442  
443 =item *
444  
445 Loads (with C<require>) the module implementing STRUCTURE. In the
446 absence of other information, the module name is derived by appending
447 STRUCTURE to C<"Text::BibTeX::">---thus, the module C<Text::BibTeX::Bib>
448 implements the C<Bib> structure. Use the pseudo-option C<module> to
449 override this module name. For instance, if the structure C<Foo> is
450 implemented by the module C<Foo>:
451  
452 $structure = Text::BibTeX::Structure->new
453 ('Foo', module => 'Foo');
454  
455 This method C<die>s if there are any errors loading/compiling the
456 structure module.
457  
458 =item *
459  
460 Verifies that the structure module provides a structure class and a
461 structured entry class. The structure class is named by appending
462 C<"Structure"> to the name of the module, and the structured entry class
463 by appending C<"Entry">. Thus, in the absence of a C<module> option,
464 these two classes (for the C<Bib> structure) would be named
465 C<Text::BibTeX::BibStructure> and C<Text::BibTeX::BibEntry>. Either or
466 both of the default class names may be overridden by having the
467 structure module return a reference to a hash (as opposed to the
468 traditional C<1> returned by modules). This hash could then supply a
469 C<structure_class> element to name the structure class, and an
470 C<entry_class> element to name the structured entry class.
471  
472 Apart from ensuring that the two classes actually exist, C<new> verifies
473 that they inherit correctly (from C<Text::BibTeX::Structure> and
474 C<Text::BibTeX::StructuredEntry> respectively), and that the structure
475 class provides the required C<known_option>, C<default_option>, and
476 C<describe_entry> methods.
477  
478 =item *
479  
480 Creates the new structure object, and blesses it into the structure
481 class. Supplies it with options by passing all (OPTION, VALUE) pairs to
482 its C<set_options> method. Calls its C<describe_entry> method, which
483 should list the field requirements for all entry types recognized by
484 this structure. C<describe_entry> will most likely use some or all of
485 the C<set_fields>, C<add_fields>, and C<add_constraints>
486 methods---described below---for this.
487  
488 =back
489  
490 =cut
491  
492 sub new
493 1 ▶{
Subroutine new: called CC=6 SCAR=18.8
Pod documented
494 1 my ($type, $name, %options) = @_;
495  
496 # - $type is presumably "Text::BibTeX::Structure" (if called from
497 # Text::BibTeX::File::set_structure), but shouldn't assume that
498 # - $name is the name of the user-supplied structure; it also
499 # determines the module we will attempt to load here, unless
500 # a 'module' option is given in %options
501 # - %options is a mix of options recognized here (in particular
502 # 'module'), by Text::BibTeX::StructuredEntry (? 'check', 'coerce',
503 # 'warn' flags), and by the user structure classes
504  
505 1 ▶ my $module = (delete $options{'module'}) || ('Text::BibTeX::' . $name);
Condition50%1/2
delete $options{'module'} || 'Text::BibTeX::' . $name
l!l
01
MC/DC0%0/1
delete $options{'module'} || 'Text::BibTeX::' . $name
delete $options{'module'}
Truth table50%1/2
A
A= delete $options{'module'}
Aresult
11
00
506  
507 1 my $module_info = eval "require $module";
508 1 ▶ die "Text::BibTeX::Structure: unable to load module \"$module\" for " .
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
509 "user structure \"$name\": $@\n"
510 if $@;
511  
512 1 my ($structure_class, $entry_class);
513 1 ▶ if (ref $module_info eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $module_info eq "HASH") 0 1 1
514 {
515 0 $structure_class = $module_info->{'structure_class'};
516 0 $entry_class = $module_info->{'entry_class'};
517 }
518 1 ▶ $structure_class ||= $module . 'Structure';
Condition50%1/2
$structure_class ||= $module . 'Structure'
l!l
01
MC/DC0%0/1
$structure_class ||= $module . 'Structure'
$structure_class
Truth table50%1/2
A
A= $structure_class
Aresult
11
00
519 1 ▶ $entry_class ||= $module . 'Entry';
Condition50%1/2
$entry_class ||= $module . 'Entry'
l!l
01
MC/DC0%0/1
$entry_class ||= $module . 'Entry'
$entry_class
Truth table50%1/2
A
A= $entry_class
Aresult
11
00
520  
521 1 check_class ($structure_class, "user structure class",
522 'Text::BibTeX::Structure',
523 ['known_option', 'default_option', 'describe_entry']);
524 1 check_class ($entry_class, "user entry class",
525 'Text::BibTeX::StructuredEntry',
526 []);
527  
528 1 my $self = bless {}, $structure_class;
529 1 $self->{entry_class} = $entry_class;
530 1 $self->{name} = $name;
531 1 $self->set_options (%options); # these methods are both provided by
532 1 $self->describe_entry; # the user structure class
533 1 $self;
534 }
535  
536  
537 =item name ()
538  
539 Returns the name of the structure described by the object.
540  
541 =item entry_class ()
542  
543 Returns the name of the structured entry class associated with this
544 structure.
545  
546 =back
547  
548 =cut
549  
550 0 ▶sub name { shift->{'name'} }
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
551  
552 2 ▶sub entry_class { shift->{'entry_class'} }
Subroutine entry_class: called CC=1 SCAR=0.0
Pod documented
553  
554  
555 =head2 Field structure description methods
556  
557 =over 4
558  
559 =item add_constraints (TYPE, CONSTRAINT, ...)
560  
561 Adds one or more field constraints to the structure. A field constraint
562 is specified as a reference to a three-element list; the last element is
563 a reference to the list of fields affected, and the first two elements
564 are the minimum and maximum number of fields from the constraint set
565 allowed in an entry of type TYPE. See L<"Field lists and constraint
566 sets"> for a full explanation of field constraints.
567  
568 =cut
569  
570 sub add_constraints
571 14 ▶{
Subroutine add_constraints: called CC=7 SCAR=22.0
Pod documented
572 14 my ($self, $type, @constraints) = @_;
573 14 my ($constraint);
574  
575 14 foreach $constraint (@constraints)
576 {
577 9 my ($min, $max, $fields) = @$constraint;
578 9 ▶ croak "add_constraints: constraint record must be a 3-element " .
Branch: 1 branch
BranchTrueFalseTotal
unless @$constraint == 3 and ref $fields eq "ARRAY" 0 9 9
Condition33%1/3
@$constraint == 3 and ref $fields eq "ARRAY"
!ll&&!rl&&r
009
MC/DC0%0/2
@$constraint == 3 and ref $fields eq "ARRAY"
@$constraint == 3 ref $fields eq "ARRAY"
Truth table33%1/3
A and B
A= @$constraint == 3B= ref $fields eq "ARRAY"
ABresult
0X0
100
111
579 "list, with the last element a list ref"
580 unless (@$constraint == 3 && ref $fields eq 'ARRAY');
581 9 ▶ croak "add_constraints: constraint record must have 0 <= 'min' " .
Branch: 1 branch
BranchTrueFalseTotal
unless $min >= 0 and $max >= $min and $max <= @$fields 0 9 9
Condition33%2/6
$min >= 0 and $max >= $min and $max <= @$fields
!ll&&!rl&&r
009
$min >= 0 and $max >= $min
!ll&&!rl&&r
009
MC/DC0%0/3
$min >= 0 and $max >= $min and $max <= @$fields
$min >= 0 $max >= $min $max <= @$fields
Truth table25%1/4
A and B and C
A= $min >= 0B= $max >= $minC= $max <= @$fields
ABCresult
0XX0
10X0
1100
1111
582 "<= 'max' <= length of field list"
583 unless ($min >= 0 && $max >= $min && $max <= @$fields);
584 9 map { $self->{fields}{$type}{$_} = $constraint } @$fields;
585 }
586 14 push (@{$self->{fieldgroups}{$type}{'constraints'}}, @constraints);
587  
588 } # add_constraints
589  
590  
591 =item add_fields (TYPE, REQUIRED [, OPTIONAL [, CONSTRAINT, ...]])
592  
593 Adds fields to the required/optional lists for entries of type TYPE.
594 Can also add field constraints, but you can just as easily use
595 C<add_constraints> for that.
596  
597 REQUIRED and OPTIONAL, if defined, should be references to lists of
598 fields to add to the respective field lists. The CONSTRAINTs, if given,
599 are exactly as described for C<add_constraints> above.
600  
601 =cut
602  
603 sub add_fields # add fields for a particular type
604 0 ▶{
Subroutine add_fields: not called CC=3 SCAR=24.8
Pod documented
605 0 my ($self, $type, $required, $optional, @constraints) = @_;
606  
607 # to be really robust and inheritance-friendly, we should:
608 # - check that no field is in > 1 list (just check $self->{fields}
609 # before we start assigning stuff)
610 # - allow sub-classes to delete fields or move them to another group
611  
612 0 ▶ if ($required)
Branch: 1 branch
BranchTrueFalseTotal
if ($required) 0 0 0
613 {
614 0 push (@{$self->{fieldgroups}{$type}{'required'}}, @$required);
615 0 map { $self->{fields}{$type}{$_} = 'required' } @$required;
616 }
617  
618 0 ▶ if ($optional)
Branch: 1 branch
BranchTrueFalseTotal
if ($optional) 0 0 0
619 {
620 0 push (@{$self->{fieldgroups}{$type}{'optional'}}, @$optional);
621 0 map { $self->{fields}{$type}{$_} = 'optional' } @$optional;
622 }
623  
624 0 $self->add_constraints ($type, @constraints);
625  
626 } # add_fields
627  
628  
629 =item set_fields (TYPE, REQUIRED [, OPTIONAL [, CONSTRAINTS, ...]])
630  
631 Sets the lists of required/optional fields for entries of type TYPE.
632 Identical to C<add_fields>, except that the field lists and list of
633 constraints are set from scratch here, rather than being added to.
634  
635 =back
636  
637 =cut
638  
639 sub set_fields
640 14 ▶{
Subroutine set_fields: called CC=3 SCAR=11.0
Pod documented
641 14 my ($self, $type, $required, $optional, @constraints) = @_;
642 14 my ($constraint, $field);
643  
644 14 undef %{$self->{fields}{$type}};
645  
646 14 ▶ if ($required)
Branch: 1 branch
BranchTrueFalseTotal
if ($required) 14 0 14
647 {
648 14 $self->{fieldgroups}{$type}{'required'} = $required;
649 14 map { $self->{fields}{$type}{$_} = 'required' } @$required;
650 }
651  
652 14 ▶ if ($optional)
Branch: 1 branch
BranchTrueFalseTotal
if ($optional) 14 0 14
653 {
654 14 $self->{fieldgroups}{$type}{'optional'} = $optional;
655 14 map { $self->{fields}{$type}{$_} = 'optional' } @$optional;
656 }
657  
658 14 undef @{$self->{fieldgroups}{$type}{'constraints'}};
659 14 $self->add_constraints ($type, @constraints);
660  
661 } # set_fields
662  
663  
664 =head2 Field structure query methods
665  
666 =over 4
667  
668 =item types ()
669  
670 Returns the list of entry types supported by the structure.
671  
672 =item known_type (TYPE)
673  
674 Returns true if TYPE is a supported entry type.
675  
676 =item known_field (TYPE, FIELD)
677  
678 Returns true if FIELD is in the required list, optional list, or one of
679 the constraint sets for entries of type TYPE.
680  
681 =item required_fields (TYPE)
682  
683 Returns the list of required fields for entries of type TYPE.
684  
685 =item optional_fields ()
686  
687 Returns the list of optional fields for entries of type TYPE.
688  
689 =item field_constraints ()
690  
691 Returns the list of field constraints (in the format supplied to
692 C<add_constraints>) for entries of type TYPE.
693  
694 =back
695  
696 =cut
697  
698 sub types
699 0 ▶{
Subroutine types: not called CC=1 SCAR=6.9
Pod documented
700 0 my $self = shift;
701  
702 0 keys %{$self->{'fieldgroups'}};
703 }
704  
705 sub known_type
706 0 ▶{
Subroutine known_type: not called CC=1 SCAR=6.9
Pod documented
707 0 my ($self, $type) = @_;
708  
709 0 exists $self->{'fieldgroups'}{$type};
710 }
711  
712 sub _check_type
713 0 ▶{
Subroutine _check_type: not called CC=2 SCAR=17.9
714 0 my ($self, $type) = @_;
715  
716 croak "unknown entry type \"$type\" for $self->{'name'} structure"
717 0 ▶ unless exists $self->{'fieldgroups'}{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"fieldgroups"}{$type} 0 0 0
718 }
719  
720 sub known_field
721 0 ▶{
Subroutine known_field: not called CC=1 SCAR=6.9
Pod documented
722 0 my ($self, $type, $field) = @_;
723  
724 0 $self->_check_type ($type);
725 0 $self->{'fields'}{$type}{$field}; # either 'required', 'optional', or
726 } # a constraint record (or undef!)
727  
728 sub required_fields
729 0 ▶{
Subroutine required_fields: not called CC=1 SCAR=6.9
Pod documented
730 0 my ($self, $type) = @_;
731  
732 0 $self->_check_type ($type);
733 0 @{$self->{'fieldgroups'}{$type}{'required'}};
734 }
735  
736 sub optional_fields
737 0 ▶{
Subroutine optional_fields: not called CC=1 SCAR=6.9
Pod documented
738 0 my ($self, $type) = @_;
739  
740 0 $self->_check_type ($type);
741 0 @{$self->{'fieldgroups'}{$type}{'optional'}};
742 }
743  
744 sub field_constraints
745 0 ▶{
Subroutine field_constraints: not called CC=1 SCAR=6.9
Pod documented
746 0 my ($self, $type) = @_;
747  
748 0 $self->_check_type ($type);
749 0 @{$self->{'fieldgroups'}{$type}{'constraints'}};
750 }
751  
752  
753 =head2 Option methods
754  
755 =over 4
756  
757 =item known_option (OPTION)
758  
759 Returns false. This is mainly for the use of derived structures that
760 don't have any options, and thus don't need to provide their own
761 C<known_option> method. Structures that actually offer options should
762 override this method; it should return true if OPTION is a supported
763 option.
764  
765 =cut
766  
767 sub known_option
768 0 ▶{
Subroutine known_option: not called CC=1 SCAR=6.9
Pod documented
769 0 return 0;
770 }
771  
772  
773 =item default_option (OPTION)
774  
775 Crashes with an "unknown option" message. Again, this is mainly for use
776 by derived structure classes that don't actually offer any options.
777 Structures that handle options should override this method; every option
778 handled by C<known_option> should have a default value (which might just
779 be C<undef>) that is returned by C<default_option>. Your
780 C<default_options> method should crash on an unknown option, perhaps by
781 calling C<SUPER::default_option> (in order to ensure consistent error
782 messages). For example:
783  
784 sub default_option
785 {
786 my ($self, $option) = @_;
787 return $default_options{$option}
788 if exists $default_options{$option};
789 $self->SUPER::default_option ($option); # crash
790 }
791  
792 The default value for an option is returned by C<get_options> when that
793 options has not been explicitly set with C<set_options>.
794  
795 =cut
796  
797 sub default_option
798 0 ▶{
Subroutine default_option: not called CC=1 SCAR=6.9
Pod documented
799 0 my ($self, $option) = @_;
800  
801 0 croak "unknown option \"$option\" for structure \"$self->{'name'}\"";
802 }
803  
804  
805 =item set_options (OPTION =E<gt> VALUE, ...)
806  
807 Sets one or more option values. (You can supply as many
808 C<OPTION =E<gt> VALUE> pairs as you like, just so long as there are an even
809 number of arguments.) Each OPTION must be handled by the structure
810 module (as indicated by the C<known_option> method); if not
811 C<set_options> will C<croak>. Each VALUE may be any scalar value; it's
812 up to the structure module to validate them.
813  
814 =cut
815  
816 sub set_options
817 9 ▶{
Subroutine set_options: called CC=4 SCAR=14.0
Pod documented
818 9 my $self = shift;
819 9 my ($option, $value);
820  
821 9 ▶ croak "must supply an even number of arguments (option/value pairs)"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 9 9
822 unless @_ % 2 == 0;
823 9 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 10 9 19
824 {
825 10 ($option, $value) = (shift, shift);
826 10 ▶ croak "unknown option \"$option\" for structure \"$self->{'name'}\""
Branch: 1 branch
BranchTrueFalseTotal
unless $self->known_option($option) 0 10 10
827 unless $self->known_option ($option);
828 10 $self->{'options'}{$option} = $value;
829 }
830 }
831  
832  
833 =item get_options (OPTION, ...)
834  
835 Returns the value(s) of one or more options. Any OPTION that has not
836 been set by C<set_options> will return its default value, fetched using
837 the C<default_value> method. If OPTION is not supported by the
838 structure module, then your program either already crashed (when it
839 tried to set it with C<set_option>), or it will crash here (thanks to
840 calling C<default_option>).
841  
842 =back
843  
844 =cut
845  
846 sub get_options
847 49 ▶{
Subroutine get_options: called CC=4 SCAR=13.9
Pod documented
848 49 my $self = shift;
849 49 my ($options, $option, $value, @values);
850  
851 49 $options = $self->{'options'};
852 49 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 58 49 107
853 {
854 58 $option = shift;
855 $value = (exists $options->{$option})
856 58 ▶ ? $options->{$option}
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{$option} ? : 22 36 58
857 : $self->default_option ($option);
858 58 push (@values, $value);
859 }
860  
861 49 ▶ wantarray ? @values : $values[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 9 40 49
862 }
863  
864  
865  
866 # ----------------------------------------------------------------------
867 # Text::BibTeX::StructuredEntry methods dealing with entry structure
868  
869 package Text::BibTeX::StructuredEntry;
870 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
871 1 our $VERSION = '1.00';
872  
873 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
874  
875 1 our @ISA = ('Text::BibTeX::Entry');
876 1 ▶use Text::BibTeX qw(:metatypes display_list);
Subroutine BEGIN: called CC=1 SCAR=0.0
877  
878 =head1 METHODS 2: BASE STRUCTURED ENTRY CLASS
879  
880 The other class provided by the C<Structure> module is
881 C<StructuredEntry>, the base class for all structured entry classes.
882 This class inherits from C<Entry>, so all of its entry
883 query/manipulation methods are available. C<StructuredEntry> adds
884 methods for checking that an entry conforms to the database structure
885 defined by a structure class.
886  
887 It only makes sense for C<StructuredEntry> to be used as a base class;
888 you would never create standalone C<StructuredEntry> objects. The
889 superficial reason for this is that only particular structured-entry
890 classes have an actual structure class associated with them,
891 C<StructuredEntry> on its own doesn't have any information about allowed
892 types, required fields, field constraints, and so on. For a deeper
893 understanding, consult L<"CLASS INTERACTIONS"> above.
894  
895 Since C<StructuredEntry> derives from C<Entry>, it naturally operates on
896 BibTeX entries. Hence, the following descriptions refer to "the
897 entry"---this is just the object (entry) being operated on. Note that
898 these methods are presented in bottom-up order, meaning that the methods
899 you're most likely to actually use---C<check>, C<coerce>, and
900 C<silently_coerce> are at the bottom. On a first reading, you'll
901 probably want to skip down to them for a quick summary.
902  
903 =over 4
904  
905 =item structure ()
906  
907 Returns the object that defines the structure the entry to which is
908 supposed to conform. This will be an instantiation of some structure
909 class, and exists mainly so the check/coerce methods can query the
910 structure about the types and fields it recognizes. If, for some
911 reason, you wanted to query an entry's structure about the validity of
912 type C<foo>, you might do this:
913  
914 # assume $entry is an object of some structured entry class, i.e.
915 # it inherits from Text::BibTeX::StructuredEntry
916 $structure = $entry->structure;
917 $foo_known = $structure->known_type ('foo');
918  
919 =cut
920  
921 sub structure
922 49 ▶{
Subroutine structure: called CC=1 SCAR=0.0
923 49 my $self = shift;
924 49 $self->{'structure'};
925 }
926  
927  
928 =item check_type ([WARN])
929  
930 Returns true if the entry has a valid type according to its structure.
931 If WARN is true, then an invalid type results in a warning being
932 printed.
933  
934 =cut
935  
936 sub check_type
937 0 ▶{
Subroutine check_type: not called CC=3 SCAR=24.8
938 0 my ($self, $warn) = @_;
939  
940 0 my $type = $self->{'type'};
941 0 ▶ if (! $self->{'structure'}->known_type ($type))
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"structure"}->known_type($type)) 0 0 0
942 {
943 0 ▶ $self->warn ("unknown entry type \"$type\"") if $warn;
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
944 0 return 0;
945 }
946 0 return 1;
947 }
948  
949  
950 =item check_required_fields ([WARN [, COERCE]])
951  
952 Checks that all required fields are present in the entry. If WARN is
953 true, then a warning is printed for every missing field. If COERCE is
954 true, then missing fields are set to the empty string.
955  
956 This isn't generally used by other code; see the C<check> and C<coerce>
957 methods below.
958  
959 =cut
960  
961 sub check_required_fields
962 0 ▶{
Subroutine check_required_fields: not called CC=8 SCAR=42.8
963 0 my ($self, $warn, $coerce) = @_;
964 0 my ($field, $warning);
965 0 my $num_errors = 0;
966
967 0 foreach $field ($self->{'structure'}->required_fields ($self->type))
968 {
969 0 ▶ if (! $self->exists ($field))
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->exists($field)) 0 0 0
970 {
971 0 ▶ $warning = "required field '$field' not present" if $warn;
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
972 0 ▶ if ($coerce)
Branch: 1 branch
BranchTrueFalseTotal
if ($coerce) 0 0 0
973 {
974 0 ▶ $warning .= " (setting to empty string)" if $warn;
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
975 0 $self->set ($field, '');
976 }
977 0 ▶ $self->warn ($warning) if $warn;
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
978 0 $num_errors++;
979 }
980 }
981
982 # Coercion is always successful, so if $coerce is true return true.
983 # Otherwise, return true if no errors found.
984  
985 0 ▶ return $coerce || ($num_errors == 0);
Condition0%0/3
$coerce || $num_errors == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$coerce || $num_errors == 0
$coerce $num_errors == 0
Truth table0%0/3
A || B
A= $coerceB= $num_errors == 0
ABresult
1X1
011
000
986  
987 } # check_required_fields
988  
989  
990 =item check_field_constraints ([WARN [, COERCE]])
991  
992 Checks that the entry conforms to all of the field constraints imposed
993 by its structure. Recall that a field constraint consists of a list of
994 fields, and a minimum and maximum number of those fields that must be
995 present in an entry. For each constraint, C<check_field_constraints>
996 simply counts how many fields in the constraint's field set are present.
997 If this count falls below the minimum or above the maximum for that
998 constraint and WARN is true, a warning is issued. In general, this
999 warning is of the form "between x and y of fields foo, bar, and baz must
1000 be present". The more common cases are handled specially to generate
1001 more useful and human-friendly warning messages.
1002  
1003 If COERCE is true, then the entry is modified to force it into
1004 conformance with all field constraints. How this is done depends on
1005 whether the violation is a matter of not enough fields present in the
1006 entry, or of too many fields present. In the former case, just enough
1007 fields are added (as empty strings) to meet the requirements of the
1008 constraint; in the latter case, fields are deleted. Which fields to add
1009 or delete is controlled by the order of fields in the constraint's field
1010 list.
1011  
1012 An example should clarify this. For instance, a field constraint
1013 specifying that exactly one of C<author> or C<editor> must appear in an
1014 entry would look like this:
1015  
1016 [1, 1, ['author', 'editor']]
1017  
1018 Suppose the following entry is parsed and expected to conform to this
1019 structure:
1020  
1021 @inbook{unknown:1997a,
1022 title = "An Unattributed Book Chapter",
1023 booktitle = "An Unedited Book",
1024 publisher = "Foo, Bar \& Company",
1025 year = 1997
1026 }
1027  
1028 If C<check_field_constraints> is called on this method with COERCE true
1029 (which is done by any of the C<full_check>, C<coerce>, and
1030 C<silently_coerce> methods), then the C<author> field is set to the
1031 empty string. (We go through the list of fields in the constraint's
1032 field set in order -- since C<author> is the first missing field, we
1033 supply it; with that done, the entry now conforms to the
1034 C<author>/C<editor> constraint, so we're done.)
1035  
1036 However, if the same structure was applied to this entry:
1037  
1038 @inbook{smith:1997a,
1039 author = "John Smith",
1040 editor = "Fred Jones",
1041 ...
1042 }
1043  
1044 then the C<editor> field would be deleted. In this case, we allow the
1045 first field in the constraint's field list---C<author>. Since only one
1046 field from the set may be present, all fields after the first one are in
1047 violation, so they are deleted.
1048  
1049 Again, this method isn't generally used by other code; rather, it is
1050 called by C<full_check> and its friends below.
1051  
1052 =cut
1053  
1054 sub check_field_constraints
1055 0 ▶{
Subroutine check_field_constraints: not called CC=19 SCAR=59.4
1056 0 my ($self, $warn, $coerce) = @_;
1057  
1058 0 my $num_errors = 0;
1059 0 my $constraint;
1060  
1061 0 foreach $constraint ($self->{'structure'}->field_constraints ($self->type))
1062 {
1063 0 my ($warning);
1064 0 my ($min, $max, $fields) = @$constraint;
1065  
1066 0 my $field;
1067 0 my $num_seen = 0;
1068 0 ▶ map { $num_seen++ if $self->exists ($_) } @$fields;
Branch: 1 branch
BranchTrueFalseTotal
if $self->exists($_) 0 0 0
1069  
1070 0 ▶ if ($num_seen < $min || $num_seen > $max)
Branch: 1 branch
BranchTrueFalseTotal
if ($num_seen < $min or $num_seen > $max) 0 0 0
Condition0%0/3
$num_seen < $min or $num_seen > $max
l!l&&r!l&&!r
000
MC/DC0%0/2
$num_seen < $min or $num_seen > $max
$num_seen < $min $num_seen > $max
Truth table0%0/3
A or B
A= $num_seen < $minB= $num_seen > $max
ABresult
1X1
011
000
1071 {
1072 0 ▶ if ($warn)
Branch: 1 branch
BranchTrueFalseTotal
if ($warn) 0 0 0
1073 {
1074 0 ▶ if ($min == 0 && $max > 0)
Branch: 3 branches
BranchTrueFalseTotal
if ($min == 0 and $max > 0) { } 0 0 0
elsif ($min < @$fields and $max == @$fields) { } 0 0 0
elsif ($min == $max) { } 0 0 0
Condition0%0/3
$min == 0 and $max > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$min == 0 and $max > 0
$min == 0 $max > 0
Truth table0%0/3
A and B
A= $min == 0B= $max > 0
ABresult
0X0
100
111
1075 {
1076 0 $warning = sprintf ("at most %d of fields %s may be present",
1077 $max, display_list ($fields, 1));
1078 }
1079 ▶ elsif ($min < @$fields && $max == @$fields)
Condition0%0/3
$min < @$fields and $max == @$fields
!ll&&!rl&&r
000
MC/DC0%0/2
$min < @$fields and $max == @$fields
$min < @$fields $max == @$fields
Truth table0%0/3
A and B
A= $min < @$fieldsB= $max == @$fields
ABresult
0X0
100
111
1080 {
1081 0 $warning = sprintf ("at least %d of fields %s must be present",
1082 $min, display_list ($fields, 1));
1083 }
1084 elsif ($min == $max)
1085 {
1086 0 ▶ $warning = sprintf ("exactly %d of fields %s %s be present",
Branch: 1 branch
BranchTrueFalseTotal
$num_seen < $min ? : 0 0 0
1087 $min, display_list ($fields, 1),
1088 ($num_seen < $min) ? "must" : "may");
1089 }
1090 else
1091 {
1092 0 $warning = sprintf ("between %d and %d of fields %s " .
1093 "must be present",
1094 $min, $max, display_list ($fields, 1))
1095 }
1096 }
1097  
1098 0 ▶ if ($coerce)
Branch: 1 branch
BranchTrueFalseTotal
if ($coerce) 0 0 0
1099 {
1100 0 ▶ if ($num_seen < $min)
Branch: 2 branches
BranchTrueFalseTotal
if ($num_seen < $min) { } 0 0 0
elsif ($num_seen > $max) { } 0 0 0
1101 {
1102 0 my @blank = @{$fields}[$num_seen .. ($min-1)];
1103 0 ▶ $warning .= sprintf (" (setting %s to empty string)",
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
1104 display_list (\@blank, 1))
1105 if $warn;
1106 0 @blank = map (($_, ''), @blank);
1107 0 $self->set (@blank);
1108 }
1109 elsif ($num_seen > $max)
1110 {
1111 0 my @delete = @{$fields}[$max .. ($num_seen-1)];
1112 0 ▶ $warning .= sprintf (" (deleting %s)",
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
1113 display_list (\@delete, 1))
1114 if $warn;
1115 0 $self->delete (@delete);
1116 }
1117 } # if $coerce
1118  
1119 0 ▶ $self->warn ($warning) if $warn;
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
1120 0 $num_errors++;
1121 } # if $num_seen out-of-range
1122  
1123 } # foreach $constraint
1124  
1125 # Coercion is always successful, so if $coerce is true return true.
1126 # Otherwise, return true if no errors found.
1127  
1128 0 ▶ return $coerce || ($num_errors == 0);
Condition0%0/3
$coerce || $num_errors == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$coerce || $num_errors == 0
$coerce $num_errors == 0
Truth table0%0/3
A || B
A= $coerceB= $num_errors == 0
ABresult
1X1
011
000
1129  
1130 } # check_field_constraints
1131  
1132  
1133 =item full_check ([WARN [, COERCE]])
1134  
1135 Returns true if an entry's type and fields are all valid. That is, it
1136 calls C<check_type>, C<check_required_fields>, and
1137 C<check_field_constraints>; if all of them return true, then so does
1138 C<full_check>. WARN and COERCE are simply passed on to the three
1139 C<check_*> methods: the first controls the printing of warnings, and the
1140 second decides whether we should modify the entry to force it into
1141 conformance.
1142  
1143 =cut
1144  
1145 sub full_check
1146 0 ▶{
Subroutine full_check: not called CC=4 SCAR=30.0
1147 0 my ($self, $warn, $coerce) = @_;
1148  
1149 0 ▶ return 1 unless $self->metatype == &BTE_REGULAR;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->metatype == &BTE_REGULAR 0 0 0
1150 0 ▶ return unless $self->check_type ($warn);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($warn) 0 0 0
1151 0 ▶ return $self->check_required_fields ($warn, $coerce) &&
Condition0%0/3
$self->check_required_fields($warn, $coerce) && $self->check_field_constraints($warn, $coerce)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->check_required_fields($warn, $coerce) && $self->check_field_constraints($warn, $coerce)
$self->check_required_fields($warn, $coerce) $self->check_field_constraints($warn, $coerce)
Truth table0%0/3
A && B
A= $self->check_required_fields($warn, $coerce)B= $self->check_field_constraints($warn, $coerce)
ABresult
0X0
100
111
1152 $self->check_field_constraints ($warn, $coerce);
1153 }
1154  
1155  
1156 # Front ends for full_check -- there are actually four possible wrappers,
1157 # but having both $warn and $coerce false is pointless.
1158  
1159 =item check ()
1160  
1161 Checks that the entry conforms to the requirements of its associated
1162 database structure: the type must be known, all required fields must be
1163 present, and all field constraints must be met. See C<check_type>,
1164 C<check_required_fields>, and C<check_field_constraints> for details.
1165  
1166 Calling C<check> is the same as calling C<full_check> with WARN true and
1167 COERCE false.
1168  
1169 =item coerce ()
1170  
1171 Same as C<check>, except entries are coerced into conformance with the
1172 database structure---that is, it's just like C<full_check> with both
1173 WARN and COERCE true.
1174  
1175 =item silently_coerce ()
1176  
1177 Same as C<coerce>, except warnings aren't printed---that is, it's just
1178 like C<full_check> with WARN false and COERCE true.
1179  
1180 =back
1181  
1182 =cut
1183  
1184 0 ▶sub check { shift->full_check (1, 0) }
Subroutine check: not called CC=1 SCAR=6.9
1185  
1186 0 ▶sub coerce { shift->full_check (1, 1) }
Subroutine coerce: not called CC=1 SCAR=6.9
1187  
1188 0 ▶sub silently_coerce { shift->full_check (0, 1) }
Subroutine silently_coerce: not called CC=1 SCAR=6.9
1189  
1190 1 1;
1191  
1192 =head1 SEE ALSO
1193  
1194 L<Text::BibTeX>, L<Text::BibTeX::Entry>, L<Text::BibTeX::File>
1195  
1196 =head1 AUTHOR
1197  
1198 Greg Ward <gward@python.net>
1199  
1200 =head1 COPYRIGHT
1201  
1202 Copyright (c) 1997-2000 by Gregory P. Ward. All rights reserved. This file
1203 is part of the Text::BibTeX library. This library is free software; you
1204 may redistribute it and/or modify it under the same terms as Perl itself.
↑ Summary blib/lib/Text/BibTeX/Value.pm »