File Coverage

blib/lib/Hash/FieldHash.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 20 20 100.0


line stmt bran cond sub pod time code
1             package Hash::FieldHash;
2              
3 19     19   273302 use 5.008_005;
  19         51  
  19         524  
4 19     19   63 use strict;
  19         20  
  19         712  
5              
6             our $VERSION = '0.14';
7              
8 19     19   7520 use parent qw(Exporter);
  19         4274  
  19         68  
9             our @EXPORT_OK = qw(fieldhash fieldhashes from_hash to_hash);
10             our %EXPORT_TAGS = (all => \@EXPORT_OK);
11              
12 19     19   1106 use XSLoader;
  19         23  
  19         998  
13             XSLoader::load(__PACKAGE__, $VERSION);
14              
15             sub fieldhashes{
16 6     6 1 48186 foreach my $hash_ref(@_){
17 12         1247 &fieldhash($hash_ref);
18             }
19             }
20              
21             1;
22             __END__
23              
24             =for stopwords uvar CPAN rw-accessors chainable
25              
26             =head1 NAME
27              
28             Hash::FieldHash - Lightweight field hash for inside-out objects
29              
30             =head1 VERSION
31              
32             This document describes Hash::FieldHash version 0.14.
33              
34             =head1 SYNOPSIS
35              
36             use Hash::FieldHash qw(:all);
37              
38             fieldhash my %foo;
39              
40             fieldhashes \my(%bar, %baz);
41              
42             {
43             my $o = Something->new();
44              
45             $foo{$o} = 42;
46              
47             print $foo{$o}; # => 42
48             }
49             # when $o is released, $foo{$o} is also deleted,
50             # so %foo is empty in here.
51              
52             # in a class
53             {
54             package Foo;
55             use Hash::FieldHash qw(:all);
56              
57             fieldhash my %bar, 'bar'; # make an accessor
58             }
59              
60             my $obj = bless {}, 'Foo';
61             $obj->bar(10); # does $bar{$obj} = 10
62              
63             =head1 DESCRIPTION
64              
65             C<Hash::FieldHash> provides the field hash mechanism which supports
66             the inside-out technique.
67              
68             You may know C<Hash::Util::FieldHash>. It's a very useful module,
69             but too complex to understand the functionality and only available in 5.10.
70             C<H::U::F::Compat> is available for pre-5.10, but it is too slow to use.
71              
72             This is a better alternative to C<H::U::F> with following features:
73              
74             =over 4
75              
76             =item Simpler interface
77              
78             C<Hash::FieldHash> provides a few functions: C<fieldhash()> and C<fieldhashes()>.
79             That's enough.
80              
81             =item Higher performance
82              
83             C<Hash::FieldHash> is faster than C<Hash::Util::FieldHash>, because
84             its internals use simpler structures.
85              
86             =item Relic support
87              
88             Although C<Hash::FieldHash> uses a new feature introduced in Perl 5.10,
89             I<the uvar magic for hashes> described in L<Hash::Util::Fieldhash/"GUTS">,
90             it supports Perl 5.8 using the traditional tie-hash layer.
91              
92             =back
93              
94             =head1 INTERFACE
95              
96             =head2 Exportable functions
97              
98             =over 4
99              
100             =item C<< fieldhash(%hash, ?$name, ?$package) >>
101              
102             Creates a field hash. The first argument must be a hash.
103              
104             Optional I<$name> and I<$package> indicate the name of the field, which will
105             create rw-accessors, using the same name as I<$name>.
106              
107             Returns nothing.
108              
109             =item C<< fieldhashes(@hash_refs) >>
110              
111             Creates a number of field hashes. All the arguments must be hash references.
112              
113             Returns nothing.
114              
115             =item C<< from_hash($object, \%fields) >>
116              
117             Fills the named fields associated with I<$object> with I<%fields>.
118             The keys of I<%fields> can be simple or fully qualified.
119              
120             Returns I<$object>.
121              
122             =item C<< to_hash($object, ?-fully_qualify) >>
123              
124             Serializes I<$object> into a hash reference.
125              
126             If the C<-fully_qualify> option is supplied , field keys are fully qualified.
127              
128             For example:
129              
130             package MyClass;
131             use FieldHash qw(:all);
132              
133             fieldhash my %foo => 'foo';
134              
135             sub new{
136             my $class = shift;
137             my $self = bless {}, $class;
138             return from_hash($self, @_);
139             }
140              
141             package MyDerivedClass;
142             use parent -norequire => 'MyClass';
143             use FieldHash qw(:all);
144              
145             fieldhash my %bar => 'bar';
146              
147             package main;
148              
149             my $o = MyDerivedClass->new(foo => 10, bar => 20);
150             my $p = MyDerivedClass->new('MyClass::foo' => 10, 'MyDerivedClass::bar' => 20);
151              
152             use Data::Dumper;
153             print Dumper($o->to_hash());
154             # $VAR1 = { foo => 10, bar => 20 }
155              
156             print Dumper($o->to_hash(-fully_qualify));
157             # $VAR1 = { 'MyClass::foo' => 10, 'MyDerived::bar' => 20 }
158              
159             =back
160              
161             =head1 ROBUSTNESS
162              
163             =head2 Thread support
164              
165             As C<Hash::Util::FieldHash> does, C<Hash::FieldHash> fully supports threading
166             using the C<CLONE> method.
167              
168             =head2 Memory leaks
169              
170             C<Hash::FieldHash> itself does not leak memory, but it may leak memory when
171             you uses hash references as field hash keys because of an issue of perl 5.10.0.
172              
173             =head1 NOTES
174              
175             =head2 The type of field hash keys
176              
177             C<Hash::FieldHash> accepts only references and registered addresses as its
178             keys, whereas C<Hash::Util::FieldHash> accepts any type of scalars.
179              
180             According to L<Hash::Util::FieldHash/"The Generic Object">,
181             Non-reference keys in C<H::U::F> are used for class fields. That is,
182             all the fields defined by C<H::U::F> act as both object fields and class fields
183             by default. It seems confusing; if you do not want them to be class fields,
184             you must check the type of I<$self> explicitly. In addition,
185             these class fields are never inherited.
186             This behavior seems problematic, so C<Hash::FieldHash>
187             restricts the type of keys.
188              
189             =head2 The ID of field hash keys
190              
191             While C<Hash::Util::FieldHash> uses C<refaddr> as the IDs of field
192             hash keys, C<Hash::FieldHash> allocates arbitrary integers as the
193             IDs.
194              
195             =head2 What accessors return
196              
197             The accessors C<fieldhash()> creates are B<chainable> accessors.
198             That is, it returns the I<$object> (i.e. C<$self>) with a parameter,
199             where as it returns the I<$value> without it.
200              
201             For example:
202              
203             my $o = YourClass->new();
204             $o->foo(42); # returns $o itself
205             my $value = $o->foo(); # retuns 42
206              
207             =head1 DEPENDENCIES
208              
209             Perl 5.8.5 or later, and a C compiler.
210              
211             =head1 BUGS
212              
213             No bugs have been reported.
214              
215             Please report any bugs or feature requests to the author.
216              
217             =head1 SEE ALSO
218              
219             L<Hash::Util::FieldHash>.
220              
221             L<Hash::Util::FieldHash::Compat>.
222              
223             L<perlguts/"Magic Virtual Tables">.
224              
225             L<Class::Std> describes the inside-out technique.
226              
227             =head1 AUTHOR
228              
229             Fuji, Goro (gfx) E<lt>gfuji(at)cpan.orgE<gt>.
230              
231             =head1 LICENSE AND COPYRIGHT
232              
233             Copyright (c) 2009-2010, Fuji, Goro. All rights reserved.
234              
235             This library is free software; you can redistribute it and/or modify
236             it under the same terms as Perl itself.
237              
238             =cut