File Coverage

blib/lib/Tie/File/Indexed/Utf8.pm
Criterion Covered Total %
statement 17 17 100.0
branch 4 6 66.6
condition 1 2 50.0
subroutine 5 5 100.0
pod 2 2 100.0
total 29 32 90.6


line stmt bran cond sub pod time code
1             ##-*- Mode: CPerl -*-
2             ##
3             ## File: Tie/File/Indexed/Utf8.pm
4             ## Author: Bryan Jurish
5             ## Description: tied array access to indexed data files: utf8-encoded strings
6              
7              
8             package Tie::File::Indexed::Utf8;
9 2     2   15428 use Tie::File::Indexed;
  2         3  
  2         48  
10 2     2   975 use utf8;
  2         21  
  2         7  
11 2     2   45 use strict;
  2         2  
  2         264  
12              
13             ##======================================================================
14             ## Globals
15              
16             our @ISA = qw(Tie::File::Indexed);
17              
18             ##======================================================================
19             ## Subclass API: Data I/O: overides
20              
21             ## $bool = $tfi->writeData($utf8_string)
22             ## + override transparently encodes $data as utf8
23             sub writeData {
24 4     4 1 4 my $val = $_[1];
25 4 50       8 if (defined($val)) {
26 4 100       9 utf8::upgrade($val) if (!utf8::is_utf8($val)); ##-- convert byte-strings to utf8
27 4         5 utf8::encode($val); ##-- ... but write (encoded) byte-strings
28             }
29 4   50     15 return $_[0]{datfh}->print($val//'');
30             }
31              
32             ## $data_or_undef = $tfi->readData($length)
33             ## + read item data from $tfi->{datfh} from its current position
34             ## + returned scalars should always have their utf8 flag set
35             sub readData {
36 4 50   4 1 12 defined(my $buf = $_[0]->SUPER::readData($_[1])) or return undef;
37 4         11 utf8::decode($buf);
38 4         8 return $buf;
39             }
40              
41              
42             1; ##-- be happpy