blib/lib/Text/BibTeX/Value.pm

filter: Statementstmt 55.8% 24 / 43 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 66.6% 8 / 12 Podpod 100% 3 / 3 totaltotal 32.1% 35 / 109 CC 15 SCAR 38.4
CC
15
Cov
30%
new
90.0
new
56.0
value
2.0
CRAP 46.7
« blib/lib/Text/BibTeX/Structure.pm ↑ Summary blib/lib/Text/BibTeX.pm »
1 # ----------------------------------------------------------------------
2 # NAME : Text::BibTeX::Value
3 # CLASSES : Text::BibTeX::Value, Text::BibTeX::SimpleValue
4 # RELATIONS :
5 # DESCRIPTION: Provides interfaces to BibTeX values (list of simple
6 # values) and simple values (string/macro/number).
7 # CREATED : 1998/03/12, 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::Value;
19  
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 our $VERSION = '1.00';
25  
26 =head1 NAME
27  
28 Text::BibTeX::Value - interfaces to BibTeX values and simple values
29  
30 =head1 SYNOPSIS
31  
32 use Text::BibTeX;
33  
34 $entry = Text::BibTeX::Entry->new;
35  
36 # set the 'preserve_values' flag to 1 for this parse
37 $entry->parse ($filename, $filehandle, 1);
38  
39 # 'get' method now returns a Text::BibTeX::Value object
40 # rather than a string
41 $value = $entry->get ($field);
42  
43 # query the `Value' object (list of SimpleValue objects)
44 @all_values = $value->values;
45 $first_value = $value->value (0);
46 $last_value = $value->value (-1);
47  
48 # query the simple value objects -- type will be one of BTAST_STRING,
49 # BTAST_MACRO, or BTAST_NUMBER
50 use Text::BibTex (':nodetypes'); # import "node type" constants
51 $is_macro = ($first_value->type == BTAST_MACRO);
52 $text = $first_value->text;
53  
54 =head1 DESCRIPTION
55  
56 The C<Text::BibTeX::Value> module provides two classes,
57 C<Text::BibTeX::Value> and C<Text::BibTeX::SimpleValue>, which respectively
58 give you access to BibTeX "compound values" and "simple values". Recall
59 that every field value in a BibTeX entry is the concatenation of one or
60 more simple values, and that each of those simple values may be a literal
61 string, a macro (abbreviation), or a number. Normally with
62 C<Text::BibTeX>, field values are "fully processed," so that you only have
63 access to the string that results from expanding macros, converting numbers
64 to strings, concatenating all sub-strings, and collapsing whitespace in the
65 resulting string.
66  
67 For example, in the following entry:
68  
69 @article{homer97,
70 author = "Homer Simpson" # and # "Ned Flanders",
71 title = {Territorial Imperatives in Modern Suburbia},
72 journal = jss,
73 year = 1997
74 }
75  
76 we see the full range of options. The C<author> field consists of three
77 simple values: a string, a macro (C<and>), and another string. The
78 C<title> field is a single string, and the C<journal> and C<year> fields
79 are, respectively, a single macro and a single number. If you parse
80 this entry in the usual way:
81  
82 $entry = Text::BibTeX::Entry->new($entry_text);
83  
84 then the C<get> method on C<$entry> would return simple strings.
85 Assuming that the C<and> macro is defined as C<" and ">, then
86  
87 $entry->get ('author')
88  
89 would return the Perl string C<"Homer Simpson and Ned Flanders">.
90  
91 However, you can also request that the library preserve the input values
92 in your entries, i.e. not lose the information about which values use
93 macros, which values are composed of multiple simple values, and so on.
94 There are two ways to make this request: per-file and per-entry. For a
95 per-file request, use the C<preserve_values> method on your C<File>
96 object:
97  
98 $bibfile = Text::BibTeX::File->new($filename);
99 $bibfile->preserve_values (1);
100  
101 $entry = Text::BibTeX::Entry->new($bibfile);
102 $entry->get ($field); # returns a Value object
103  
104 $bibfile->preserve_values (0);
105 $entry = Text::BibTeX::Entry->new($bibfile);
106 $entry->get ($field); # returns a string
107  
108 If you're not using a C<File> object, or want to control things at a
109 finer scale, then you have to pass in the C<preserve_values> flag when
110 invoking C<read>, C<parse>, or C<parse_s> on your C<Entry> objects:
111  
112 # no File object, parsing from a string
113 $entry = Text::BibTeX::Entry->new;
114 $entry->parse_s ($entry_text, 0); # preserve_values=0 (default)
115 $entry->get ($field); # returns a string
116  
117 $entry->parse_s ($entry_text, 1);
118 $entry->get ($field); # returns a Value object
119  
120 # using a File object, but want finer control
121 $entry->read ($bibfile, 0); # now get will return strings (default)
122 $entry->read ($bibfile, 1); # now get will return Value objects
123
124 A compound value, usually just called a value, is simply a list of
125 simple values. The C<Text::BibTeX::Value> class (hereinafter
126 abbreviated as C<Value>) provides a simple interface to this list; you
127 can request the whole list, or an individual member of the list. The
128 C<SimpleValue> class gives you access to the innards of each simple
129 value, which consist of the I<type> and the I<text>. The type just
130 tells you if this simple value is a string, macro, or number; it is
131 represented using the Perl translation of the "node type" enumeration
132 from C. The possible types are C<BTAST_STRING>, C<BTAST_NUMBER>, and
133 C<BTAST_MACRO>. The text is just what appears in the original entry
134 text, be it a string, number, or macro.
135  
136 For example, we could parse the above entry in "preserve values" mode as
137 follows:
138  
139 $entry->parse_s ($entry_text, 1); # preserve_values is 1
140  
141 Then, using the C<get> method on C<$entry> would return not a string,
142 but a C<Value> object. We can get the list of all simple values using
143 the C<values> method, or a single value using C<value>:
144  
145 $author = $entry->get ('author'); # now a Text::BibTeX::Value object
146 @all_values = $author->values; # array of Text::BibTeX::SimpleValue
147 $second = $author->value (1); # same as $all_values[1]
148  
149 The simple values may be queried using the C<Text::BibTeX::SimpleValue>
150 methods, C<type> and C<text>:
151  
152 $all_values[0]->type; # returns BTAST_STRING
153 $second->type; # returns BTAST_MACRO
154  
155 $all_values[0]->text; # "Homer Simpson"
156 $second->text; # "and" (NOT the macro expansion!)
157  
158 $entry->get ('year')->value (0)->text; # "1997"
159  
160 =head1 POST-PROCESSING
161  
162 Unless values are preserved, each field value goes through the same
163 processing as in BibTeX:
164  
165 =over 4
166  
167 =item *
168  
169 numbers are converted to strings (they are always kept as the string of
170 digits that appears in the file);
171  
172 =item *
173  
174 macros are expanded (an undefined macro expands to the empty string,
175 with a warning);
176  
177 =item *
178  
179 the simple values are pasted together into a single string;
180  
181 =item *
182  
183 whitespace is collapsed: runs of whitespace become a single space, and
184 leading and trailing whitespace is removed.
185  
186 =back
187  
188 So, with the C<and> macro defined as C<" and ">, the C<author> field in
189 the example above becomes C<"Homer Simpson and Ned Flanders">. Note
190 that whitespace is collapsed I<after> pasting, so it doesn't matter
191 which of the pieces the spaces between them came from.
192  
193 The text of a macro definition (C<@string>) is processed in the same way
194 except that its whitespace is not collapsed: that happens when the
195 macro is used in a field value.
196  
197 When values are preserved, none of this is done, so the text of each
198 simple value is as it appears in the input (minus the quotes or braces
199 around strings), except that each newline, carriage return and tab in a
200 string is replaced with a space.
201  
202 Whether or not values are preserved, entry types and field names are
203 converted to lowercase (using Unicode case mapping).
204  
205 =head1 METHODS
206  
207 Normally, you won't need to create C<Value> or C<SimpleValue>
208 objects---they'll be created for you when an entry is parsed, and
209 returned to you by the C<get> method in the C<Entry> class. Thus, the
210 query methods (C<values> and C<value> for the C<Value> class, C<type>
211 and C<text> for C<SimpleValue>) are probably all you need to worry
212 about. If you wish, though, you can create new values and simple values
213 using the two classes' respective constructors. You can also put
214 newly-created C<Value> objects back into an existing C<Entry> object
215 using the C<set> entry method; it doesn't matter how the entry was
216 parsed, this is acceptable anytime.
217  
218 =head2 Text::BibTeX::Value methods
219  
220 =over 4
221  
222 =item new (SVAL, ...)
223  
224 Creates a new C<Value> object from a list of simple values. Each simple
225 value, SVAL, may be either a C<SimpleValue> object or a reference to a
226 two-element list containing the type and text of the simple value. For
227 example, one way to recreate the C<author> field of the example entry in
228 L<"DESCRIPTION"> would be:
229  
230 $and_macro = Text::BibTeX::SimpleValue->new (BTAST_MACRO, 'and');
231 $value = Text::BibTeX::Value->new
232 ([BTAST_STRING, 'Homer Simpson'],
233 $and_macro,
234 [BTAST_STRING, 'Ned Flanders']);
235  
236 The resulting C<Value> object could then be installed into an entry
237 using the C<set> method of the C<Entry> class.
238  
239 =cut
240  
241 sub new
242 0 ▶{
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
243 0 my $class = shift;
244  
245 0 ▶ $class = ref $class || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
246 0 my $self = bless [], $class;
247 0 ▶ while (my $sval = shift)
Branch: 1 branch
BranchTrueFalseTotal
if (my $sval = shift()) 0 0 0
248 {
249 0 ▶ $sval = Text::BibTeX::SimpleValue->new(@$sval)
Branch: 1 branch
BranchTrueFalseTotal
if ref $sval eq "ARRAY" and @$sval == 2 0 0 0
Condition0%0/3
ref $sval eq "ARRAY" and @$sval == 2
!ll&&!rl&&r
000
MC/DC0%0/2
ref $sval eq "ARRAY" and @$sval == 2
ref $sval eq "ARRAY" @$sval == 2
Truth table0%0/3
A and B
A= ref $sval eq "ARRAY"B= @$sval == 2
ABresult
0X0
100
111
250 if ref $sval eq 'ARRAY' && @$sval == 2;
251 0 ▶ croak "simple value is neither a two-element array ref " .
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($sval) and $sval->isa("Text::BibTeX::SimpleValue") 0 0 0
Condition0%0/3
builtin::blessed($sval) and $sval->isa("Text::BibTeX::SimpleValue")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($sval) and $sval->isa("Text::BibTeX::SimpleValue")
builtin::blessed($sval) $sval->isa("Text::BibTeX::SimpleValue")
Truth table0%0/3
A and B
A= builtin::blessed($sval)B= $sval->isa("Text::BibTeX::SimpleValue")
ABresult
0X0
100
111
252 "nor a Text::BibTeX::SimpleValue object"
253 unless blessed($sval) && $sval->isa('Text::BibTeX::SimpleValue');
254 0 push (@$self, $sval);
255 }
256  
257 0 $self;
258 }
259  
260 =item values ()
261  
262 Returns the list of C<SimpleValue> objects that make up a C<Value> object.
263  
264 =item value (NUM)
265  
266 Returns the NUM'th C<SimpleValue> object from the list of C<SimpleValue>
267 objects that make up a C<Value> object. This is just like a Perl array
268 reference: NUM is zero-based, and negative numbers count from the end of
269 the array.
270  
271 =back
272  
273 =cut
274  
275 # A Text::BibTeX::Value object is just an array ref; that array is a list
276 # of Text::BibTeX::SimpleValue objects. Most of the real work for Value
277 # and SimpleValue is done behind the scenes when an entry is parsed, in
278 # BibTeX.xs and btxs_support.c.
279  
280 2 ▶sub values { @{$_[0]} }
Subroutine values: called CC=1 SCAR=0.0
Pod documented
281  
282 0 ▶sub value { $_[0]->[$_[1]] }
Subroutine value: not called CC=1 SCAR=6.9
Pod documented
283  
284  
285 package Text::BibTeX::SimpleValue;
286  
287 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
288 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
289 1 ▶use Text::BibTeX qw(:nodetypes);
Subroutine BEGIN: called CC=1 SCAR=0.0
290  
291 1 our $VERSION = '1.00';
292  
293  
294 =head2 Text::BibTeX::SimpleValue methods
295  
296 =over
297  
298 =item new (TYPE, TEXT)
299  
300 Creates a new C<SimpleValue> object with the specified TYPE and TEXT.
301 TYPE must be one of the allowed types for BibTeX simple values,
302 i.e. C<BTAST_STRING>, C<BTAST_NUMBER>, or C<BTAST_MACRO>. You'll
303 probably want to import these constants from C<Text::BibTeX> using the
304 C<nodetypes> export tag:
305  
306 use Text::BibTeX qw(:nodetypes);
307  
308 TEXT may be any string. Note that if TYPE is C<BTAST_NUMBER> and TEXT
309 is not a string of digits, the C<SimpleValue> object will be created
310 anyways, but a warning will be issued. No warning is issued about
311 non-existent macros.
312  
313 =cut
314  
315 sub new
316 0 ▶{
Subroutine new: not called CC=9 SCAR=45.0
317 0 my ($class, $type, $text) = @_;
318  
319 0 ▶ croak "invalid simple value type ($type)"
Branch: 1 branch
BranchTrueFalseTotal
unless $type == &BTAST_STRING or $type == &BTAST_NUMBER or $type == &BTAST_MACRO 0 0 0
Condition0%0/6
$type == &BTAST_STRING or $type == &BTAST_NUMBER or $type == &BTAST_MACRO
l!l&&r!l&&!r
000
$type == &BTAST_STRING or $type == &BTAST_NUMBER
l!l&&r!l&&!r
000
MC/DC0%0/3
$type == &BTAST_STRING or $type == &BTAST_NUMBER or $type == &BTAST_MACRO
$type == &BTAST_STRING $type == &BTAST_NUMBER $type == &BTAST_MACRO
Truth table0%0/4
A or B or C
A= $type == &BTAST_STRINGB= $type == &BTAST_NUMBERC= $type == &BTAST_MACRO
ABCresult
1XX1
01X1
0011
0000
320 unless ($type == &BTAST_STRING ||
321 $type == &BTAST_NUMBER ||
322 $type == &BTAST_MACRO);
323 0 ▶ croak "invalid simple value text (must be a simple string or number)"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 0 0 0
Condition0%0/3
defined $text and not ref $text
!ll&&!rl&&r
000
MC/DC0%0/2
defined $text and not ref $text
defined $text not ref $text
Truth table0%0/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
324 unless defined $text && ! ref $text;
325 0 ▶ carp "warning: creating a 'number' simple value with non-numeric text"
Branch: 1 branch
BranchTrueFalseTotal
if $type == &BTAST_NUMBER and not $text =~ /^\d+$/ 0 0 0
Condition0%0/3
$type == &BTAST_NUMBER and not $text =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$type == &BTAST_NUMBER and not $text =~ /^\d+$/
$type == &BTAST_NUMBER not $text =~ /^\d+$/
Truth table0%0/3
A and not(B)
A= $type == &BTAST_NUMBERB= not $text =~ /^\d+$/
ABresult
0X0
100
111
326 if $type == &BTAST_NUMBER && $text !~ /^\d+$/;
327  
328 0 ▶ $class = ref $class || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
329 0 my $self = bless [undef, undef], $class;
330 0 $self->[0] = $type;
331 0 $self->[1] = $text;
332 0 $self;
333 }
334  
335  
336 =item type ()
337  
338 Returns the type of a simple value. This will be one of the allowed
339 "node types" as described under L</new> above.
340  
341 =item text ()
342  
343 Returns the text of a simple value. This is just the text that appears
344 in the original entry---unexpanded macro name, or unconverted number.
345 (Of course, converting numbers doesn't make any difference from Perl; in
346 fact, it's all the same in C too, since the C code just keeps numbers as
347 strings of digits. It's simply a matter of whether the string of digits
348 is represented as a string or a number, which you might be interested in
349 knowing if you want to preserve the structure of the input as much
350 possible.)
351  
352 =back
353  
354 =cut
355  
356 0 ▶sub type { shift->[0] }
Subroutine type: not called CC=1 SCAR=6.9
357  
358 2 ▶sub text { shift->[1] }
Subroutine text: called CC=1 SCAR=0.0
359  
360 1 1;
361  
362 =head1 SEE ALSO
363  
364 L<Text::BibTeX>, L<Text::BibTeX::File>, L<Text::BibTeX::Entry>
365  
366 =head1 AUTHOR
367  
368 Greg Ward <gward@python.net>
369  
370 =head1 COPYRIGHT
371  
372 Copyright (c) 1997-2000 by Gregory P. Ward. All rights reserved. This file
373 is part of the Text::BibTeX library. This library is free software; you
374 may redistribute it and/or modify it under the same terms as Perl itself.
375  
376 =cut
« blib/lib/Text/BibTeX/Structure.pm ↑ Summary blib/lib/Text/BibTeX.pm »