DiaColloDB/Temp/Hash.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 109 CC 7 SCAR 20.8
CC
7
Cov
0%
TIEHASH
20.0
utf8_filter_fetch
6.0
utf8_filter_store
6.0
CRAP 8.0
1 ## -*- Mode: CPerl -*-
2 ## File: DiaColloDB::Temp::Array.pm
3 ## Author: Bryan Jurish <moocow@cpan.org>
4 ## Description: DiaColloDB: temporary hashes
5  
6 package DiaColloDB::Temp::Hash;
7 use DiaColloDB::Temp;
8 use DB_File;
9 use Fcntl qw(:DEFAULT);
10 use strict;
11  
12 ##======================================================================
13 ## Globals
14 our @ISA = qw(DB_File DiaColloDB::Temp);
15  
16 ## %TMPFILES : ("$obj" => $filename, ...)
17 ## + stores temporary filenames by stringified object; used by cleanup()
18 our %TMPFILES = qw();
19  
20 ##======================================================================
21 ## Overrides
22  
23 ## $tied = TIEHASH($classname, $file, %opts)
24 ## + %opts:
25 ## (
26 ## ##-- DB_File options
27 ## flags => $flags, ##-- DB_File flags; default=O_RDWR|O_CREAT|O_TRUNC
28 ## mode => $mode, ##-- DB_File mode; default=(0666 & ~umask)
29 ## type => $type, ##-- DB_File type; default=$DB_File::DB_BTREE
30 ## utf8keys => $bool, ##-- if true, keys will be stored as utf8 (default=false)
31 ## utf8vals => $bool, ##-- if true, values will be stored as utf8 (default=false)
32 ## utf8 => $bool, ##-- alias for utf8keys=>$bool, utf8vals=>$bool
33 ## ##
34 ## ##-- local options
35 ## UNLINK => $bool, ##-- if unspecified or true, file will be unlinked on DESTROY
36 ## )
37 sub TIEHASH {
38 my ($that,$file,%opts) = @_;
39 my $flags = $opts{flags} // (O_RDWR|O_CREAT|O_TRUNC);
40 my $mode = $opts{mode} // (0666 & ~umask);
41 my $type = $opts{type} // $DB_File::DB_BTREE;
42 my $tied = DB_File->TIEHASH($file, $flags, $mode, $type)
43 or $that->logconfess("TIEHASH(): failed to tie '$file' via DB_File: $!");
44 bless($tied, ref($that)||$that);
45  
46 ##-- track temporary files
47 if (!exists($opts{UNLINK}) || $opts{UNLINK}) {
48 $TMPFILES{"$tied"} = $file;
49 }
50  
51 ##-- optionally setup utf8 filters
52 if ($opts{utf8} || $opts{utf8keys}) {
53 $tied->filter_fetch_key(\&utf8_filter_fetch);
54 $tied->filter_store_key(\&utf8_filter_store);
55 }
56 if ($opts{utf8} || $opts{utf8vals}) {
57 $tied->filter_fetch_value(\&utf8_filter_fetch);
58 $tied->filter_store_value(\&utf8_filter_store);
59 }
60  
61 return $tied;
62 }
63  
64 ## undef = utf8_filter_fetch
65 sub utf8_filter_fetch {
66 utf8::decode($_) if (defined($_));
67 }
68  
69 ## undef = utf8_filter_store
70 sub utf8_filter_store {
71 utf8::encode($_) if (defined($_));
72 }
73  
74 ## undef = $tied->DESTROY()
75 ## + destructor calls cleanup()
76 sub DESTROY {
77 my $tied = shift;
78  
79 ##-- inherited destructor
80 $tied->DB_File::DESTROY();
81  
82 ##-- unlink temps
83 $tied->cleanup();
84 }
85  
86 ##======================================================================
87 ## DiaColloDB::Temp API
88  
89 ## undef = $obj->cleanup()
90 ## + unlink temp files (only if 'temp' attribute is set)
91 sub cleanup {
92 my $obj = shift;
93 return if (!exists($TMPFILES{"$obj"}));
94 CORE::unlink($TMPFILES{"$obj"});
95 delete $TMPFILES{"$obj"};
96 }
97  
98 ##======================================================================
99 ## Global destruction
100 END {
101 CORE::unlink($_) foreach (values %TMPFILES);
102 }
103  
104  
105 1; ##-- be happy
106  
107 __END__