blib/lib/Text/BibTeX/File.pm

filter: Statementstmt 97.4% 77 / 79 Branchbran 77.2% 34 / 44 Conditioncond 70.9% 22 / 31 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 100% 17 / 17 Podpod 100% 7 / 7 totaltotal 84.5% 164 / 194 CC 33 SCAR 23.9
CC
33
Cov
86%
open
22.1
new
5.0
_parser
4.1
CRAP 11.0
« blib/lib/Text/BibTeX.pm ↑ Summary blib/lib/Text/BibTeX/Entry.pm »
1 # ----------------------------------------------------------------------
2 # NAME : BibTeX/File.pm
3 # CLASSES : Text::BibTeX::File
4 # RELATIONS :
5 # DESCRIPTION: Provides an object-oriented interface to whole BibTeX
6 # files.
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  
18 package Text::BibTeX::File;
19  
20 18 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 18 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 18 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 18 ▶use Text::BibTeX;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 18 ▶use Text::BibTeX::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 18 our $VERSION = '1.00';
30  
31 =head1 NAME
32  
33 Text::BibTeX::File - interface to whole BibTeX files
34  
35 =head1 SYNOPSIS
36  
37 use Text::BibTeX::File;
38  
39 $bib = Text::BibTeX::File->new("foo.bib") or die "foo.bib: $!\n";
40 # or:
41 $bib = Text::BibTeX::File->new;
42 $bib->open("foo.bib", {binmode => 'utf-8', normalization => 'NFC'}) || die "foo.bib: $!\n";
43  
44 $bib->set_structure ($structure_name,
45 $option1 => $value1, ...);
46  
47 $at_eof = $bib->eof;
48  
49 $bib->close;
50  
51 =head1 DESCRIPTION
52  
53 C<Text::BibTeX::File> provides an object-oriented interface to BibTeX
54 files. Its most obvious purpose is to keep track of a filename and
55 filehandle together for use by the C<Text::BibTeX::Entry> module (which
56 is much more interesting). In addition, it allows you to specify
57 certain options which are applicable to a whole database (file), rather
58 than having to specify them for each entry in the file. Currently, you
59 can specify the I<database structure> and some I<structure options>.
60 These concepts are fully documented in L<Text::BibTeX::Structure>.
61  
62 =head1 METHODS
63  
64 =head2 Object creation, file operations
65  
66 =over 4
67  
68 =item new ([FILENAME], [OPTS])
69  
70 Creates a new C<Text::BibTeX::File> object. If FILENAME is supplied, passes
71 it to the C<open> method (along with OPTS). If the C<open> fails, C<new>
72 fails and returns false; if the C<open> succeeds (or if FILENAME isn't
73 supplied), C<new> returns the new object reference.
74  
75 =item open (SOURCE [, OPTS])
76  
77 Opens SOURCE for reading. SOURCE is usually a filename, but may also be
78 a reference to a scalar (to read from a string in memory) or an already
79 open filehandle (any Perl filehandle works, including tied handles and
80 handles with I/O layers). OPTS is a hashref that can have the following
81 values (case-insensitive):
82  
83 =over 4
84  
85 =item MODE
86  
87 mode as specified by L<IO::File>
88  
89 =item PERMS
90  
91 permissions as specified by L<IO::File>. Can only be used in conjunction
92 with C<MODE>
93  
94 =item BINMODE
95  
96 C<'utf-8'> (the default) or C<'bytes'> (also C<'raw'>).
97  
98 In C<'utf-8'> mode the input is decoded as UTF-8 and all strings returned
99 (entry types, keys, field names, values, names...) are Perl character
100 strings, normalised as given by C<NORMALIZATION>. Bytes that are not valid
101 UTF-8 are replaced by U+FFFD with a warning; use C<ENCODING> to read files
102 in other encodings. A leading byte order mark is ignored.
103  
104 In C<'bytes'> mode the input is not decoded at all and strings are
105 returned as byte strings, exactly as they appear in the file.
106  
107 =item ENCODING
108  
109 The encoding of the file, if it isn't UTF-8 (eg. C<'latin1'>): an
110 C<:encoding(...)> layer is pushed onto the filehandle. If you pass in a
111 filehandle that already has an encoding layer, you don't need this.
112  
113 =item NORMALIZATION
114  
115 The Unicode normalization form for strings returned in C<'utf-8'> mode:
116 C<'NFC'> (the default), C<'NFD'>, C<'NFKC'>, C<'NFKD'>, or C<'none'>.
117  
118 =item RESET_MACROS
119  
120 By default, macros (C<@string> definitions) accumulate in one global
121 table, as in BibTeX itself: when you open a second file, macros defined by
122 the first are still available. This option clears all macro definitions
123 (except for the month macros) before the file is read.
124  
125 =item PRIVATE_MACROS
126  
127 Give this file its own macro table: C<@string> definitions in it are not
128 seen by any other file, and it does not see those made by other files.
129 It still sees the global macros (the month names, and any defined with
130 L<Text::BibTeX/add_macro_text>).
131  
132 =item FILENAME
133  
134 When SOURCE is a filehandle, the name to use in error messages.
135  
136 =back
137  
138 =item close ()
139  
140 Closes the filehandle associated with the object. If there is no such
141 filehandle (i.e., C<open> was never called on the object), does nothing.
142  
143 =item eof ()
144  
145 Returns the end-of-file state of the filehandle associated with the
146 object: a true value means we are at the end of the file.
147  
148 =back
149  
150 =cut
151  
152 sub new
153 32 ▶{
Subroutine new: called CC=4 SCAR=16.1
Pod documented
154 32 my $class = shift;
155  
156 32 ▶ $class = ref ($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0320
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
157 32 my $self = bless {}, $class;
158 32 ▶ ($self->open (@_) || return undef) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 32 0 32
Condition40%2/5
@_ && ($self->open(@_) || (return undef))
!ll&&!rl&&r
0320
$self->open(@_) or return undef
l!l
320
MC/DC0%0/2
@_ && ($self->open(@_) || (return undef))
@_ $self->open(@_)
Truth table0%0/3
A && B
A= @_B= $self->open(@_)
ABresult
0X0
100
111
159 32 $self;
160 }
161  
162 32 ▶sub open {
Subroutine open: called CC=21 SCAR=31.0
Pod documented
163 32 my ($self, $source, @args) = @_;
164 32 ▶ my $opts = (ref $args[0] eq 'HASH') ? shift @args : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] eq 'HASH' ? : 11 21 32
165 32 my ($binmode, $norm, $o) = Text::BibTeX::_options($opts);
166  
167 32 ▶ $self->close if $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handle"} 0 32 32
168 32 $self->{binmode} = $binmode;
169 32 $self->{normalization} = $norm;
170 32 ▶ $self->{private_macros} = $o->{private_macros} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$o->{'private_macros'} ? : 1 31 32
171  
172 32 ▶ if ($o->{reset_macros}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{"reset_macros"}) 0 32 32
173 0 Text::BibTeX::delete_all_macros();
174 0 Text::BibTeX::_define_months();
175 }
176  
177 32 ▶ if (exists $o->{mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $o->{"mode"}) 2 30 32
178 2 @args = ($o->{mode});
179 2 ▶ push @args, $o->{perms} if exists $o->{perms};
Branch: 1 branch
BranchTrueFalseTotal
if exists $o->{"perms"} 0 2 2
180 }
181  
182 32 my $fh;
183 32 ▶ if (ref $source && (reftype($source) // '') eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $source and (builtin::reftype($source) // '') eq 'GLOB') { } 10 22 32
Condition80%4/5
ref $source and (builtin::reftype($source) // "") eq "GLOB"
!ll&&!rl&&r
14810
builtin::reftype($source) // ""
l!l
180
MC/DC50%1/2
ref $source and (builtin::reftype($source) // "") eq "GLOB"
ref $source builtin::reftype($source)
Truth table66%2/3
A and B
A= ref $sourceB= builtin::reftype($source)
ABresult
0X0
100
111
184 10 $fh = $source; # an open filehandle
185 10 $self->{filename} = $o->{filename};
186 10 $self->{own_handle} = 0;
187 }
188 else {
189 22 ▶ $self->{filename} = $o->{filename} // (ref $source ? undef : $source);
Branch: 1 branch
BranchTrueFalseTotal
ref $source ? : 8 14 22
Condition66%2/3
$o->{"filename"} // (ref $source ? undef : $source)
l!l&&r!l&&!r
0148
MC/DC50%1/2
$o->{"filename"} // (ref $source ? undef : $source)
$o->{"filename"} (ref $source ? undef : $source)
Truth table66%2/3
A // B
A= $o->{"filename"}B= (ref $source ? undef : $source)
ABresult
1X1
011
000
190 22 $fh = IO::File->new;
191 # (a plain filename keeps IO::File's 2-argument open semantics)
192 22 ▶ @args = ('<') if ref $source && !@args;
Branch: 1 branch
BranchTrueFalseTotal
if ref $source and not @args 8 14 22
Condition66%2/3
ref $source and not @args
!ll&&!rl&&r
1408
MC/DC50%1/2
ref $source and not @args
ref $source not @args
Truth table66%2/3
A and not(B)
A= ref $sourceB= not @args
ABresult
0X0
100
111
193 22 ▶ $fh->open($source, @args) or return;
Branch: 1 branch
BranchTrueFalseTotal
$fh->open($source, @args) or return 22 0 22
194 22 $self->{own_handle} = 1;
195 }
196  
197 32 ▶ if (defined $o->{encoding}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $o->{'encoding'}) { } 1 31 32
elsif ($binmode eq 'utf-8' and $self->{'own_handle'} and @args and $args[0] =~ /^\s*(?:[>+]|[wa])/u) { } 1 52 53
198 1 ▶ binmode($fh, ":encoding($o->{encoding})")
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, qq[:encoding($o->{"encoding"})] or croak(qq[Text::BibTeX::File: can't use encoding '$o->{"encoding"}': $!]) 1 0 1
199 or croak "Text::BibTeX::File: can't use encoding '$o->{encoding}': $!";
200 }
201 ▶ elsif ($binmode eq 'utf-8' && $self->{own_handle} && @args
Condition100%9/9
$binmode eq "utf-8" and $self->{"own_handle"} and @args and $args[0] =~ /^\s*(?:[>+]|[wa])/u
!ll&&!rl&&r
2281
$binmode eq "utf-8" and $self->{"own_handle"} and @args
!ll&&!rl&&r
12109
$binmode eq "utf-8" and $self->{"own_handle"}
!ll&&!rl&&r
21019
MC/DC100%4/4
$binmode eq "utf-8" and $self->{"own_handle"} and @args and $args[0] =~ /^\s*(?:[>+]|[wa])/u
$binmode eq "utf-8" $self->{"own_handle"} @args $args[0] =~ /^\s*(?:[>+]|[wa])/u
Truth table100%5/5
A and B and C and D
A= $binmode eq "utf-8"B= $self->{"own_handle"}C= @argsD= $args[0] =~ /^\s*(?:[>+]|[wa])/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
202 && $args[0] =~ /^\s*(?:[>+]|[wa])/) {
203 # writing character strings: encode them as UTF-8
204 1 binmode($fh, ':encoding(UTF-8)');
205 }
206  
207 32 $self->{handle} = $fh;
208 32 return 1;
209 }
210  
211 # The parser for this file (created when the first entry is read)
212 148 ▶sub _parser {
Subroutine _parser: called CC=4 SCAR=14.2
213 148 my $self = shift;
214 148 ▶ croak "Text::BibTeX::File: file is not open" unless $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"handle"} 0 148 148
215 return $self->{parser} //= Text::BibTeX::Parser->new(
216 $self->{handle}, $self->{filename},
217 148 ▶ $self->{binmode} eq 'utf-8' ? 1 : 0, $self->{private_macros});
Branch: 1 branch
BranchTrueFalseTotal
$self->{'binmode'} eq 'utf-8' ? : 28 2 30
Condition66%2/3
$self->{"parser"} //= "Text::BibTeX::Parser"->new($self->{"handle"}, $self->{"filename"}, $self->{"binmode"} eq "utf-8" ? 1 : 0, $self->{"private_macros"})
l!l&&r!l&&!r
118300
MC/DC0%0/2
$self->{"parser"} //= "Text::BibTeX::Parser"->new($self->{"handle"}, $self->{"filename"}, $self->{"binmode"} eq "utf-8" ? 1 : 0, $self->{"private_macros"})
$self->{"parser"} "Text::BibTeX::Parser"->new($self->{"handle"}, $self->{"filename"}, $self->{"binmode"} eq "utf-8" ? 1 : 0, $self->{"private_macros"})
Truth table66%2/3
A //= B
A= $self->{"parser"}B= "Text::BibTeX::Parser"->new($self->{"handle"}, $self->{"filename"}, $self->{"binmode"} eq "utf-8" ? 1 : 0, $self->{"private_macros"})
ABresult
1X1
011
000
218 }
219  
220 sub close
221 35 ▶{
Subroutine close: called CC=3 SCAR=11.0
Pod documented
222 35 my $self = shift;
223 35 delete $self->{parser};
224 35 ▶ if (my $fh = delete $self->{handle}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = delete $self->{"handle"}) 32 3 35
225 32 ▶ $fh->close if $self->{own_handle};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"own_handle"} 22 10 32
226 }
227 }
228  
229 sub eof
230 4 ▶{
Subroutine eof: called CC=3 SCAR=11.0
Pod documented
231 4 my $self = shift;
232 4 ▶ return 1 unless $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"handle"} 0 4 4
233 4 ▶ return $self->{parser}->eof if $self->{parser};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parser"} 3 1 4
234 1 eof ($self->{handle});
235 }
236  
237 sub DESTROY
238 32 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
239 32 my $self = shift;
240 32 $self->close;
241 }
242  
243 =head2 Object properties
244  
245 =over 4
246  
247 =item set_structure (STRUCTURE [, OPTION =E<gt> VALUE, ...])
248  
249 Sets the database structure for a BibTeX file. At the simplest level,
250 this means that entries from the file are expected to conform to certain
251 field requirements as specified by the I<structure module>. It also
252 gives you full access to the methods of the particular I<structured
253 entry class> for this structure, allowing you to perform operations
254 specific to this kind of database. See L<Text::BibTeX::Structure/"CLASS
255 INTERACTIONS"> for all the consequences of setting the database
256 structure for a C<Text::BibTeX::File> object.
257  
258 =item structure ()
259  
260 Returns the name of the database structure associated with the object
261 (as set by C<set_structure>).
262  
263 =cut
264  
265 sub set_structure
266 1 ▶{
Subroutine set_structure: called CC=2 SCAR=7.0
Pod documented
267 1 my ($self, $structure, @options) = @_;
268  
269 1 require Text::BibTeX::Structure;
270 1 ▶ croak "Text::BibTeX::File::set_structure: options list must have even " .
Branch: 1 branch
BranchTrueFalseTotal
unless @options % 2 == 0 0 1 1
271 "number of elements"
272 unless @options % 2 == 0;
273 1 $self->{structure} = Text::BibTeX::Structure->new($structure, @options);
274 }
275  
276 144 ▶sub structure { shift->{structure} }
Subroutine structure: called CC=1 SCAR=0.0
Pod documented
277  
278  
279 =item preserve_values ([PRESERVE])
280  
281 Sets the "preserve values" flag, to control all future parsing of entries
282 from this file. If PRESERVE isn't supplied, returns the current state of
283 the flag. See L<Text::BibTeX::Value> for details on parsing in "value
284 preservation" mode.
285  
286 =back
287  
288 =cut
289  
290 sub preserve_values
291 148 ▶{
Subroutine preserve_values: called CC=2 SCAR=7.1
Pod documented
292 148 my $self = shift;
293  
294 148 ▶ $self->{'preserve_values'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 148 148
295 148 $self->{'preserve_values'};
296 }
297  
298  
299 18 1;
300  
301 =head1 SEE ALSO
302  
303 L<Text::BibTeX>, L<Text::BibTeX::Entry>, L<Text::BibTeX::Structure>
304  
305 =head1 AUTHOR
306  
307 Greg Ward <gward@python.net>
308  
309 =head1 COPYRIGHT
310  
311 Copyright (c) 1997-2000 by Gregory P. Ward. All rights reserved. This file
312 is part of the Text::BibTeX library. This library is free software; you
313 may redistribute it and/or modify it under the same terms as Perl itself.
« blib/lib/Text/BibTeX.pm ↑ Summary blib/lib/Text/BibTeX/Entry.pm »