blib/lib/Text/BibTeX.pm

filter: Statementstmt 82.3% 107 / 130 Branchbran 50.0% 32 / 64 Conditioncond 38.1% 21 / 55 MC/DCmcdc 20.6% 6 / 29 Subroutinesub 86.3% 19 / 22 Podpod 100% 4 / 4 totaltotal 62.1% 189 / 304 CC 55 SCAR 27.3
CC
55
Cov
64%
_process_result
56.0
bibloop
42.0
_process_argument
30.0
CRAP 15.3
« blib/lib/Text/BibTeX/Value.pm ↑ Summary blib/lib/Text/BibTeX/File.pm »
1 # ----------------------------------------------------------------------
2 # NAME : BibTeX.pm
3 # DESCRIPTION: Code for the Text::BibTeX module; loads up everything
4 # needed for parsing BibTeX files (both Perl and C code).
5 # CREATED : February 1997, Greg Ward
6 # MODIFIED :
7 # VERSION : $Id: BibTeX.pm 7274 2009-05-03 17:18:14Z ambs $
8 # COPYRIGHT : Copyright (c) 1997-2000 by Gregory P. Ward. All rights reserved.
9 #
10 # This file is part of the Text::BibTeX library. This
11 # library is free software; you may redistribute it and/or
12 # modify it under the same terms as Perl itself.
13 # ----------------------------------------------------------------------
14  
15 package Text::BibTeX;
16  
17 18 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 18 ▶use warnings::register; # the 'Text::BibTeX' warnings category
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 18 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 18 ▶use Unicode::Normalize ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 18 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 18 our $VERSION = '1.00';
28  
29 18 our %EXPORT_TAGS = (nodetypes => [qw(BTAST_STRING BTAST_MACRO BTAST_NUMBER)],
30 metatypes => [qw(BTE_UNKNOWN BTE_REGULAR BTE_COMMENT
31 BTE_PREAMBLE BTE_MACRODEF)],
32 nameparts => [qw(BTN_FIRST BTN_VON BTN_LAST BTN_JR BTN_NONE)],
33 joinmethods => [qw(BTJ_MAYTIE BTJ_SPACE
34 BTJ_FORCETIE BTJ_NOTHING)],
35 subs => [qw(bibloop split_list
36 purify_string change_case)],
37 macrosubs => [qw(add_macro_text
38 delete_macro
39 delete_all_macros
40 macro_length
41 macro_text)]);
42 18 our @EXPORT_OK = (@{$EXPORT_TAGS{'subs'}},
43 18 @{$EXPORT_TAGS{'macrosubs'}},
44 18 @{$EXPORT_TAGS{'nodetypes'}},
45 18 @{$EXPORT_TAGS{'nameparts'}},
46 18 @{$EXPORT_TAGS{'joinmethods'}},
47 'check_class', 'display_list' );
48 18 our @EXPORT = @{$EXPORT_TAGS{'metatypes'}};
49  
50 18 require XSLoader;
51 18 XSLoader::load('Text::BibTeX', $VERSION);
52  
53 18 require Text::BibTeX::Error;
54 18 require Text::BibTeX::Name;
55 18 require Text::BibTeX::NameFormat;
56  
57 # ----------------------------------------------------------------------
58 # Strings in and out of the C library
59 #
60 # In 'utf-8' mode (the default) all strings handed out are Perl character
61 # strings, normalised once when they come in (by default to NFC). In
62 # 'bytes' mode strings are passed through untouched, as byte strings.
63  
64 18 my %Normalisers = map { $_ => Unicode::Normalize->can($_) } qw(NFC NFD NFKC NFKD);
65  
66 154 ▶sub _options {
Subroutine _options: called CC=11 SCAR=25.3
67 154 my ($opts) = @_;
68 154 ▶ my %o = map { lc($_) => $opts->{$_} } keys %{$opts || {}};
Condition100%2/2
$opts || {}
l!l
10153
MC/DC100%1/1
$opts || {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
69 154 ▶ my $binmode = (!defined $o{binmode} || $o{binmode} =~ /^utf-?8$/i)
Branch: 1 branch
BranchTrueFalseTotal
!defined($o{'binmode'}) || $o{'binmode'} =~ /^utf-?8$/iu ? : 149 5 154
Condition100%3/3
not defined $o{"binmode"} or $o{"binmode"} =~ /^utf-?8$/iu
l!l&&r!l&&!r
81685
MC/DC100%2/2
not defined $o{"binmode"} or $o{"binmode"} =~ /^utf-?8$/iu
not defined $o{"binmode"} $o{"binmode"} =~ /^utf-?8$/iu
Truth table100%3/3
not(A) or B
A= not defined $o{"binmode"}B= $o{"binmode"} =~ /^utf-?8$/iu
ABresult
1X1
011
000
70 ? 'utf-8' : 'bytes';
71 croak "binmode must be 'utf-8' or 'bytes'"
72 154 ▶ if defined $o{binmode} && $o{binmode} !~ /^(?:utf-?8|bytes|raw)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $o{"binmode"} and not $o{"binmode"} =~ /^(?:utf-?8|bytes|raw)$/iu 0 154 154
Condition66%2/3
defined $o{"binmode"} and not $o{"binmode"} =~ /^(?:utf-?8|bytes|raw)$/iu
!ll&&!rl&&r
81730
MC/DC0%0/2
defined $o{"binmode"} and not $o{"binmode"} =~ /^(?:utf-?8|bytes|raw)$/iu
defined $o{"binmode"} not $o{"binmode"} =~ /^(?:utf-?8|bytes|raw)$/iu
Truth table66%2/3
A and not(B)
A= defined $o{"binmode"}B= not $o{"binmode"} =~ /^(?:utf-?8|bytes|raw)$/iu
ABresult
0X0
100
111
73 154 ▶ my $norm = exists $o{normalization} ? $o{normalization} : 'NFC';
Branch: 1 branch
BranchTrueFalseTotal
exists $o{'normalization'} ? : 59 95 154
74 154 ▶ $norm = 'none' if !defined $norm || $norm eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $norm or $norm eq "" 0 154 154
Condition33%1/3
not defined $norm or $norm eq ""
l!l&&r!l&&!r
00154
MC/DC0%0/2
not defined $norm or $norm eq ""
not defined $norm $norm eq ""
Truth table33%1/3
not(A) or B
A= not defined $normB= $norm eq ""
ABresult
1X1
011
000
75 croak "unknown normalization '$norm'"
76 154 ▶ unless $norm eq 'none' || $Normalisers{$norm};
Branch: 1 branch
BranchTrueFalseTotal
unless $norm eq "none" or $Normalisers{$norm} 0 154 154
Condition33%1/3
$norm eq "none" or $Normalisers{$norm}
l!l&&r!l&&!r
01540
MC/DC0%0/2
$norm eq "none" or $Normalisers{$norm}
$norm eq "none" $Normalisers{$norm}
Truth table33%1/3
A or B
A= $norm eq "none"B= $Normalisers{$norm}
ABresult
1X1
011
000
77 154 return ($binmode, $norm, \%o);
78 }
79  
80 # Normalise a character string (cheaply skipping pure ASCII)
81 1085 ▶sub _normalise {
Subroutine _normalise: called CC=4 SCAR=13.9
82 1085 my ($norm, $s) = @_;
83 1085 ▶ return $s if !defined $s || $norm eq 'none' || $s !~ /[^\x00-\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $s or $norm eq "none" or not $s =~ /[^\x00-\x7f]/u 979 106 1085
Condition83%5/6
not defined $s or $norm eq "none" or not $s =~ /[^\x00-\x7f]/u
l!l&&r!l&&!r
20959106
not defined $s or $norm eq "none"
l!l&&r!l&&!r
2001065
MC/DC66%2/3
not defined $s or $norm eq "none" or not $s =~ /[^\x00-\x7f]/u
not defined $s $norm eq "none" not $s =~ /[^\x00-\x7f]/u
Truth table75%3/4
not(A) or B or not(C)
A= not defined $sB= $norm eq "none"C= not $s =~ /[^\x00-\x7f]/u
ABCresult
1XX1
01X1
0011
0000
84 106 return $Normalisers{$norm}->($s);
85 }
86  
87 # A string coming from the user, in internal form. In 'utf-8' mode a
88 # byte string holding valid UTF-8 is decoded (so encoded and character
89 # strings both work); anything else is taken as characters.
90 472 ▶sub _in {
Subroutine _in: called CC=7 SCAR=21.0
91 472 my ($binmode, $norm, $s) = @_;
92 472 ▶ return $s if !defined $s || ref $s || $binmode ne 'utf-8';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $s or ref $s or $binmode ne "utf-8" 0 472 472
Condition33%2/6
not defined $s or ref $s or $binmode ne "utf-8"
l!l&&r!l&&!r
00472
not defined $s or ref $s
l!l&&r!l&&!r
00472
MC/DC0%0/3
not defined $s or ref $s or $binmode ne "utf-8"
not defined $s ref $s $binmode ne "utf-8"
Truth table25%1/4
not(A) or B or C
A= not defined $sB= ref $sC= $binmode ne "utf-8"
ABCresult
1XX1
01X1
0011
0000
93 472 ▶ return $s if $s !~ /[^\x00-\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /[^\x00-\x7f]/u 443 29 472
94 29 ▶ if (!utf8::is_utf8($s)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::is_utf8($s)) 1 28 29
95 1 my $c = $s;
96 1 ▶ $s = $c if utf8::decode($c);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::decode($c) 1 0 1
97 }
98 29 return _normalise($norm, $s);
99 }
100  
101 # A string being handed to the C library: in 'bytes' mode a character
102 # string is passed as its UTF-8 encoding (so results come back as bytes);
103 # in 'utf-8' mode, as for _in.
104 123 ▶sub _to_c {
Subroutine _to_c: called CC=5 SCAR=17.3
105 123 my ($binmode, $norm, $s) = @_;
106 123 ▶ if ($binmode ne 'utf-8') {
Branch: 1 branch
BranchTrueFalseTotal
if ($binmode ne "utf-8") 3 120 123
107 3 ▶ if (defined $s && !ref $s && utf8::is_utf8($s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s and not ref $s and utf8::is_utf8($s)) 3 0 3
Condition33%2/6
defined $s and not ref $s and utf8::is_utf8($s)
!ll&&!rl&&r
003
defined $s and not ref $s
!ll&&!rl&&r
003
MC/DC0%0/3
defined $s and not ref $s and utf8::is_utf8($s)
defined $s not ref $s utf8::is_utf8($s)
Truth table25%1/4
A and not(B) and C
A= defined $sB= not ref $sC= utf8::is_utf8($s)
ABCresult
0XX0
10X0
1100
1111
108 3 $s = "$s";
109 3 utf8::encode($s);
110 }
111 3 return $s;
112 }
113 120 return _in($binmode, $norm, $s);
114 }
115  
116 # Compatibility shims for code that used the old (private) helpers
117 0 ▶sub _process_result {
Subroutine _process_result: not called CC=7 SCAR=40.3
118 0 my ($self, $result, $binmode, $norm) = @_;
119 0 ▶ return $result unless defined $result && ($binmode // '') eq 'utf-8';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result and ($binmode // "") eq "utf-8" 0 0 0
Condition0%0/5
defined $result and ($binmode // "") eq "utf-8"
!ll&&!rl&&r
000
$binmode // ""
l!l
00
MC/DC0%0/2
defined $result and ($binmode // "") eq "utf-8"
defined $result $binmode
Truth table0%0/3
A and B
A= defined $resultB= $binmode
ABresult
0X0
100
111
120 0 ▶ if (!utf8::is_utf8($result)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::is_utf8($result)) 0 0 0
121 0 my $c = $result;
122 0 ▶ $result = $c if utf8::decode($c);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::decode($c) 0 0 0
123 }
124 0 ▶ return _normalise($norm || 'NFC', $result);
Condition0%0/2
$norm || 'NFC'
l!l
00
MC/DC0%0/1
$norm || 'NFC'
$norm
Truth table0%0/2
A
A= $norm
Aresult
11
00
125 }
126  
127 0 ▶sub _process_argument {
Subroutine _process_argument: not called CC=5 SCAR=34.0
128 0 my ($self, $value, $binmode) = @_;
129 0 ▶ return Encode::encode('UTF-8', $value)
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and ($binmode // "") eq "utf-8" and utf8::is_utf8($value) 0 0 0
Condition0%0/8
defined $value and ($binmode // "") eq "utf-8" and utf8::is_utf8($value)
!ll&&!rl&&r
000
defined $value and ($binmode // "") eq "utf-8"
!ll&&!rl&&r
000
$binmode // ""
l!l
00
MC/DC0%0/3
defined $value and ($binmode // "") eq "utf-8" and utf8::is_utf8($value)
defined $value $binmode utf8::is_utf8($value)
Truth table0%0/4
A and B and C
A= defined $valueB= $binmodeC= utf8::is_utf8($value)
ABCresult
0XX0
10X0
1100
1111
130 if defined $value && ($binmode // '') eq 'utf-8' && utf8::is_utf8($value);
131 0 return $value;
132 }
133  
134 38 ▶sub split_list {
Subroutine split_list: called CC=2 SCAR=6.9
Pod documented
135 38 my ( $field, $delim, $filename, $line, $desc, $opts ) = @_;
136 38 my ($binmode, $norm) = _options($opts);
137 38 ▶ my @list = Text::BibTeX::isplit_list( _to_c($binmode, $norm, $field), $delim,
Condition100%2/2
$line // 0
l!l
2513
MC/DC100%1/1
$line // 0
$line
Truth table100%2/2
A
A= $line
Aresult
11
00
138 $filename, $line // 0, $desc );
139 38 return @list;
140 }
141  
142 =encoding UTF-8
143  
144 =head1 NAME
145  
146 Text::BibTeX - interface to read and parse BibTeX files
147  
148 =head1 SYNOPSIS
149  
150 use Text::BibTeX;
151  
152 my $bibfile = Text::BibTeX::File->new("foo.bib");
153 my $newfile = Text::BibTeX::File->new(">newfoo.bib");
154  
155 while ($entry = Text::BibTeX::Entry->new($bibfile))
156 {
157 next unless $entry->parse_ok;
158  
159 . # hack on $entry contents, using various
160 . # Text::BibTeX::Entry methods
161 .
162  
163 $entry->write ($newfile);
164 }
165  
166 =head1 DESCRIPTION
167  
168 The C<Text::BibTeX> module serves mainly as a high-level introduction to
169 the C<Text::BibTeX> library, for both code and documentation purposes.
170 The code loads the two fundamental modules for processing BibTeX files
171 (C<Text::BibTeX::File> and C<Text::BibTeX::Entry>), and this
172 documentation gives a broad overview of the whole library that isn't
173 available in the documentation for the individual modules that comprise
174 it.
175  
176 In addition, the C<Text::BibTeX> module provides a number of
177 miscellaneous functions that are useful in processing BibTeX data
178 (especially the kind that comes from bibliographies as defined by BibTeX
179 0.99, rather than generic database files). These functions don't
180 generally fit in the object-oriented class hierarchy centred around the
181 C<Text::BibTeX::Entry> class, mainly because they are specific to
182 bibliographic data and operate on generic strings (rather than being
183 tied to a particular BibTeX entry). These are also documented here, in
184 L<"MISCELLANEOUS FUNCTIONS">.
185  
186 Note that every module described here begins with the C<Text::BibTeX>
187 prefix. For brevity, I have dropped this prefix from most class and
188 module names in the rest of this manual page (and in most of the other
189 manual pages in the library).
190  
191 =head1 STRINGS AND UNICODE
192  
193 By default (C<binmode =E<gt> 'utf-8'>), C<Text::BibTeX> reads its input
194 as UTF-8 and every string it gives you -- entry types, keys, field names,
195 values, name parts, formatted names -- is a Perl character string,
196 normalised once, when it is read, to Unicode Normalization Form C (or
197 whichever form you ask for with the C<normalization> option). Bytes that
198 are not valid UTF-8 are replaced by U+FFFD, with a warning; to read files
199 in other encodings use the C<encoding> option of
200 L<Text::BibTeX::File/open>, or pass in a filehandle with an
201 C<:encoding(...)> layer. Strings you pass in may be character strings or
202 UTF-8 encoded byte strings.
203  
204 Unicode is supported throughout: names (entry types, keys, field and
205 macro names) may contain any characters; field names and entry types are
206 lowercased, and macro names compared, using full Unicode case mappings;
207 and splitting and formatting names, L</purify_string> and
208 L</change_case> classify and case-map characters (whole grapheme
209 clusters, for initials) according to Unicode. The Unicode tables are
210 built from the Unicode database of the perl C<Text::BibTeX> was built
211 with; L</unicode_version> tells you which version that is.
212  
213 TeX markup is passed through unchanged. BibTeX's own rules for TeX
214 "special characters" such as C<{\'e}> (which count as one character for
215 initials, and are treated specially by L</purify_string> and
216 L</change_case>) are followed, but TeX is never converted to Unicode or
217 back; do that separately if you need to.
218  
219 With C<binmode =E<gt> 'bytes'> nothing is decoded or normalised: strings
220 are passed through as byte strings, exactly as they appear in the input.
221  
222 =head1 ERRORS AND WARNINGS
223  
224 Problems found while parsing (syntax errors, undefined macros, malformed
225 names, and so on) are reported as L<Text::BibTeX::Error> objects. Each
226 entry keeps the errors found while it was parsed (see
227 L<Text::BibTeX::Entry/errors>), and each error is also passed to Perl's
228 C<warn>, with the same text as in earlier versions, for example:
229  
230 foo.bib, line 12, warning: undefined macro "jan"
231  
232 so it goes wherever C<STDERR> goes and can be caught with
233 C<$SIG{__WARN__}>. These warnings follow the warnings pragma of the
234 calling code, using the category C<Text::BibTeX>: they are on by default,
235 are silenced by C<no warnings 'Text::BibTeX'>, and become exceptions
236 under C<use warnings FATAL =E<gt> 'Text::BibTeX'>. Misuse of the library
237 is always reported with C<die>. See L<Text::BibTeX::Error> for details,
238 including how to take over reporting completely.
239  
240 =head1 UPGRADING FROM 0.x
241  
242 Version 1.00 replaced the C library at the heart of C<Text::BibTeX>.
243 Programs written for earlier versions should mostly work unchanged, but
244 note the following:
245  
246 =over 4
247  
248 =item *
249  
250 Strings are now returned as Perl character strings by default (as if
251 you had given C<binmode =E<gt> 'utf-8'>, which was the recommended
252 option). If your code expects UTF-8 encoded byte strings, pass
253 C<binmode =E<gt> 'bytes'> to L<Text::BibTeX::File/open>,
254 L<Text::BibTeX::Entry/new> and L<Text::BibTeX::Name/new>. In C<'utf-8'>
255 mode, invalid UTF-8 in the input is now replaced (with a warning) rather
256 than passed through.
257  
258 =item *
259  
260 Errors and warnings are reported through Perl's C<warn> (see
261 L</"ERRORS AND WARNINGS">) instead of being written directly to the C
262 library's standard error stream; usage errors throw exceptions instead
263 of terminating the program.
264  
265 =item *
266  
267 Field names and entry types are lowercased according to Unicode, so
268 C<ÉDITOR> becomes C<éditor> (it used to become C<Éditor>), and
269 L</purify_string> and L</change_case> now handle non-ASCII letters.
270  
271 =item *
272  
273 Error recovery is better: after a syntax error the parser resynchronises
274 at the next entry, and a missing closing brace no longer causes the
275 following entry to be lost. Some messages after the first error in a
276 damaged file may therefore differ. "N characters of junk" warnings are
277 now reported with the entry that follows the junk.
278  
279 =item *
280  
281 Any Perl filehandle can be read from, including in-memory handles and
282 handles with I/O layers.
283  
284 =item *
285  
286 The C library (derived from B<btparse>) is compiled into the module
287 and is no longer installed as a separate shared library. The C<biblex>,
288 C<bibparse> and C<dumpnames> programs and the B<btparse> manual pages
289 that came with it are gone: the BibTeX language is described in
290 L<Text::BibTeX::Language>, and the rules for splitting and formatting
291 names, purifying strings, changing case and processing field values are
292 described in the documentation of the modules that use them.
293  
294 =item *
295  
296 Perl 5.26 or later is required.
297  
298 =back
299  
300 =head1 MODULES AND CLASSES
301  
302 The C<Text::BibTeX> library includes a number of modules, many of which
303 provide classes. Usually, the relationship is simple and obvious: a
304 module provides a class of the same name---for instance, the
305 C<Text::BibTeX::Entry> module provides the C<Text::BibTeX::Entry> class.
306 There are a few exceptions, though: most obviously, the C<Text::BibTeX>
307 module doesn't provide any classes itself, it merely loads two modules
308 (C<Text::BibTeX::Entry> and C<Text::BibTeX::File>) that do. The other
309 exceptions are mentioned in the descriptions below, and discussed in
310 detail in the documentation for the respective modules.
311  
312 The modules are presented roughly in order of increasing specialization:
313 the first three are essential for any program that processes BibTeX data
314 files, regardless of what kind of data they hold. The later modules are
315 specialized for use with bibliographic databases, and serve both to
316 emulate BibTeX 0.99's standard styles and to provide an example of how
317 to define a database structure through such specialized modules. Each
318 module is fully documented in its respective manual page.
319  
320 =over 4
321  
322 =item C<Text::BibTeX>
323  
324 Loads the two fundamental modules (C<Entry> and C<File>), and provides a
325 number of miscellaneous functions that don't fit anywhere in the class
326 hierarchy.
327  
328 =item C<Text::BibTeX::File>
329  
330 Provides an object-oriented interface to BibTeX database files. In
331 addition to the obvious attributes of filename and filehandle, the
332 "file" abstraction manages properties such as the database structure and
333 options for it.
334  
335 =item C<Text::BibTeX::Entry>
336  
337 Provides an object-oriented interface to BibTeX entries, which can be
338 parsed from C<File> objects, arbitrary filehandles, or strings. Manages
339 all the properties of a single entry: type, key, fields, and values.
340 Also serves as the base class for the I<structured entry classes>
341 (described in detail in L<Text::BibTeX::Structure>).
342  
343 =item C<Text::BibTeX::Value>
344  
345 Provides an object-oriented interface to I<values> and I<simple values>,
346 high-level constructs that can be used to represent the strings
347 associated with each field in an entry. Normally, field values are
348 returned simply as Perl strings, with macros expanded and multiple
349 strings "pasted" together. If desired, you can instruct C<Text::BibTeX>
350 to return C<Text::BibTeX::Value> objects, which give you access to the
351 original form of the data.
352  
353 =item C<Text::BibTeX::Structure>
354  
355 Provides the C<Structure> and C<StructuredEntry> classes, which serve
356 primarily as base classes for the two kinds of classes that define
357 database structures. Read this man page for a comprehensive description
358 of the mechanism for implementing Perl classes analogous to BibTeX
359 "style files".
360  
361 =item C<Text::BibTeX::Bib>
362  
363 Provides the C<BibStructure> and C<BibEntry> classes, which serve two
364 purposes: they fulfill the same role as the standard style files of
365 BibTeX 0.99, and they give an example of how to write new database
366 structures. These ultimately derive from, respectively, the
367 C<Structure> and C<StructuredEntry> classes provided by the C<Structure>
368 module.
369  
370 =item C<Text::BibTeX::BibSort>
371  
372 One of the C<BibEntry> class's base classes: handles the generation of
373 sort keys for sorting prior to output formatting.
374  
375 =item C<Text::BibTeX::BibFormat>
376  
377 One of the C<BibEntry> class's base classes: handles the formatting of
378 bibliographic data for output in a markup language such as LaTeX.
379  
380 =item C<Text::BibTeX::Name>
381  
382 A class used by the C<Bib> structure and specific to bibliographic data
383 as defined by BibTeX itself: parses individual author names into
384 "first", "von", "last", and "jr" parts.
385  
386 =item C<Text::BibTeX::NameFormat>
387  
388 Also specific to bibliographic data: puts split-up names (as parsed by
389 the C<Name> class) back together in a custom way.
390  
391 =back
392  
393 The BibTeX data language itself, as C<Text::BibTeX> parses it, is
394 described in L<Text::BibTeX::Language>.
395  
396 For a first time through the library, you'll probably want to confine
397 your reading to L<Text::BibTeX::File> and L<Text::BibTeX::Entry>. The
398 other modules will come in handy eventually, especially if you need to
399 emulate BibTeX in a fairly fine grained way (e.g. parsing names,
400 generating sort keys). But for the simple database hacks that are the
401 bread and butter of the C<Text::BibTeX> library, the C<File> and
402 C<Entry> classes are the bulk of what you'll need. You may also find
403 some of the material in this manual page useful, namely L<"CONSTANT
404 VALUES"> and L<"UTILITY FUNCTIONS">.
405  
406 =cut
407  
408 sub AUTOLOAD
409 16 ▶{
Subroutine AUTOLOAD: called CC=5 SCAR=16.4
410 # This AUTOLOAD is used to 'autoload' constants from the constant()
411 # XS function.
412  
413 16 our $AUTOLOAD;
414 16 my ($constname, $val);
415 16 ($constname = $AUTOLOAD) =~ s/.*:://;
416 16 ▶ return if $constname eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $constname eq "DESTROY" 0 16 16
417 16 ▶ carp ("Recursive AUTOLOAD--probable compilation error"), return
Branch: 1 branch
BranchTrueFalseTotal
if $constname eq "constant" 0 16 16
418 if $constname eq 'constant';
419 16 ▶ $val = constant ($constname)
Branch: 1 branch
BranchTrueFalseTotal
if $constname =~ /^BT/u 16 0 16
420 if $constname =~ /^BT/;
421 16 ▶ croak ("Unknown Text::BibTeX function: \"$constname\"")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 16 16
422 unless (defined $val);
423  
424 18 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
425 16 ▶ *$AUTOLOAD = sub { $val };
Subroutine __ANON__: called CC=1 SCAR=0.0
426 16 $val;
427 }
428  
429 # Load the two fundamental classes in the Text::BibTeX hierarchy
430 18 require Text::BibTeX::File;
431 18 require Text::BibTeX::Entry;
432  
433 18 _define_months();
434  
435 18 ▶sub _define_months {
Subroutine _define_months: called CC=2 SCAR=6.9
436 18 for my $month (qw.january february march april may june
437 july august september october november december.) {
438 216 add_macro_text(substr($month, 0, 3), ucfirst($month));
439 }
440 }
441  
442  
443 =head1 EXPORTS
444  
445 The C<Text::BibTeX> module has a number of optional exports, most of
446 them constant values described in L<"CONSTANT VALUES"> below. The
447 default exports are a subset of these constant values that are used
448 particularly often, the "entry metatypes" (also accessible via the
449 export tag C<metatypes>). Thus, the following two lines are equivalent:
450  
451 use Text::BibTeX;
452 use Text::BibTeX qw(:metatypes);
453  
454 Some of the various subroutines provided by the module are also
455 exportable. C<bibloop>, C<split_list>, C<purify_string>, and
456 C<change_case> are all useful in everyday processing of BibTeX data, but
457 don't really fit anywhere in the class hierarchy. They may be imported
458 from C<Text::BibTeX> using the C<subs> export tag. C<check_class> and
459 C<display_list> are also exportable, but only by name; they are not
460 included in any export tag. (These two mainly exist for use by other
461 modules in the library.) For instance, to use C<Text::BibTeX> and
462 import the entry metatype constants and the common subroutines:
463  
464 use Text::BibTeX qw(:metatypes :subs);
465  
466 Another group of subroutines exists for direct manipulation of the macro
467 table maintained by the underlying C library. These functions (see
468 L<"Macro table functions">, below) allow you to define, delete, and
469 query the value of BibTeX macros (or "abbreviations"). They may be
470 imported I<en masse> using the C<macrosubs> export tag:
471  
472 use Text::BibTeX qw(:macrosubs);
473  
474 =head1 CONSTANT VALUES
475  
476 The C<Text::BibTeX> module makes a number of constant values available.
477 These correspond to the values of various enumerated types in the
478 underlying C library.
479  
480 Each group of constants is optionally exportable using an export tag
481 given in the descriptions below.
482  
483 =over 4
484  
485 =item Entry metatypes
486  
487 C<BTE_UNKNOWN>, C<BTE_REGULAR>, C<BTE_COMMENT>, C<BTE_PREAMBLE>,
488 C<BTE_MACRODEF>. The C<metatype> method in the C<Entry> class always
489 returns one of these values. The latter three describe, respectively,
490 C<comment>, C<preamble>, and C<string> entries; C<BTE_REGULAR> describes
491 all other entry types. C<BTE_UNKNOWN> should never be seen (it's mainly
492 useful for C code that might have to detect half-baked data structures).
493 Export tag: C<metatypes>.
494  
495 =item AST node types
496  
497 C<BTAST_STRING>, C<BTAST_MACRO>, C<BTAST_NUMBER>. Used to distinguish
498 the three kinds of simple values---strings, macros, and numbers. The
499 C<SimpleValue> class' C<type> method always returns one of these three
500 values. See also L<Text::BibTeX::Value>. Export tag:
501 C<nodetypes>.
502  
503 =item Name parts
504  
505 C<BTN_FIRST>, C<BTN_VON>, C<BTN_LAST>, C<BTN_JR>, C<BTN_NONE>. Used to
506 specify the various parts of a name after it has been split up. These
507 are mainly useful when using the C<NameFormat> class. See also
508 L<Text::BibTeX::Name> and L<Text::BibTeX::NameFormat>. Export tag:
509 C<nameparts>.
510  
511 =item Join methods
512  
513 C<BTJ_MAYTIE>, C<BTJ_SPACE>, C<BTJ_FORCETIE>, C<BTJ_NOTHING>. Used to
514 tell the C<NameFormat> class how to join adjacent tokens together; see
515 L<Text::BibTeX::NameFormat/"FORMATTING RULES">. Export tag:
516 C<joinmethods>.
517  
518 =back
519  
520 =head1 UTILITY FUNCTIONS
521  
522 C<Text::BibTeX> provides several functions that operate outside of the
523 normal class hierarchy. Of these, only C<bibloop> is likely to be of
524 much use to you in writing everyday BibTeX-hacking programs; the other
525 two (C<check_class> and C<display_list>) are mainly provided for the use
526 of other modules in the library. They are documented here mainly for
527 completeness, but also because they might conceivably be useful in other
528 circumstances.
529  
530 =over 4
531  
532 =item bibloop (ACTION, FILES [, DEST])
533  
534 Loops over all entries in a set of BibTeX files, performing some
535 caller-supplied action on each entry. FILES should be a reference to
536 the list of filenames to process, and ACTION a reference to a subroutine
537 that will be called on each entry. DEST, if given, should be a
538 C<Text::BibTeX::File> object (opened for output) to which entries might
539 be printed.
540  
541 The subroutine referenced by ACTION is called with exactly one argument:
542 the C<Text::BibTeX::Entry> object representing the entry currently being
543 processed. Information about both the entry itself and the file where
544 it originated is available through this object; see
545 L<Text::BibTeX::Entry>. The ACTION subroutine is only called if the
546 entry was successfully parsed; any syntax errors will result in a
547 warning message being printed, and that entry being skipped. Note that
548 I<all> successfully parsed entries are passed to the ACTION subroutine,
549 even C<preamble>, C<string>, and C<comment> entries. To skip these
550 pseudo-entries and only process "regular" entries, then your action
551 subroutine should look something like this:
552  
553 sub action {
554 my $entry = shift;
555 return unless $entry->metatype == BTE_REGULAR;
556 # process $entry ...
557 }
558  
559 If your action subroutine needs any more arguments, you can just create
560 a closure (anonymous subroutine) as a wrapper, and pass it to
561 C<bibloop>:
562  
563 sub action {
564 my ($entry, $extra_stuff) = @_;
565 # ...
566 }
567  
568 my $extra = ...;
569 Text::BibTeX::bibloop (sub { &action ($_[0], $extra) }, \@files);
570  
571 If the ACTION subroutine returns a true value and DEST was given, then
572 the processed entry will be written to DEST.
573  
574 =cut
575  
576 # ----------------------------------------------------------------------
577 # NAME : bibloop
578 # INPUT : $action
579 # $files
580 # $dest
581 # OUTPUT :
582 # RETURNS :
583 # DESCRIPTION: Loops over all entries in a set of files, calling
584 # &$action on each one.
585 # CREATED : summer 1996 (in original Bibtex.pm module)
586 # MODIFIED : May 1997 (added to Text::BibTeX with revisions)
587 # Feb 1998 (simplified and documented)
588 # ----------------------------------------------------------------------
589 sub bibloop (&$;$)
590 0 ▶{
Subroutine bibloop: not called CC=6 SCAR=37.4
Pod documented
591 0 my ($action, $files, $dest) = @_;
592  
593 0 my $file;
594 0 ▶ while ($file = shift @$files)
Branch: 1 branch
BranchTrueFalseTotal
if ($file = shift @$files) 0 0 0
595 {
596 0 my $bib = Text::BibTeX::File->new($file);
597
598 0 ▶ while (! $bib->eof())
Branch: 1 branch
BranchTrueFalseTotal
unless ($bib->eof) 0 0 0
599 {
600 0 my $entry = Text::BibTeX::Entry->new($bib);
601 0 ▶ next unless $entry->parse_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->parse_ok 0 0 0
602  
603 0 my $result = &$action ($entry);
604 0 ▶ $entry->write ($dest, 1)
Branch: 1 branch
BranchTrueFalseTotal
if $result and $dest 0 0 0
Condition0%0/3
$result and $dest
!ll&&!rl&&r
000
MC/DC0%0/2
$result and $dest
$result $dest
Truth table0%0/3
A and B
A= $resultB= $dest
ABresult
0X0
100
111
605 if ($result && $dest)
606 }
607 }
608 }
609  
610 =item check_class (PACKAGE, DESCRIPTION, SUPERCLASS, METHODS)
611  
612 Ensures that a PACKAGE implements a class meeting certain requirements.
613 First, it inspects Perl's symbol tables to ensure that a package named
614 PACKAGE actually exists. Then, it ensures that the class named by
615 PACKAGE derives from SUPERCLASS (using the universal method C<isa>).
616 This derivation might be through multiple inheritance, or through
617 several generations of a class hierarchy; the only requirement is that
618 SUPERCLASS is somewhere in PACKAGE's tree of base classes. Finally, it
619 checks that PACKAGE provides each method listed in METHODS (a reference
620 to a list of method names). This is done with the universal method
621 C<can>, so the methods might actually come from one of PACKAGE's base
622 classes.
623  
624 DESCRIPTION should be a brief string describing the class that was
625 expected to be provided by PACKAGE. It is used for generating warning
626 messages if any of the class requirements are not met.
627  
628 This is mainly used by the supervisory code in
629 C<Text::BibTeX::Structure>, to ensure that user-supplied structure
630 modules meet the rules required of them.
631  
632 =cut
633  
634 # ----------------------------------------------------------------------
635 # NAME : check_class
636 # INPUT : $package - the name of a package that is expected to exist
637 # $description
638 # - string describing what the package is
639 # $superclass
640 # - a package name from which $package is expected
641 # to inherit
642 # $methods - ref to list of method names expected to be
643 # available via $package (possibly through
644 # inheritance)
645 # OUTPUT :
646 # RETURNS :
647 # DESCRIPTION: Makes sure that a package named by $package exists
648 # (by following the chain of symbol tables starting
649 # at %::) Dies if not.
650 # CALLERS : Text::BibTeX::Structure::new
651 # CREATED : 1997/09/09, GPW
652 # MODIFIED :
653 # ----------------------------------------------------------------------
654 sub check_class
655 2 ▶{
Subroutine check_class: called CC=7 SCAR=21.5
Pod documented
656 2 my ($package, $description, $superclass, $methods) = @_;
657 2 my (@components, $component, $prev_symtab);
658  
659 2 @components = split ('::', $package);
660 2 $prev_symtab = \%::;
661 2 ▶ while (@components)
Branch: 1 branch
BranchTrueFalseTotal
if (@components) 6 2 8
662 {
663 6 $component = (shift @components) . '::';
664 6 ▶ unless (defined ($prev_symtab = $prev_symtab->{$component}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($prev_symtab = $prev_symtab->{$component})) 0 6 6
665 {
666 0 die "Text::BibTeX::Structure: $description " .
667 "\"$package\" apparently not supplied\n";
668 }
669 }
670  
671 2 ▶ if ($superclass && ! $package->isa($superclass))
Branch: 1 branch
BranchTrueFalseTotal
if ($superclass and not $package->isa($superclass)) 0 2 2
Condition33%1/3
$superclass and not $package->isa($superclass)
!ll&&!rl&&r
020
MC/DC0%0/2
$superclass and not $package->isa($superclass)
$superclass not $package->isa($superclass)
Truth table33%1/3
A and not(B)
A= $superclassB= not $package->isa($superclass)
ABresult
0X0
100
111
672 {
673 0 die "Text::BibTeX::Structure: $description \"$package\" " .
674 "improperly defined: ! isa ($superclass)\n";
675 }
676  
677 2 my $method;
678 2 for $method (@$methods)
679 {
680 3 ▶ unless ($package->can($method))
Branch: 1 branch
BranchTrueFalseTotal
unless ($package->can($method)) 0 3 3
681 {
682 0 die "Text::BibTeX::Structure: $description \"$package\" " .
683 "improperly defined: no method \"$method\"\n";
684 }
685 }
686 } # &check_class
687  
688  
689 =item display_list (LIST, QUOTE)
690  
691 Converts a list of strings to the grammatical conventions of a human
692 language (currently, only English rules are supported). LIST must be a
693 reference to a list of strings. If this list is empty, the empty string
694 is returned. If it has one element, then just that element is
695 returned. If it has two elements, then they are joined with the string
696 C<" and "> and the resulting string is returned. Otherwise, the list
697 has I<N> elements for I<N> E<gt>= 3; elements 1..I<N>-1 are joined with
698 commas, and the final element is tacked on with an intervening
699 C<", and ">.
700  
701 If QUOTE is true, then each string is encased in single quotes before
702 anything else is done.
703  
704 This is used elsewhere in the library for two very distinct purposes:
705 for generating warning messages describing lists of fields that should
706 be present or are conflicting in an entry, and for generating lists of
707 author names in formatted bibliographies.
708  
709 =cut
710  
711 # ----------------------------------------------------------------------
712 # NAME : display_list
713 # INPUT : $list - reference to list of strings to join
714 # $quote - if true, they will be single-quoted before join
715 # OUTPUT :
716 # RETURNS : elements of @$list, joined together into a single string
717 # with commas and 'and' as appropriate
718 # DESCRIPTION: Formats a list of strings for display as English text.
719 # CALLERS : Text::BibTeX::Structure::check_interacting_fields
720 # CALLS :
721 # CREATED : 1997/09/23, GPW
722 # MODIFIED :
723 # ----------------------------------------------------------------------
724 sub display_list
725 9 ▶{
Subroutine display_list: called CC=5 SCAR=17.5
Pod documented
726 9 my ($list, $quote) = @_;
727 9 my @list;
728  
729 9 ▶ return '' if @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 0 9 9
730 0 ▶ @list = $quote ? map { "'$_'" } @$list : @$list;
Branch: 1 branch
BranchTrueFalseTotal
$quote ? : 0 9 9
731 9 ▶ return $list[0] if @list == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @list == 1 2 7 9
732 7 ▶ return $list[0] . ' and ' . $list[1] if @list == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @list == 2 7 0 7
733 0 return join (', ', @list[0 .. ($#list-1)]) . ', and ' . $list[-1];
734 }
735  
736  
737 =back
738  
739 =head1 MISCELLANEOUS FUNCTIONS
740  
741 In addition to loading the C<File> and C<Entry> modules, C<Text::BibTeX>
742 loads the XSUB code which bridges the Perl modules to the underlying C
743 library. This XSUB code provides a number of miscellaneous
744 utility functions, most of which are put into other packages in the
745 C<Text::BibTeX> family for use by the corresponding classes. (For
746 instance, the XSUB code loaded by C<Text::BibTeX> provides a function
747 C<Text::BibTeX::Entry::parse>, which is actually documented as the
748 C<parse> method of the C<Text::BibTeX::Entry> class---see
749 L<Text::BibTeX::Entry>. However, for completeness this function---and
750 all the other functions that become available when you C<use
751 Text::BibTeX>---are at least mentioned here. The only functions from
752 this group that you're ever likely to use are described in L<"Generic
753 string-processing functions">.
754  
755 =head2 Startup/shutdown functions
756  
757 =over 4
758  
759 =item initialize ()
760  
761 Does nothing; kept for compatibility.
762  
763 =item cleanup ()
764  
765 Deletes all macros from the global macro table.
766  
767 =item unicode_version ()
768  
769 Returns the version of the Unicode database the C library's character
770 tables were built from (that of the perl C<Text::BibTeX> was built with).
771  
772 =back
773  
774 =head2 Generic string-processing functions
775  
776 =over 4
777  
778 =item split_list (STRING, DELIM [, FILENAME [, LINE [, DESCRIPTION [, OPTS]]]])
779  
780 Splits a string on a fixed delimiter according to the BibTeX rules for
781 splitting up lists of names. With BibTeX, the delimiter is hard-coded
782 as C<"and">; here, you can supply any string. This is I<not> the same
783 as Perl's C<split> function: an instance of DELIM in STRING is only a
784 delimiter if
785  
786 =over 4
787  
788 =item *
789  
790 it is surrounded by whitespace (so C<"Anderson"> is not split);
791  
792 =item *
793  
794 it is not at the beginning or end of STRING (so C<"and"> on its own is
795 one item);
796  
797 =item *
798  
799 it is at brace-depth zero (so C<"{Barnes and Noble}"> is one item).
800  
801 =back
802  
803 The comparison is case-insensitive, so
804  
805 Candy and Apples AnD {Green Eggs and Ham}
806  
807 splits into C<"Candy">, C<"Apples">, and C<"{Green Eggs and Ham}">.
808 Empty items (as in C<"Smith and and Jones">) are returned as C<undef>,
809 with a warning. OPTS is a hash ref of the same binmode and
810 normalization arguments as with, e.g. Text::BibTeX::File->open(); the
811 default is C<'utf-8'> mode. FILENAME, LINE and DESCRIPTION (such as
812 C<"name">) are used in warnings.
813  
814 Returns the list of strings resulting from splitting STRING on DELIM.
815  
816 =item isplit_list (STRING, DELIM [, FILENAME [, LINE [, DESCRIPTION]]])
817  
818 Splits a string on a fixed delimiter in the same way as L</split_list>.
819 The strings returned are of the same kind as STRING: character
820 strings if it was a character string, byte strings otherwise. Use
821 C<split_list> to specify binmode and normalization arguments.
822  
823 Returns the list of strings resulting from splitting STRING on DELIM.
824  
825 =item purify_string (STRING [, OPTIONS])
826  
827 "Purifies" STRING in the BibTeX way (usually for generation of sort
828 keys). A purified copy of the input string is returned: a character
829 string if STRING was one, a byte string otherwise. OPTIONS is currently
830 unused.
831  
832 Purification keeps letters and digits (in any script, with any
833 combining marks attached to them) and spaces, converts hyphens and ties
834 (C<"~">) to spaces, and removes (almost) everything else.
835  
836 "Almost", because TeX "special characters" (used for accented and
837 non-English letters) are handled specially. A special character is a
838 brace group that starts at brace-depth zero and whose first character is
839 a backslash. For instance, the string
840  
841 {\foo bar}Herr M\"uller went from {P{\r r}erov} to {\AA}rhus
842  
843 contains two special characters: C<"{\foo bar}"> and C<"{\AA}">.
844 Neither the C<\"u> nor the C<\r r> are special characters, because they
845 are not at the right brace depth.
846  
847 In a special character, a control sequence that is one of LaTeX's
848 "foreign letters" (C<\oe>, C<\ae>, C<\aa>, C<\o>, C<\l>, C<\ss>, plus
849 uppercase versions) is converted to an English approximation by
850 stripping the backslash and lowercasing the second character (if any),
851 so C<{\AA}> becomes C<Aa>. All other control sequences in a special
852 character are removed, as are all non-alphanumeric characters.
853 Outside special characters, control sequences lose only their
854 backslash. So the string above becomes
855  
856 barHerr Muller went from Pr rerov to Aarhus
857  
858 Something has gone wrong with C<P{\r r}erov>: the accented `r' should be
859 a special character, starting at brace-depth zero. Written as
860  
861 {\foo bar}Herr M\"uller went from P{\r r}erov to {\AA}rhus
862  
863 the result is more sensible:
864  
865 barHerr Muller went from Prerov to Aarhus
866  
867 The "nonsense" special character C<{\foo bar}> is a common trick to put
868 text in a string solely for generating sort keys: TeX ignores it, as
869 long as C<\foo> is defined as a macro that does nothing.
870  
871 C<purify_string> is adequate for sorting by English-language
872 conventions. To follow the conventions of other languages, use a
873 collation library (such as L<Unicode::Collate>) on the purified string.
874  
875 =item change_case (TRANSFORM, STRING [, OPTIONS])
876  
877 Transforms the case of STRING according to TRANSFORM, which is one of
878 C<'u'> (uppercase), C<'l'> (lowercase), or C<'t'> ("title
879 capitalization", as for non-book titles). The transformed copy is
880 returned: a character string if STRING was one, a byte string otherwise.
881 An invalid TRANSFORM throws an exception. OPTIONS is currently unused.
882  
883 Case conversion uses the full Unicode case mappings, so the result may
884 be longer than the input (C<"ß"> uppercases to C<"SS">). Title
885 capitalization means that everything is converted to lowercase, except
886 the first letter of the first word and of words immediately following a
887 colon or sentence-ending punctuation, which are converted to I<titlecase>
888 (this only differs from uppercase for a few digraphs, such as C<"dž">).
889 For instance,
890  
891 Flying Squirrels: Their Peculiar Habits. Part One
892  
893 becomes
894  
895 Flying squirrels: Their peculiar habits. Part one
896  
897 Text in braces is treated differently. In a special character (see
898 L</purify_string>), control sequences that are LaTeX's non-English
899 letters are converted (C<\AE> becomes C<\ae> when lowercasing); other
900 control sequences, including accents, are preserved; and all other text,
901 at any depth, is converted to lowercase or uppercase (to lowercase for
902 title capitalization). Brace groups that are not special characters are
903 left completely untouched.
904  
905 For example, with title capitalization,
906  
907 A Guide to \LaTeXe: Document Preparation ...
908  
909 becomes
910  
911 A guide to \latexe: Document preparation ...
912  
913 which is probably not what you want. Writing C<{\LaTeXe}> keeps the
914 control sequence intact, but C<{\LaTeXe}> is then a special character
915 that is not a non-English letter, so C<purify_string> removes it, and a
916 sort key generated from the title would be C<"A Guide to Document
917 Preparation">. The solution (for any title with a TeX command that
918 produces actual text) is to bury the control sequence at brace-depth
919 two:
920  
921 A Guide to {{\LaTeXe}}: Document Preparation ...
922  
923 =back
924  
925 =head2 Entry-parsing functions
926  
927 Although these functions are provided by the C<Text::BibTeX> module,
928 they are actually in the C<Text::BibTeX::Entry> package. That's because
929 they are implemented in C, and thus loaded with the XSUB code that
930 C<Text::BibTeX> loads; however, they are actually methods in the
931 C<Text::BibTeX::Entry> class. Thus, they are documented as methods in
932 L<Text::BibTeX::Entry>.
933  
934 =over 4
935  
936 =item _parse (ENTRY_STRUCT, PARSER [, PRESERVE])
937  
938 =item _parse_s (ENTRY_STRUCT, TEXT [, PRESERVE [, UTF8]])
939  
940 PARSER is a C<Text::BibTeX::Parser> object, which wraps a C parser
941 reading from a Perl filehandle.
942  
943 =back
944  
945 =head2 Macro table functions
946  
947 These functions allow direct access to the global macro table. In the
948 normal course of events, macro definitions always accumulate, and are
949 only defined as a result of parsing a macro definition (C<@string>)
950 entry, so (as in BibTeX) macros defined in one file are available in the
951 files read after it. If, for example, you wish to start fresh with new
952 macros for every file, use C<delete_all_macros> (or the C<reset_macros>
953 option to L<Text::BibTeX::File/open>); to keep each file's macros to
954 itself, use the C<private_macros> option. Macro names are
955 case-insensitive. When C<Text::BibTeX> is loaded it defines the
956 standard three-letter month macros (C<jan> to C<dec>). If you wish
957 to pre-define certain macros, use C<add_macro_text>. (But note that the
958 C<Bib> structure, as part of its mission to emulate BibTeX 0.99, defines
959 the standard "month name" macros for you.)
960  
961 =over 4
962  
963 =item add_macro_text (MACRO, TEXT [, FILENAME [, LINE]])
964  
965 Defines a new macro, or redefines an old one. MACRO is the name of the
966 macro, and TEXT is the text it should expand to. FILENAME and LINE are
967 just used to generate any warnings about the macro definition. The only
968 such warning occurs when you redefine an old macro: its value is
969 overridden, and C<add_macro_text()> issues a warning saying so.
970  
971 =item delete_macro (MACRO)
972  
973 Deletes a macro from the macro table. If MACRO isn't defined,
974 takes no action.
975  
976 =item delete_all_macros ()
977  
978 Deletes all macros from the macro table, even the predefined month
979 names.
980  
981 =item macro_length (MACRO)
982  
983 Returns the length of a macro's expansion text. If the macro is
984 undefined, returns 0; no warning is issued.
985  
986 =item macro_text (MACRO [, FILENAME [, LINE]])
987  
988 Returns the expansion text of a macro, as a (UTF-8 encoded) byte string.
989 If the macro is not defined, issues a warning and returns C<undef>. FILENAME and LINE, if supplied,
990 are used for generating this warning; they should be supplied if you're
991 looking up the macro as a result of finding it in a file.
992  
993 =back
994  
995 =head2 Name-parsing functions
996  
997 These are both private functions for the use of the C<Name> class, and
998 therefore are put in the C<Text::BibTeX::Name> package. You should use
999 the interface provided by that class for parsing names in the BibTeX
1000 style.
1001  
1002 =over 4
1003  
1004 =item _split (NAME_STRUCT, NAME, FILENAME, LINE, NAME_NUM, KEEP_CSTRUCT)
1005  
1006 =item free (NAME_STRUCT)
1007  
1008 =back
1009  
1010 =head2 Name-formatting functions
1011  
1012 These are private functions for the use of the C<NameFormat> class, and
1013 therefore are put in the C<Text::BibTeX::NameFormat> package. You
1014 should use the interface provided by that class for formatting names in
1015 the BibTeX style.
1016  
1017 =over 4
1018  
1019 =item create ([PARTS [, ABBREV_FIRST]])
1020  
1021 =item free (FORMAT_STRUCT)
1022  
1023 =item _set_text (FORMAT_STRUCT, PART, PRE_PART, POST_PART, PRE_TOKEN, POST_TOKEN)
1024  
1025 =item _set_options (FORMAT_STRUCT, PART, ABBREV, JOIN_TOKENS, JOIN_PART)
1026  
1027 =item format_name (NAME_STRUCT, FORMAT_STRUCT [, UTF8])
1028  
1029 =back
1030  
1031 =head1 BUGS AND LIMITATIONS
1032  
1033 The parser reads ahead in its input as far as it needs to: don't read
1034 from a filehandle yourself while a C<Text::BibTeX::File> (or
1035 C<Text::BibTeX::Entry::parse>) is reading from it.
1036  
1037 Name splitting follows BibTeX's rules, which are designed for Western
1038 names.
1039  
1040 Any crash, memory error or leak in the C library should be considered a
1041 bug. It is tested with AddressSanitizer and UndefinedBehaviorSanitizer
1042 on randomly generated and mutated input (see F<fuzz/bt_fuzz.c> in the
1043 distribution).
1044  
1045 =head1 SEE ALSO
1046  
1047 L<btool_faq>, L<Text::BibTeX::File>, L<Text::BibTeX::Entry>,
1048 L<Text::BibTeX::Value>, L<Text::BibTeX::Error>, L<Text::BibTeX::Language>
1049  
1050 =head1 AUTHOR
1051  
1052 Greg Ward <gward@python.net>
1053  
1054 =head1 COPYRIGHT
1055  
1056 Copyright (c) 1997-2000 by Gregory P. Ward. All rights reserved. This file
1057 is part of the Text::BibTeX library. This library is free software; you
1058 may redistribute it and/or modify it under the same terms as Perl itself.
1059  
1060 =cut
1061  
1062 18 1;
« blib/lib/Text/BibTeX/Value.pm ↑ Summary blib/lib/Text/BibTeX/File.pm »