File Coverage

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


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