blib/lib/Text/BibTeX/Entry.pm

filter: Statementstmt 87.5% 225 / 257 Branchbran 67.2% 74 / 110 Conditioncond 65.2% 45 / 69 MC/DCmcdc 41.0% 16 / 39 Subroutinesub 95.4% 42 / 44 Podpod 96.6% 29 / 30 totaltotal 78.5% 431 / 549 CC 93 SCAR 22.1
CC
93
Cov
79%
warn
30.0
print_s
22.4
new
17.2
CRAP 9.1
« blib/lib/Text/BibTeX/File.pm ↑ Summary blib/lib/Text/BibTeX/Error.pm »
1 # ----------------------------------------------------------------------
2 # NAME : BibTeX/Entry.pm
3 # CLASSES : Text::BibTeX::Entry
4 # RELATIONS : base class for Text::BibTeX::StructuredEntry, and
5 # ultimately for all user-supplied structured entry classes
6 # DESCRIPTION: Provides an object-oriented interface to BibTeX entries.
7 # CREATED : March 1997, Greg Ward
8 # MODIFIED :
9 # VERSION : $Id$
10 # COPYRIGHT : Copyright (c) 1997-2000 by Gregory P. Ward. All rights
11 # reserved.
12 #
13 # This file is part of the Text::BibTeX library. This
14 # library is free software; you may redistribute it and/or
15 # modify it under the same terms as Perl itself.
16 # ----------------------------------------------------------------------
17 package Text::BibTeX::Entry;
18  
19 18 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 18 ▶use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 18 ▶use Symbol ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 18 ▶use Text::BibTeX qw(:metatypes :nodetypes);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 18 our $VERSION = '1.00';
28  
29 =head1 NAME
30  
31 Text::BibTeX::Entry - read and parse BibTeX files
32  
33 =head1 SYNOPSIS
34  
35 use Text::BibTeX::Entry;
36  
37 # ...assuming that $bibfile and $newbib are both objects of class
38 # Text::BibTeX::File, opened for reading and writing (respectively):
39  
40 # Entry creation/parsing methods:
41 $entry = Text::BibTeX::Entry->new();
42 $entry->read ($bibfile);
43 $entry->parse ($filename, $filehandle);
44 $entry->parse_s ($entry_text);
45  
46 # or:
47 $entry = Text::BibTeX::Entry->new( $bibfile );
48 $entry = Text::BibTeX::Entry->new( $filename, $filehandle );
49 $entry = Text::BibTeX::Entry->new( $entry_text );
50
51 # Entry query methods
52 warn "error in input" unless $entry->parse_ok;
53 $metatype = $entry->metatype;
54 $type = $entry->type;
55  
56 # if metatype is BTE_REGULAR or BTE_MACRODEF:
57 $key = $entry->key; # only for BTE_REGULAR metatype
58 $num_fields = $entry->num_fields;
59 @fieldlist = $entry->fieldlist;
60 $has_title = $entry->exists ('title');
61 $title = $entry->get ('title');
62 # or:
63 ($val1,$val2,...$valn) = $entry->get ($field1, $field2, ..., $fieldn);
64  
65 # if metatype is BTE_COMMENT or BTE_PREAMBLE:
66 $value = $entry->value;
67  
68 # Author name methods
69 @authors = $entry->split ('author');
70 ($first_author) = $entry->names ('author');
71  
72 # Entry modification methods
73 $entry->set_type ($new_type);
74 $entry->set_key ($new_key);
75 $entry->set ('title', $new_title);
76 # or:
77 $entry->set ($field1, $val1, $field2, $val2, ..., $fieldn, $valn);
78 $entry->delete (@fields);
79 $entry->set_fieldlist (\@fieldlist);
80  
81 # Entry output methods
82 $entry->write ($newbib);
83 $entry->print ($filehandle);
84 $entry_text = $entry->print_s;
85  
86 # Reset internal parser state:
87 $entry = Text::BibTeX::Entry->new();
88 $entry->parse ($filename, undef);
89 $entry->parse_s (undef);
90  
91 # or:
92 $entry = Text::BibTeX::Entry->new( $filename, undef );
93 $entry = Text::BibTeX::Entry->new( undef );
94  
95 # Miscellaneous methods
96 $entry->warn ($entry_warning);
97 # or:
98 $entry->warn ($field_warning, $field);
99 $entry->clone;
100  
101 =head1 DESCRIPTION
102  
103 C<Text::BibTeX::Entry> does all the real work of reading and parsing
104 BibTeX files. (Well, actually it just provides an object-oriented Perl
105 front-end to a C library that does all that. But that's not important
106 right now.)
107  
108 BibTeX entries can be read either from C<Text::BibTeX::File> objects (using
109 the C<read> method), or directly from a filehandle (using the C<parse>
110 method), or from a string (using C<parse_s>). The first is preferable,
111 since you don't have to worry about supplying the filename, and because of
112 the extra functionality provided by the C<Text::BibTeX::File> class.
113 Currently, this means that you may specify the I<database structure> to
114 which entries are expected to conform via the C<File> class. This lets you
115 ensure that entries follow the rules for required fields and mutually
116 constrained fields for a particular type of database, and also gives you
117 access to all the methods of the I<structured entry class> for this
118 database structure. See L<Text::BibTeX::Structure> for details on database
119 structures.
120  
121 Once you have the entry, you can query it or change it in a variety of
122 ways. The query methods are C<parse_ok>, C<type>, C<key>, C<num_fields>,
123 C<fieldlist>, C<exists>, and C<get>. Methods for changing the entry are
124 C<set_type>, C<set_key>, C<set_fieldlist>, C<delete>, and C<set>.
125  
126 Finally, you can output BibTeX entries, again either to an open
127 C<Text::BibTeX::File> object, a filehandle or a string. (A filehandle or
128 C<File> object must, of course, have been opened in write mode.) Output to
129 a C<File> object is done with the C<write> method, to a filehandle via
130 C<print>, and to a string with C<print_s>. Using the C<File> class is
131 recommended for future extensibility, although it currently doesn't offer
132 anything extra.
133  
134 =head1 METHODS
135  
136 =head2 Entry creation/parsing methods
137  
138 =over 4
139  
140 =item new ([OPTS ,] [SOURCE])
141  
142 Creates a new C<Text::BibTeX::Entry> object. If the SOURCE parameter is
143 supplied, it must be one of the following: a C<Text::BibTeX::File> (or
144 descendant class) object, a filename/filehandle pair, or a string. Calls
145 C<read> to read from a C<Text::BibTeX::File> object, C<parse> to read from
146 a filehandle, and C<parse_s> to read from a string.
147  
148 A filehandle can be specified as a GLOB reference, or as an
149 C<IO::Handle> (or descendants) object, or as a C<FileHandle> (or
150 descendants) object. (But there's really no point in using
151 C<FileHandle> objects, since C<Text::BibTeX> requires Perl 5.004, which
152 always includes the C<IO> modules.) You can I<not> pass in the name of
153 a filehandle as a string, though, because C<Text::BibTeX::Entry>
154 conforms to the C<use strict> pragma (which disallows such symbolic
155 references).
156  
157 The corresponding filename should be supplied in order to allow for
158 accurate error messages; if you simply don't have the filename, you can
159 pass C<undef> and you'll get error messages without a filename. (It's
160 probably better to rearrange your code so that the filename is
161 available, though.)
162  
163 Thus, the following are equivalent to read from a file named by
164 C<$filename> (error handling ignored):
165  
166 # good ol' fashioned filehandle and GLOB ref
167 open (BIBFILE, $filename);
168 $entry = Text::BibTeX::Entry->new($filename, \*BIBFILE);
169  
170 # newfangled IO::File thingy
171 $file = IO::File->new($filename);
172 $entry = Text::BibTeX::Entry->new($filename, $file);
173  
174 But using a C<Text::BibTeX::File> object is simpler and preferred:
175  
176 $file = Text::BibTeX::File->new($filename);
177 $entry = Text::BibTeX::Entry->new($file);
178  
179 Returns the new object, unless SOURCE is supplied and reading/parsing
180 the entry fails (e.g., due to end of file) -- then it returns false.
181  
182 You may supply a reference to an option hash as first argument.
183 Supported options are:
184  
185 =over 4
186  
187 =item BINMODE
188  
189 Set the way Text::BibTeX deals with strings: C<'utf-8'> (the default)
190 to get Perl character strings, or C<'bytes'> to get byte strings exactly
191 as they appear in the input. See L<Text::BibTeX/"STRINGS AND UNICODE">.
192 When reading from a C<Text::BibTeX::File>, the file's setting is used
193 unless you give one here.
194  
195 Text::BibTeX::Entry->new(
196 { binmode => 'utf-8', normalization => 'NFD' },
197 $file);
198  
199 =item NORMALIZATION
200  
201 The Unicode normalization form for strings in C<'utf-8'> mode: C<'NFC'>
202 (the default), C<'NFD'>, C<'NFKC'>, C<'NFKD'> or C<'none'>.
203  
204 =back
205  
206  
207 =cut
208  
209 sub new
210 185 ▶{
Subroutine new: called CC=16 SCAR=28.5
Pod documented
211 185 my ($class, @source) = @_;
212 185 ▶ $class = ref ($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
01850
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
213  
214 185 my $self = {'file' => undef,
215 'type' => undef,
216 'key' => undef,
217 'status' => undef,
218 'metatype' => undef,
219 'fields' => [],
220 'values' => {},
221 'errors' => []};
222 185 bless $self, $class;
223  
224 185 my $opts;
225 185 ▶ $opts = shift @source if scalar(@source) and ref $source[0] eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if scalar @source and ref $source[0] eq "HASH" 1 184 185
Condition100%3/3
scalar @source and ref $source[0] eq "HASH"
!ll&&!rl&&r
91751
MC/DC100%2/2
scalar @source and ref $source[0] eq "HASH"
scalar @source ref $source[0] eq "HASH"
Truth table100%3/3
A and B
A= scalar @sourceB= ref $source[0] eq "HASH"
ABresult
0X0
100
111
226 185 ▶ if ($opts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts) 1 184 185
227 1 @{$self}{qw(binmode normalization)} = Text::BibTeX::_options($opts);
228 }
229  
230 185 ▶ if (@source)
Branch: 1 branch
BranchTrueFalseTotal
if (@source) 175 10 185
231 {
232 175 my $status;
233  
234 175 ▶ if (@source == 1 && blessed($source[0]) && $source[0]->isa ('Text::BibTeX::File'))
Branch: 3 branches
BranchTrueFalseTotal
if (@source == 1 and builtin::blessed($source[0]) and $source[0]->isa('Text::BibTeX::File')) { } 144 31 175
elsif (@source == 2 and (defined $source[0] and not ref $source[0])) { } 9 22 31
elsif (@source == 1 and not ref $source[0]) { } 22 0 22
Condition83%5/6
@source == 1 and builtin::blessed($source[0]) and $source[0]->isa("Text::BibTeX::File")
!ll&&!rl&&r
310144
@source == 1 and builtin::blessed($source[0])
!ll&&!rl&&r
922144
MC/DC66%2/3
@source == 1 and builtin::blessed($source[0]) and $source[0]->isa("Text::BibTeX::File")
@source == 1 builtin::blessed($source[0]) $source[0]->isa("Text::BibTeX::File")
Truth table75%3/4
A and B and C
A= @source == 1B= builtin::blessed($source[0])C= $source[0]->isa("Text::BibTeX::File")
ABCresult
0XX0
10X0
1100
1111
235 {
236 144 my $file = $source[0];
237 144 $status = $self->read ($file);
238 143 ▶ if (my $structure = $file->structure)
Branch: 1 branch
BranchTrueFalseTotal
if (my $structure = $file->structure) 2 141 143
239 {
240 2 $self->{structure} = $structure;
241 2 bless $self, $structure->entry_class;
242 }
243 }
244 ▶ elsif (@source == 2 && (defined ($source[0]) && ! ref ($source[0])))
Condition50%3/6
@source == 2 and (defined $source[0] and not ref $source[0])
!ll&&!rl&&r
2209
defined $source[0] and not ref $source[0]
!ll&&!rl&&r
009
MC/DC33%1/3
@source == 2 and (defined $source[0] and not ref $source[0])
@source == 2 defined $source[0] not ref $source[0]
Truth table50%2/4
A and B and not(C)
A= @source == 2B= defined $source[0]C= not ref $source[0]
ABCresult
0XX0
10X0
1100
1111
245 9 { $status = $self->parse ($source[0], $source[1]) }
246 ▶ elsif (@source == 1 && ! ref ($source[0]))
Condition33%1/3
@source == 1 and not ref $source[0]
!ll&&!rl&&r
0022
MC/DC0%0/2
@source == 1 and not ref $source[0]
@source == 1 not ref $source[0]
Truth table33%1/3
A and not(B)
A= @source == 1B= not ref $source[0]
ABresult
0X0
100
111
247 22 { $status = $self->parse_s ($source[0]) }
248 else
249 0 { croak "new: source argument must be either a Text::BibTeX::File " .
250 "(or descendant) object, filename/filehandle pair, or " .
251 "a string"; }
252  
253 173 ▶ return $status unless $status; # parse failed -- tell our caller
Branch: 1 branch
BranchTrueFalseTotal
unless $status 25 148 173
254 }
255 158 $self;
256 }
257  
258 # String conversion options for this entry
259 656 ▶sub _binmode { $_[0]{binmode} // 'utf-8' }
Subroutine _binmode: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]{"binmode"} // "utf-8"
l!l
334322
MC/DC100%1/1
$_[0]{"binmode"} // "utf-8"
$_[0]{"binmode"}
Truth table100%2/2
A
A= $_[0]{"binmode"}
Aresult
11
00
260 575 ▶sub _norm { $_[0]{normalization} // 'NFC' }
Subroutine _norm: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]{"normalization"} // "NFC"
l!l
332243
MC/DC100%1/1
$_[0]{"normalization"} // "NFC"
$_[0]{"normalization"}
Truth table100%2/2
A
A= $_[0]{"normalization"}
Aresult
11
00
261 352 ▶sub _in { Text::BibTeX::_in($_[0]->_binmode, $_[0]->_norm, $_[1]) }
Subroutine _in: called CC=1 SCAR=0.0
262  
263 =item clone
264  
265 Clone a Text::BibTeX::Entry object, returning the clone. This re-uses the reference to any
266 Text::BibTeX::Structure or Text::BibTeX::File but copies everything else,
267 so that the clone can be modified apart from the original.
268  
269 =cut
270  
271 sub clone
272 1 ▶{
Subroutine clone: called CC=6 SCAR=18.5
Pod documented
273 1 my $self = shift;
274 1 my $clone = {};
275  
276 # Use the same structure object - won't be changed
277 1 ▶ if ($self->{structure}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"structure"}) 0 1 1
278 0 $clone->{structure} = $self->{structure};
279 }
280  
281 # Use the same file object - won't be changed
282 1 ▶ if ($self->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"file"}) 0 1 1
283 $clone->{file} = $self->{file}
284 0 }
285  
286 # These might be changed so make copies
287 1 $clone->{binmode} = $self->{binmode};
288 1 $clone->{normalization} = $self->{normalization};
289 1 $clone->{type} = $self->{type};
290 1 $clone->{key} = $self->{key};
291 1 $clone->{status} = $self->{status};
292 1 $clone->{metatype} = $self->{metatype};
293 1 ▶ $clone->{value} = $self->{value} if exists $self->{value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"value"} 0 1 1
294 1 $clone->{fields} = [ @{$self->{fields}} ];
295 1 $clone->{values} = { %{$self->{values}} };
296 1 ▶ $clone->{lines} = { %{$self->{lines} || {}} };
Condition50%1/2
$self->{'lines'} || {}
l!l
10
MC/DC0%0/1
$self->{'lines'} || {}
$self->{'lines'}
Truth table50%1/2
A
A= $self->{'lines'}
Aresult
11
00
297 1 ▶ $clone->{errors} = [ @{$self->{errors} || []} ];
Condition50%1/2
$self->{'errors'} || []
l!l
01
MC/DC0%0/1
$self->{'errors'} || []
$self->{'errors'}
Truth table50%1/2
A
A= $self->{'errors'}
Aresult
11
00
298  
299 1 bless $clone, ref($self);
300 1 return $clone;
301 }
302  
303 =item read (BIBFILE)
304  
305 Reads and parses an entry from BIBFILE, which must be a
306 C<Text::BibTeX::File> object (or descendant). The next entry will be read
307 from the file associated with that object.
308  
309 Returns the same as C<parse> (or C<parse_s>): false if no entry found
310 (e.g., at end-of-file), true otherwise. To see if the parse itself failed
311 (due to errors in the input), call the C<parse_ok> method.
312  
313 =cut
314  
315 sub read
316 148 ▶{
Subroutine read: called CC=6 SCAR=18.8
Pod documented
317 148 my ($self, $source, $preserve) = @_;
318 148 ▶ croak "`source' argument must be ref to open Text::BibTeX::File " .
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($source) and $source->isa("Text::BibTeX::File") 0 148 148
Condition33%1/3
builtin::blessed($source) and $source->isa("Text::BibTeX::File")
!ll&&!rl&&r
00148
MC/DC0%0/2
builtin::blessed($source) and $source->isa("Text::BibTeX::File")
builtin::blessed($source) $source->isa("Text::BibTeX::File")
Truth table33%1/3
A and B
A= builtin::blessed($source)B= $source->isa("Text::BibTeX::File")
ABresult
0X0
100
111
319 "(or descendant) object"
320 unless (blessed($source) && $source->isa('Text::BibTeX::File'));
321  
322 148 $self->{'file'} = $source; # store File object for later use
323 ## Propagate flags
324 148 for my $f (qw.binmode normalization.) {
325 296 ▶ $self->{$f} = $source->{$f} unless defined $self->{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$f} 288 8 296
326 }
327 148 ▶ $preserve = $self->_preserve ($preserve) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_preserve($preserve) ? : 0 148 148
328 148 return $self->_finish(_parse ($self, $source->_parser, $preserve));
329 }
330  
331  
332 =item parse (FILENAME, FILEHANDLE)
333  
334 Reads and parses the next entry from FILEHANDLE. (That is, it scans the
335 input until an '@' sign is seen, and then slurps up to the next '@'
336 sign. Everything between the two '@' signs [including the first one,
337 but not the second one -- it's pushed back onto the input stream for the
338 next entry] is parsed as a BibTeX entry, with the simultaneous
339 construction of an abstract syntax tree [AST]. The AST is traversed to
340 ferret out the most interesting information, and this is stuffed into a
341 Perl hash, which coincidentally is the C<Text::BibTeX::Entry> object
342 you've been tossing around. But you don't need to know any of that -- I
343 just figured if you've read this far, you might want to know something
344 about the inner workings of this module.)
345  
346 The success of the parse is stored internally so that you can later
347 query it with the C<parse_ok> method. Even in the presence of syntax
348 errors, you'll usually get something resembling your input, but it's
349 usually not wise to try to do anything with it. Just call C<parse_ok>,
350 and if it returns false then silently skip to the next entry. The error
351 messages should be quite adequate for the user to figure out what's
352 wrong; they are reported with Perl's C<warn> (so they can be caught or
353 redirected like any other warning), and are also available from the
354 C<errors> method. See L<Text::BibTeX::Error>.
355  
356 If no '@' signs are seen on the input before reaching end-of-file, then
357 we've exhausted all the entries in the file, and C<parse> returns a
358 false value. Otherwise, it returns a true value -- even if there were
359 syntax errors. Hence, it's important to check C<parse_ok>.
360  
361 The FILENAME parameter is only used for generating error messages, but
362 anybody using your program will certainly appreciate your setting it
363 correctly!
364  
365 FILEHANDLE may be any Perl filehandle, including in-memory and tied
366 handles. The parser reads ahead in the file, so don't read from the
367 handle yourself in between calls. Passing C<undef> as FILEHANDLE
368 forgets about all the filehandles C<parse> has been reading from. (It
369 is usually simpler to use a C<Text::BibTeX::File> object and the C<read>
370 method.)
371  
372 =item parse_s (TEXT)
373  
374 Parses a BibTeX entry (using the above rules) from the string TEXT. The
375 string is not modified; repeatedly calling C<parse_s> with the same string
376 will give you the same results each time. Thus, there's no point in
377 putting multiple entries in one string.
378  
379 Passing C<undef> to TEXT will reset the state of the underlying
380 C parser, which may be required in order to parse multiple strings.
381  
382 =back
383  
384 =cut
385  
386 sub _preserve
387 198 ▶{
Subroutine _preserve: called CC=5 SCAR=16.1
388 198 my ($self, $preserve) = @_;
389  
390 $preserve = $self->{'file'}->preserve_values
391 if ! defined $preserve &&
392 defined $self->{'file'} &&
393 198 ▶ $self->{'file'}->isa ('Text::BibTeX::File');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $preserve and defined $self->{"file"} and $self->{"file"}->isa("Text::BibTeX::File") 148 50 198
Condition83%5/6
not defined $preserve and defined $self->{"file"} and $self->{"file"}->isa("Text::BibTeX::File")
!ll&&!rl&&r
500148
not defined $preserve and defined $self->{"file"}
!ll&&!rl&&r
149148
MC/DC66%2/3
not defined $preserve and defined $self->{"file"} and $self->{"file"}->isa("Text::BibTeX::File")
not defined $preserve defined $self->{"file"} $self->{"file"}->isa("Text::BibTeX::File")
Truth table75%3/4
not(A) and B and C
A= not defined $preserveB= defined $self->{"file"}C= $self->{"file"}->isa("Text::BibTeX::File")
ABCresult
0XX0
10X0
1100
1111
394 198 ▶ require Text::BibTeX::Value if $preserve;
Branch: 1 branch
BranchTrueFalseTotal
if $preserve 1 197 198
395 198 $preserve;
396 }
397  
398 # Parsers for raw filehandles passed to parse(), keyed on the handle
399 18 my %Parsers;
400  
401 sub parse
402 12 ▶{
Subroutine parse: called CC=7 SCAR=21.5
Pod documented
403 12 my ($self, $filename, $filehandle, $preserve) = @_;
404  
405 12 ▶ if (!defined $filehandle) { # reset (forget) all parsers
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filehandle) 0 12 12
406 0 %Parsers = ();
407 0 return;
408 }
409  
410 0 ▶ my $handle = ref $filehandle ? $filehandle : \*{ Symbol::qualify($filehandle, caller) };
Branch: 1 branch
BranchTrueFalseTotal
ref $filehandle ? : 12 0 12
411 12 ▶ $preserve = $self->_preserve ($preserve) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_preserve($preserve) ? : 0 12 12
412 12 my $id = refaddr $handle;
413 12 ▶ my $parser = $Parsers{$id} //= Text::BibTeX::Parser->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->_binmode eq 'utf-8' ? : 5 0 5
Condition66%2/3
$Parsers{$id} //= "Text::BibTeX::Parser"->new($handle, $filename, $self->_binmode eq "utf-8" ? 1 : 0, 0)
l!l&&r!l&&!r
750
MC/DC0%0/2
$Parsers{$id} //= "Text::BibTeX::Parser"->new($handle, $filename, $self->_binmode eq "utf-8" ? 1 : 0, 0)
$Parsers{$id} "Text::BibTeX::Parser"->new($handle, $filename, $self->_binmode eq "utf-8" ? 1 : 0, 0)
Truth table66%2/3
A //= B
A= $Parsers{$id}B= "Text::BibTeX::Parser"->new($handle, $filename, $self->_binmode eq "utf-8" ? 1 : 0, 0)
ABresult
1X1
011
000
414 $handle, $filename, $self->_binmode eq 'utf-8' ? 1 : 0, 0);
415 12 my $found = _parse ($self, $parser, $preserve);
416 12 ▶ delete $Parsers{$id} unless $found; # at end of file
Branch: 1 branch
BranchTrueFalseTotal
unless $found 5 7 12
417 12 return $self->_finish($found);
418 }
419  
420  
421 sub parse_s
422 40 ▶{
Subroutine parse_s: called CC=8 SCAR=20.9
Pod documented
423 40 my ($self, $text, $preserve) = @_;
424  
425 40 ▶ return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 2 38 40
426 38 ▶ $preserve = $self->_preserve ($preserve) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_preserve($preserve) ? : 1 37 38
427 38 ▶ if ($self->_binmode eq 'utf-8') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_binmode eq 'utf-8') { } 38 0 38
428 38 ▶ if (!utf8::is_utf8($text) && $text =~ /[^\x00-\x7f]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not utf8::is_utf8($text) and $text =~ /[^\x00-\x7f]/u) 3 35 38
Condition100%3/3
not utf8::is_utf8($text) and $text =~ /[^\x00-\x7f]/u
!ll&&!rl&&r
10253
MC/DC100%2/2
not utf8::is_utf8($text) and $text =~ /[^\x00-\x7f]/u
not utf8::is_utf8($text) $text =~ /[^\x00-\x7f]/u
Truth table100%3/3
not(A) and B
A= not utf8::is_utf8($text)B= $text =~ /[^\x00-\x7f]/u
ABresult
0X0
100
111
429 3 my $c = $text;
430 3 ▶ $text = $c if utf8::decode($c);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::decode($c) 2 1 3
431 }
432 }
433 else {
434 0 $text = Text::BibTeX::_to_c('bytes', 'none', $text);
435 }
436 38 ▶ return $self->_finish(_parse_s ($self, $text, $preserve,
Branch: 1 branch
BranchTrueFalseTotal
$self->_binmode eq 'utf-8' ? : 38 0 38
437 $self->_binmode eq 'utf-8' ? 1 : 0));
438 }
439  
440 # After parsing: put the entry's strings into normal form
441 sub _finish
442 196 ▶{
Subroutine _finish: called CC=11 SCAR=24.0
443 196 my ($self, $found) = @_;
444 196 ▶ return $found unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 30 166 196
445 166 my $norm = $self->_norm;
446 166 ▶ return $found if $self->_binmode ne 'utf-8' || $norm eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_binmode ne "utf-8" or $norm eq "none" 5 161 166
Condition66%2/3
$self->_binmode ne "utf-8" or $norm eq "none"
l!l&&r!l&&!r
50161
MC/DC50%1/2
$self->_binmode ne "utf-8" or $norm eq "none"
$self->_binmode ne "utf-8" $norm eq "none"
Truth table66%2/3
A or B
A= $self->_binmode ne "utf-8"B= $norm eq "none"
ABresult
1X1
011
000
447  
448 161 ▶ my $n = sub { Text::BibTeX::_normalise($norm, $_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
449 161 for my $k (qw(type key)) {
450 322 ▶ $self->{$k} = $n->($self->{$k}) if defined $self->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$k} 299 23 322
451 }
452 685 ▶ my $fix_value = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
453 685 my $v = shift;
454 685 ▶ return $n->($v) unless ref $v;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v 684 1 685
455 1 $_->[1] = $n->($_->[1]) for $v->values;
456 1 return $v;
457 161 };
458 161 ▶ if (exists $self->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"value"}) 7 154 161
459 7 $self->{value} = $fix_value->($self->{value});
460 }
461 161 ▶ if (grep { /[^\x00-\x7f]/ } @{$self->{fields}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/[^\x00-\x7f]/u;} @{$$self{"fields"};}) 4 157 161
462 4 my (%values, %lines);
463 4 for my $f (@{$self->{fields}}) {
464 7 my $nf = $n->($f);
465 7 $values{$nf} = $self->{values}{$f};
466 7 $lines{$nf} = $self->{lines}{$f};
467 7 $f = $nf;
468 }
469 4 $lines{$_} = $self->{lines}{$_} for qw(START STOP);
470 4 $self->{values} = \%values;
471 4 $self->{lines} = \%lines;
472 }
473 161 $_ = $fix_value->($_) for values %{$self->{values}};
474 161 return $found;
475 }
476  
477  
478 =head2 Entry query methods
479  
480 =over 4
481  
482 =item parse_ok ()
483  
484 Returns false if there were any serious errors encountered while parsing
485 the entry. (A "serious" error is a lexical or syntax error; warnings,
486 such as "undefined macro", don't count.)
487  
488 =item errors ()
489  
490 Returns the list of L<Text::BibTeX::Error> objects for all the problems
491 -- serious or not -- found while the entry was parsed (including any
492 "junk" found before it in the file).
493  
494 =item type ()
495  
496 Returns the type of the entry. (The `type' is the word that follows the
497 '@' sign; e.g. `article', `book', `inproceedings', etc. for the standard
498 BibTeX styles.)
499  
500 =item metatype ()
501  
502 Returns the metatype of the entry. (The `metatype' is a numeric value used
503 to classify entry types into four groups: comment, preamble, macro
504 definition (C<@string> entries), and regular (all other entry types).
505 C<Text::BibTeX> exports four constants for these metatypes: C<BTE_COMMENT>,
506 C<BTE_PREAMBLE>, C<BTE_MACRODEF>, and C<BTE_REGULAR>.)
507  
508 =item key ()
509  
510 Returns the key of the entry. (The key is the token immediately
511 following the opening `{' or `(' in "regular" entries. Returns C<undef>
512 for entries that don't have a key, such as macro definition (C<@string>)
513 entries.)
514  
515 =item num_fields ()
516  
517 Returns the number of fields in the entry. (Note that, currently, this is
518 I<not> equivalent to putting C<scalar> in front of a call to C<fieldlist>.
519 See below for the consequences of calling C<fieldlist> in a scalar
520 context.)
521  
522 =item fieldlist ()
523  
524 Returns the list of fields in the entry.
525  
526 B<WARNING> In scalar context, it no longer returns a
527 reference to the object's own list of fields.
528  
529 =cut
530  
531 22 ▶sub parse_ok { shift->{'status'}; }
Subroutine parse_ok: called CC=1 SCAR=0.0
Pod documented
532  
533 35 ▶sub metatype { shift->{'metatype'} }
Subroutine metatype: called CC=1 SCAR=0.0
Pod documented
534  
535 35 ▶sub type { shift->{'type'} }
Subroutine type: called CC=1 SCAR=0.0
Pod documented
536  
537 48 ▶sub key { shift->{'key'} }
Subroutine key: called CC=1 SCAR=0.0
Pod documented
538  
539 0 ▶sub num_fields { scalar @{shift->{'fields'}}; }
Subroutine num_fields: not called CC=1 SCAR=6.9
Pod documented
540  
541 23 ▶sub fieldlist { @{shift->{'fields'}} }
Subroutine fieldlist: called CC=1 SCAR=0.0
Pod documented
542
543 =item exists (FIELD)
544  
545 Returns true if a field named FIELD is present in the entry, false
546 otherwise.
547  
548 =item get (FIELD, ...)
549  
550 Returns the value of one or more FIELDs, as a list of values. For example:
551  
552 $author = $entry->get ('author');
553 ($author, $editor) = $entry->get ('author', 'editor');
554  
555 If a FIELD is not present in the entry, C<undef> will be returned at its
556 place in the return list. However, you can't completely trust this as a
557 test for presence or absence of a field; it is possible for a field to be
558 present but undefined. Currently this can only happen due to certain
559 syntax errors in the input, or if you pass an undefined value to C<set>, or
560 if you create a new field with C<set_fieldlist> (the new field's value is
561 implicitly set to C<undef>).
562  
563 Normally, the field value is what the input looks like after "maximal
564 processing"--quote characters are removed, whitespace is collapsed (the
565 same way that BibTeX itself does it), macros are expanded, and multiple
566 tokens are pasted together. (See L<Text::BibTeX::Value/POST-PROCESSING>
567 for details.)
568  
569 For example, if your input file has the following:
570  
571 @string{of = "of"}
572 @string{foobars = "Foobars"}
573  
574 @article{foobar,
575 title = { The Mating Habits } # of # " Adult " # foobars
576 }
577  
578 then using C<get> to query the value of the C<title> field from the
579 C<foobar> entry would give the string "The Mating Habits of Adult Foobars".
580  
581 However, in certain circumstances you may wish to preserve the values as
582 they appear in the input. This is done by setting a C<preserve_values>
583 flag at some point; then, C<get> will return not strings but
584 C<Text::BibTeX::Value> objects. Each C<Value> object is a list of
585 C<Text::BibTeX::SimpleValue> objects, which in turn consists of a simple
586 value type (string, macro, or number) and the text of the simple value.
587 Various ways to set the C<preserve_values> flag and the interface to
588 both C<Value> and C<SimpleValue> objects are described in
589 L<Text::BibTeX::Value>.
590  
591 =item value ()
592  
593 Returns the single string associated with C<@comment> and C<@preamble>
594 entries. For instance, the entry
595  
596 @preamble{" This is a preamble" #
597 {---the concatenation of several strings}}
598  
599 would return a value of "This is a preamble---the concatenation of
600 several strings".
601  
602 If this entry was parsed in "value preservation" mode, then C<value>
603 acts like C<get>, and returns a C<Value> object rather than a simple
604 string.
605  
606 =back
607  
608 =cut
609  
610 sub exists
611 96 ▶{
Subroutine exists: called CC=1 SCAR=0.0
Pod documented
612 96 my ($self, $field) = @_;
613 96 exists $self->{values}{$self->_in($field)};
614 }
615  
616 sub get
617 142 ▶{
Subroutine get: called CC=2 SCAR=6.9
Pod documented
618 142 my ($self, @fields) = @_;
619 142 my @x = @{$self->{'values'}}{map { $self->_in($_) } @fields};
620 142 ▶ return (@x > 1) ? @x : $x[0];
Branch: 1 branch
BranchTrueFalseTotal
@x > 1 ? : 30 112 142
621 }
622  
623 3 ▶sub value { shift->{'value'} }
Subroutine value: called CC=1 SCAR=0.0
Pod documented
624  
625 5 ▶sub errors { @{shift->{'errors'} || []} }
Subroutine errors: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
+(shift())->{'errors'} || []
l!l
41
MC/DC100%1/1
+(shift())->{'errors'} || []
+(shift())->{'errors'}
Truth table100%2/2
A
A= +(shift())->{'errors'}
Aresult
11
00
626  
627  
628 =head2 Author name methods
629  
630 This is the only part of the module that makes any assumption about the
631 nature of the data, namely that certain fields are lists delimited by a
632 simple word such as "and", and that the delimited sub-strings are human
633 names of the "First von Last" or "von Last, Jr., First" style used by
634 BibTeX. If you are using this module for anything other than
635 bibliographic data, you can most likely forget about these two methods.
636 However, if you are in fact hacking on BibTeX-style bibliographic data,
637 these could come in very handy -- the name-parsing done by BibTeX is not
638 trivial, and the list-splitting would also be a pain to implement in
639 Perl because you have to pay attention to brace-depth. (Not that it
640 wasn't a pain to implement in C -- it's just a lot more efficient than a
641 Perl implementation would be.)
642  
643 Incidentally, both of these methods assume that the strings being split
644 have already been "collapsed" in the BibTeX way, i.e. all leading and
645 trailing whitespace removed and internal whitespace reduced to single
646 spaces. This should always be the case when using these two methods on
647 a C<Text::BibTeX::Entry> object, but these are actually just front ends
648 to more general functions in C<Text::BibTeX>. (More general in that you
649 supply the string to be parsed, rather than supplying the name of an
650 entry field.) Should you ever use those more general functions
651 directly, you might have to worry about collapsing whitespace; see
652 L<Text::BibTeX> (the C<split_list> and C<split_name> functions in
653 particular) for more information.
654  
655 Please note that the interface to author name parsing is experimental,
656 subject to change, and open to discussion. Please let me know if you
657 have problems with it, think it's just perfect, or whatever.
658  
659 =over 4
660  
661 =item split (FIELD [, DELIM [, DESC]])
662  
663 Splits the value of FIELD on DELIM (default: `and'). Don't assume that
664 this works the same as Perl's builtin C<split> just because the names are
665 the same: in particular, DELIM must be a simple string (no regexps), and
666 delimiters that are at the beginning or end of the string, or at non-zero
667 brace depth, or not surrounded by whitespace, are ignored. Some examples
668 might illuminate matters:
669  
670 if field F is... then split (F) returns...
671 'Name1 and Name2' ('Name1', 'Name2')
672 'Name1 and and Name2' ('Name1', undef, 'Name2')
673 'Name1 and' ('Name1 and')
674 'and Name2' ('and Name2')
675 'Name1 {and} Name2 and Name3' ('Name1 {and} Name2', 'Name3')
676 '{Name1 and Name2} and Name3' ('{Name1 and Name2}', 'Name3')
677  
678 Note that a warning will be issued for empty names (as in the second
679 example above). A warning ought to be issued for delimiters at the
680 beginning or end of a string, but currently this isn't done. (Hmmm.)
681  
682 DESC is a one-word description of the substrings; it defaults to 'name'.
683 It is only used for generating warning messages.
684  
685 =item names (FIELD)
686  
687 Splits FIELD as described above, and further splits each name into four
688 components: first, von, last, and jr.
689  
690 Returns a list of C<Text::BibTeX::Name> objects, each of which represents
691 one name. Use the C<part> method to query these objects; see
692 L<Text::BibTeX::Name> for details on the interface to name objects (and on
693 name-parsing as well).
694  
695 For example if this entry:
696  
697 @article{foo,
698 author = {John Smith and
699 Hacker, J. Random and
700 Ludwig van Beethoven and
701 {Foo, Bar and Company}}}
702  
703 has been parsed into a C<Text::BibTeX::Entry> object C<$entry>, then
704  
705 @names = $entry->names ('author');
706  
707 will put a list of C<Text::BibTeX::Name> objects in C<@names>. These can
708 be queried individually as described in L<Text::BibTeX::Name>; for instance,
709  
710 @last = $names[0]->part ('last');
711  
712 would put the list of tokens comprising the last name of the first author
713 into the C<@last> array: C<('Smith')>.
714  
715 =cut
716  
717 sub split
718 23 ▶{
Subroutine split: called CC=5 SCAR=17.0
Pod documented
719 23 my ($self, $field, $delim, $desc) = @_;
720  
721 23 ▶ return unless $self->exists($field);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists($field) 0 23 23
722 23 $field = $self->_in($field);
723 23 ▶ $delim ||= 'and';
Condition50%1/2
$delim ||= "and"
l!l
023
MC/DC0%0/1
$delim ||= "and"
$delim
Truth table50%1/2
A
A= $delim
Aresult
11
00
724 23 ▶ $desc ||= 'name';
Condition50%1/2
$desc ||= "name"
l!l
023
MC/DC0%0/1
$desc ||= "name"
$desc
Truth table50%1/2
A
A= $desc
Aresult
11
00
725  
726 Text::BibTeX::split_list($self->{values}{$field},
727 $delim,
728 ($self->{file} && $self->{file}{filename}),
729 23 ▶ $self->{lines}{$field},
Condition66%2/3
$self->{'file'} && $self->{'file'}{'filename'}
!ll&&!rl&&r
9014
MC/DC50%1/2
$self->{'file'} && $self->{'file'}{'filename'}
$self->{'file'} $self->{'file'}{'filename'}
Truth table66%2/3
A && B
A= $self->{'file'}B= $self->{'file'}{'filename'}
ABresult
0X0
100
111
730 $desc,
731 {binmode => $self->_binmode,
732 normalization => $self->_norm});
733 }
734  
735 sub names
736 17 ▶{
Subroutine names: called CC=4 SCAR=13.9
Pod documented
737 17 require Text::BibTeX::Name;
738  
739 17 my ($self, $field) = @_;
740 17 my (@names, $i);
741  
742 17 ▶ my $filename = ($self->{'file'} && $self->{'file'}{'filename'});
Condition66%2/3
$self->{'file'} && $self->{'file'}{'filename'}
!ll&&!rl&&r
7010
MC/DC50%1/2
$self->{'file'} && $self->{'file'}{'filename'}
$self->{'file'} $self->{'file'}{'filename'}
Truth table66%2/3
A && B
A= $self->{'file'}B= $self->{'file'}{'filename'}
ABresult
0X0
100
111
743 17 my $line = $self->{'lines'}{$self->_in($field)};
744  
745 17 @names = $self->split ($field);
746 17 for $i (0 .. $#names)
747 {
748 # an empty name (eg. from "and and") becomes an empty Name object
749 34 ▶ $names[$i] = Text::BibTeX::Name->new(
Condition100%2/2
$names[$i] // ""
l!l
331
MC/DC100%1/1
$names[$i] // ""
$names[$i]
Truth table100%2/2
A
A= $names[$i]
Aresult
11
00
750 {binmode => $self->_binmode, normalization => $self->_norm},
751 $names[$i] // '', $filename, $line, $i + 1);
752 }
753 17 @names;
754 }
755  
756 =back
757  
758 =head2 Entry modification methods
759  
760 =over 4
761  
762 =item set_type (TYPE)
763  
764 Sets the entry's type.
765  
766 =item set_metatype (METATYPE)
767  
768 Sets the entry's metatype (must be one of the four constants
769 C<BTE_COMMENT>, C<BTE_PREAMBLE>, C<BTE_MACRODEF>, and C<BTE_REGULAR>, which
770 are all optionally exported from C<Text::BibTeX>).
771  
772 =item set_key (KEY)
773  
774 Sets the entry's key.
775  
776 =item set (FIELD, VALUE, ...)
777  
778 Sets the value of field FIELD. (VALUE might be C<undef> or unsupplied,
779 in which case FIELD will simply be set to C<undef> -- this is where the
780 difference between the C<exists> method and testing the definedness of
781 field values becomes clear.)
782  
783 Multiple (FIELD, VALUE) pairs may be supplied; they will be processed in
784 order (i.e. the input is treated like a list, not a hash). For example:
785  
786 $entry->set ('author', $author);
787 $entry->set ('author', $author, 'editor', $editor);
788  
789 VALUE can be either a simple string or a C<Text::BibTeX::Value> object;
790 it doesn't matter if the entry was parsed in "full post-processing" or
791 "preserve input values" mode.
792  
793 =item delete (FIELD)
794  
795 Deletes field FIELD from an entry.
796  
797 =item set_fieldlist (FIELDLIST)
798  
799 Sets the entry's list of fields to FIELDLIST, which must be a list
800 reference. If any of the field names supplied in FIELDLIST are not
801 currently present in the entry, they are created with the value C<undef>
802 and a warning is printed. Conversely, if any of the fields currently
803 present in the entry are not named in the list of fields supplied to
804 C<set_fields>, they are deleted from the entry and another warning is
805 printed.
806  
807 =back
808  
809 =cut
810  
811 sub set_type
812 2 ▶{
Subroutine set_type: called CC=1 SCAR=0.0
Pod documented
813 2 my ($self, $type) = @_;
814  
815 2 $self->{'type'} = $self->_in($type);
816 }
817  
818 sub set_metatype
819 1 ▶{
Subroutine set_metatype: called CC=1 SCAR=0.0
Pod documented
820 1 my ($self, $metatype) = @_;
821  
822 1 $self->{'metatype'} = $metatype;
823 }
824  
825 sub set_key
826 2 ▶{
Subroutine set_key: called CC=1 SCAR=0.0
Pod documented
827 2 my ($self, $key) = @_;
828  
829 2 $self->{'key'} = $self->_in($key);
830 }
831  
832 sub set
833 4 ▶{
Subroutine set: called CC=4 SCAR=13.9
Pod documented
834 4 my $self = shift;
835 4 ▶ croak "set: must supply an even number of arguments"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 4 4
836 unless (@_ % 2 == 0);
837 4 my ($field, $value);
838  
839 4 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 4 9
840 {
841 5 ($field,$value) = ($self->_in(shift), $self->_in(shift));
842 1 push (@{$self->{'fields'}}, $field)
843 5 ▶ unless exists $self->{'values'}{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"values"}{$field} 1 4 5
844 5 $self->{'values'}{$field} = $value;
845 }
846 }
847  
848 sub delete
849 2 ▶{
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
850 2 my ($self, @fields) = @_;
851 2 my (%gone);
852  
853 2 @fields = map { $self->_in($_) } @fields;
854 2 %gone = map {$_, 1} @fields;
855 2 @{$self->{'fields'}} = grep (! $gone{$_}, @{$self->{'fields'}});
856 2 delete @{$self->{'values'}}{@fields};
857 }
858  
859 sub set_fieldlist
860 1 ▶{
Subroutine set_fieldlist: called CC=5 SCAR=16.4
Pod documented
861 1 my ($self, $fields) = @_;
862  
863 # Warn if any of the caller's fields aren't already present in the entry
864  
865 1 my ($field, %in_list);
866 1 foreach $field (@$fields)
867 {
868 4 $in_list{$field} = 1;
869 4 ▶ unless (exists $self->{'values'}{$field})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"values"}{$field}) 1 3 4
870 {
871 1 carp "Implicitly adding undefined field \"$field\"";
872 1 $self->{'values'}{$field} = undef;
873 }
874 }
875  
876 # And see if there are any fields in the entry that aren't in the user's
877 # list; delete them from the entry if so
878  
879 1 foreach $field (keys %{$self->{'values'}})
880 {
881 4 ▶ unless ($in_list{$field})
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_list{$field}) 0 4 4
882 {
883 0 carp "Implicitly deleting field \"$field\"";
884 0 delete $self->{'values'}{$field};
885 }
886 }
887  
888 # Now we can install (a copy of) the caller's desired field list;
889  
890 1 $self->{'fields'} = [@$fields];
891 }
892  
893  
894 =head2 Entry output methods
895  
896 =over 4
897  
898 =item write (BIBFILE)
899  
900 Prints a BibTeX entry on the filehandle associated with BIBFILE (which
901 should be a C<Text::BibTeX::File> object, opened for output). Currently
902 the printout is not particularly human-friendly; a highly configurable
903 pretty-printer will be developed eventually.
904  
905 =item print (FILEHANDLE)
906  
907 Prints a BibTeX entry on FILEHANDLE.
908  
909 =item print_s ()
910  
911 Prints a BibTeX entry to a string, which is the return value.
912  
913 =cut
914  
915 sub write
916 2 ▶{
Subroutine write: called CC=1 SCAR=0.0
Pod documented
917 2 my ($self, $bibfile) = @_;
918  
919 2 my $fh = $bibfile->{'handle'};
920 2 $self->print ($fh);
921 }
922  
923 sub print
924 4 ▶{
Subroutine print: called CC=2 SCAR=7.1
Pod documented
925 4 my ($self, $handle) = @_;
926  
927 4 ▶ $handle ||= \*STDOUT;
Condition50%1/2
$handle ||= \*STDOUT
l!l
40
MC/DC0%0/1
$handle ||= \*STDOUT
$handle
Truth table50%1/2
A
A= $handle
Aresult
11
00
928 4 print $handle $self->print_s;
929 }
930  
931 sub print_s
932 5 ▶{
Subroutine print_s: called CC=11 SCAR=31.1
Pod documented
933 5 my $self = shift;
934 5 my ($field, $output);
935  
936 sub value_to_string
937 17 ▶ {
Subroutine value_to_string: called CC=5 SCAR=26.5
Pod undocumented
938 17 my $value = shift;
939  
940 17 ▶ if (! ref $value) # just a string
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $value) { } 17 0 17
941 {
942 17 return "{$value}";
943 }
944 else # a Text::BibTeX::Value object
945 {
946 0 ▶ confess "value is a reference, but not to Text::BibTeX::Value object"
Branch: 1 branch
BranchTrueFalseTotal
unless $value->isa("Text::BibTeX::Value") 0 0 0
947 unless $value->isa ('Text::BibTeX::Value');
948 0 my @values = $value->values;
949 0 foreach (@values)
950 {
951 0 ▶ $_ = $_->type == &BTAST_STRING ? '{' . $_->text . '}' : $_->text;
Branch: 1 branch
BranchTrueFalseTotal
$_->type == &BTAST_STRING ? : 0 0 0
952 }
953 0 return join (' # ', @values);
954 }
955 }
956  
957 5 ▶ carp "entry type undefined" unless defined $self->{'type'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"type"} 0 5 5
958 5 ▶ carp "entry metatype undefined" unless defined $self->{'metatype'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"metatype"} 0 5 5
959  
960 # Regular and macro-def entries have to be treated differently when
961 # printing the first line, because the former have keys and the latter
962 # do not.
963 5 ▶ if ($self->{'metatype'} == &BTE_REGULAR)
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'metatype'} == &BTE_REGULAR) { } 5 0 5
elsif ($self->{'metatype'} == &BTE_MACRODEF) { } 0 0 0
964 {
965 5 ▶ carp "entry key undefined" unless defined $self->{'key'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"key"} 0 5 5
966 $output = sprintf ("@%s{%s,\n",
967 $self->{'type'} || '',
968 5 ▶ $self->{'key'} || '');
Condition50%2/4
$self->{'type'} || ''
l!l
50
$self->{'key'} || ''
l!l
50
MC/DC0%0/1
$self->{'type'} || ''
$self->{'type'}
MC/DC0%0/1
$self->{'key'} || ''
$self->{'key'}
Truth table50%1/2
A
A= $self->{'type'}
Aresult
11
00
Truth table50%1/2
A
A= $self->{'key'}
Aresult
11
00
969 }
970 elsif ($self->{'metatype'} == &BTE_MACRODEF)
971 {
972 $output = sprintf ("@%s{\n",
973 0 ▶ $self->{'type'} || '');
Condition0%0/2
$self->{'type'} || ''
l!l
00
MC/DC0%0/1
$self->{'type'} || ''
$self->{'type'}
Truth table0%0/2
A
A= $self->{'type'}
Aresult
11
00
974 }
975  
976 # Comment and preamble entries are treated the same -- we print out
977 # the entire entry, on one line, right here.
978 else # comment or preamble
979 {
980 return sprintf ("@%s{%s}\n\n",
981 $self->{'type'},
982 0 value_to_string ($self->{'value'}));
983 }
984  
985 # Here we print out all the fields/values of a regular or macro-def entry
986 5 my @fields = @{$self->{'fields'}};
987 5 ▶ while ($field = shift @fields)
Branch: 1 branch
BranchTrueFalseTotal
if ($field = shift @fields) 17 5 22
988 {
989 17 my $value = $self->{'values'}{$field};
990 17 ▶ if (! defined $value)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 17 17
991 {
992 0 carp "field \"$field\" has undefined value\n";
993 0 $value = '';
994 }
995  
996 17 $output .= " $field = ";
997 17 $output .= value_to_string ($value);
998  
999 17 $output .= ",\n";
1000 }
1001  
1002 # Tack on the last line, and we're done!
1003 5 $output .= "}\n\n";
1004 5 $output;
1005 }
1006  
1007 =back
1008  
1009 =head2 Miscellaneous methods
1010  
1011 =over 4
1012  
1013 =item warn (WARNING [, FIELD])
1014  
1015 Prepends a bit of location information (filename and line number(s)) to
1016 WARNING, appends a newline, and passes it to Perl's C<warn>. If FIELD is
1017 supplied, the line number given is just that of the field; otherwise, the
1018 range of lines for the whole entry is given. (Well, almost -- currently,
1019 the line number of the last field is used as the last line of the whole
1020 entry. This is a bug.)
1021  
1022 For example, if lines 10-15 of file F<foo.bib> look like this:
1023  
1024 @article{homer97,
1025 author = {Homer Simpson and Ned Flanders},
1026 title = {Territorial Imperatives in Modern Suburbia},
1027 journal = {Journal of Suburban Studies},
1028 year = 1997
1029 }
1030  
1031 then, after parsing this entry to C<$entry>, the calls
1032  
1033 $entry->warn ('what a silly entry');
1034 $entry->warn ('what a silly journal', 'journal');
1035  
1036 would result in the following warnings being issued:
1037  
1038 foo.bib, lines 10-14: what a silly entry
1039 foo.bib, line 13: what a silly journal
1040  
1041 =cut
1042  
1043 sub warn
1044 0 ▶{
Subroutine warn: not called CC=5 SCAR=34.0
Pod documented
1045 0 my ($self, $warning, $field) = @_;
1046  
1047 0 my $location = '';
1048 0 ▶ if ($self->{'file'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"file"}) 0 0 0
1049 {
1050 0 $location = $self->{'file'}{'filename'} . ", ";
1051 }
1052  
1053 0 my $lines = $self->{'lines'};
1054 0 ▶ my $entry_range = ($lines->{'START'} == $lines->{'STOP'})
Branch: 1 branch
BranchTrueFalseTotal
$lines->{'START'} == $lines->{'STOP'} ? : 0 0 0
1055 ? "line $lines->{'START'}"
1056 : "lines $lines->{'START'}-$lines->{'STOP'}";
1057  
1058 0 ▶ if (defined $field)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) { } 0 0 0
1059 {
1060 0 ▶ $location .= (exists $lines->{$field})
Branch: 1 branch
BranchTrueFalseTotal
exists $lines->{$field} ? : 0 0 0
1061 ? "line $lines->{$field}: "
1062 : "$entry_range (unknown field \"$field\"): ";
1063 }
1064 else
1065 {
1066 0 $location .= "$entry_range: ";
1067 }
1068  
1069 0 warn "$location$warning\n";
1070 }
1071  
1072  
1073 =item line ([FIELD])
1074  
1075 Returns the line number of FIELD. If the entry was parsed from a string,
1076 this still works--it's just the line number relative to the start of the
1077 string. If the entry was parsed from a file, this works just as you'd
1078 expect it to: it returns the absolute line number with respect to the
1079 whole file. Line numbers are one-based.
1080  
1081 If FIELD is not supplied, returns a two-element list containing the line
1082 numbers of the beginning and end of the whole entry. (Actually, the
1083 "end" line number is currently inaccurate: it's really the the line
1084 number of the last field in the entry. But it's better than nothing.)
1085  
1086 =cut
1087  
1088 sub line
1089 7 ▶{
Subroutine line: called CC=2 SCAR=8.4
Pod documented
1090 7 my ($self, $field) = @_;
1091  
1092 7 ▶ if (defined $field)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) { } 7 0 7
1093 {
1094 7 return $self->{'lines'}{$field};
1095 }
1096 else
1097 {
1098 0 return @{$self->{'lines'}}{'START','STOP'};
1099 }
1100 }
1101  
1102 =item filename ()
1103  
1104 Returns the name of the file from which the entry was parsed. Only
1105 works if the file is represented by a C<Text::BibTeX::File> object---if
1106 you just passed a filename/filehandle pair to C<parse>, you can't get
1107 the filename back. (Sorry.)
1108  
1109 =cut
1110  
1111 sub filename
1112 6 ▶{
Subroutine filename: called CC=1 SCAR=0.0
Pod documented
1113 6 my $self = shift;
1114  
1115 6 $self->{'file'}{'filename'}; # ooh yuck -- poking into File object
1116 }
1117  
1118 18 1;
1119  
1120 =back
1121  
1122 =head1 SEE ALSO
1123  
1124 L<Text::BibTeX>, L<Text::BibTeX::File>, L<Text::BibTeX::Structure>
1125  
1126 =head1 AUTHOR
1127  
1128 Greg Ward <gward@python.net>
1129  
1130 =head1 COPYRIGHT
1131  
1132 Copyright (c) 1997-2000 by Gregory P. Ward. All rights reserved. This file
1133 is part of the Text::BibTeX library. This library is free software; you
1134 may redistribute it and/or modify it under the same terms as Perl itself.
1135  
1136 =cut
« blib/lib/Text/BibTeX/File.pm ↑ Summary blib/lib/Text/BibTeX/Error.pm »