Debian Codegraph @

README

raw
1 NAME
2 Hash::DefHash - Manipulate defhash
3
4 VERSION
5 This document describes version 0.072 of Hash::DefHash (from Perl
6 distribution Hash-DefHash), released on 2021-07-21.
7
8 SYNOPSIS
9 use Hash::DefHash; # imports defhash()
10
11 # create a new defhash object, die when hash is invalid defhash
12 $dh = Hash::DefHash->new; # creates an empty defhash
13 $dh = Hash::DefHash->new({a=>1}); # use the hashref
14 $dh = Hash::DefHash->new({"contains space"=>1}); # dies!
15
16 # defhash() is a synonym for Hash::DefHash->new().
17 $dh = defhash({foo=>1});
18
19 # return the original hash
20 $hash = $dh->hash;
21
22 # list properties
23 @props = $dh->props;
24
25 # list property names, values, and attributes, will return ($prop => $attrs,
26 # ...). Property values will be put in $attrs with key "". For example:
27 %content = DefHash::Hash->new({p1=>1, "p1.a"=>2, p2=>3})->contents;
28 # => (p1 => {""=>1, a=>2}, p2=>3)
29
30 # get property value, will die if property does not exist
31 $propval = $dh->prop($prop);
32
33 # like prop(), but will return undef if property does not exist
34 $propval = $dh->get_prop($prop);
35
36 # check whether property exists
37 say "exists" if $dh->prop_exists($prop);
38
39 # add a new property, will die if property already exists
40 $dh->add_prop($prop, $propval);
41
42 # add new property, or set value for existing property
43 $oldpropval = $dh->set_prop($prop, $propval);
44
45 # delete property, noop if property already does not exist. set $delattrs to
46 # true to delete all property's attributes.
47 $oldpropval = $dh->del_prop($prop, $delattrs);
48
49 # delete all properties, set $delattrs to true to delete all properties's
50 # attributes too.
51 $dh->del_all_props($delattrs);
52
53 # get property's attributes. to list defhash attributes, set $prop to undef or
54 # ""
55 %attrs = $dh->attrs($prop);
56
57 # get attribute value, will die if attribute does not exist
58 $attrval = $dh->attr($prop, $attr);
59
60 # like attr(), but will return undef if attribute does not exist
61 $attrval = $dh->get_attr($prop, $attr);
62
63 # check whether an attribute exists
64 @attrs = $dh->attr_exists($prop, $attr);
65
66 # add attribute to a property, will die if attribute already exists
67 $dh->add_attr($prop, $attr, $attrval);
68
69 # add attribute to a property, or set value of existing attribute
70 $oldatrrval = $dh->set_attr($prop, $attr, $attrval);
71
72 # delete property's attribute, noop if attribute already does not exist
73 $oldattrval = $dh->del_attr($prop, $attr, $attrval);
74
75 # delete all attributes of a property
76 $dh->del_all_attrs($prop);
77
78 # get predefined properties
79 say $dh->v; # shortcut for $dh->get_prop('v')
80 say $dh->default_lang; # shortcut for $dh->get_prop('default_lang')
81 say $dh->name; # shortcut for $dh->get_prop('name')
82 say $dh->summary; # shortcut for $dh->get_prop('summary')
83 say $dh->description; # shortcut for $dh->get_prop('description')
84 say $dh->tags; # shortcut for $dh->get_prop('tags')
85
86 # get value in alternate languages
87 $propval = $dh->get_prop_lang($prop, $lang);
88
89 # get value in all available languages, result is a hash mapping lang => val
90 %vals = $dh->get_prop_all_langs($prop);
91
92 # set value for alternative language
93 $oldpropval = $dh->set_prop_lang($prop, $lang, $propval);
94
95 CONTRIBUTOR
96 Steven Haryanto <[email protected]>
97
98 FUNCTIONS
99 defhash([ $hash ]) => OBJ
100 Shortcut for "Hash::DefHash->new($hash)". As a bonus, can also detect if
101 $hash is already a defhash and returns it immediately instead of
102 wrapping it again. Exported by default.
103
104 METHODS
105 new
106 Usage:
107
108 $dh = Hash::DefHash->new([ $hash ],[ %opts ]);
109
110 Constructor. Create a new Hash::DefHash object, which is a thin OO skin
111 over the regular Perl hash. If $hash is not specified, a new anonymous
112 hash is created.
113
114 Internally, the object contains a hash reference which contains
115 reference to the hash ("bless({hash=>$orig_hash, ...},
116 'Hash::DefHash')"). It does not create a copy of the hash or bless the
117 hash directly. Be careful not to assume that the two are the same!
118
119 Will check the keys of hash for invalid properties/attributes and will
120 die if one is found, e.g..
121
122 $dh = Hash::DefHash->new({"contains space" => 1}); # dies!
123
124 Known options:
125
126 * check => BOOL (default: 1)
127
128 Whether to check that hash is a valid defhash. Will die if hash
129 turns out to contain invalid keys/values.
130
131 * parent => HASH/DEFHASH_OBJ
132
133 Set defhash's parent. Default language ("default_lang") will follow
134 parent's if unset in the current hash.
135
136 hash
137 Usage:
138
139 $hashref = $dh->hash;
140
141 Return the original hashref.
142
143 check
144 Usage:
145
146 $dh->check;
147
148 contents
149 Usage:
150
151 my %contents = $dh->contents;
152
153 default_lang
154 Usage:
155
156 $default_lang = $dh->default_lang;
157
158 props
159 Usage:
160
161 @props = $dh->props;
162
163 Return list of properties. Will ignore properties that begin with
164 underscore, e.g.:
165
166 $dh = defhash({a=>1, _b=>2});
167 $dh->props;
168
169 prop
170 Usage:
171
172 $val = $dh->prop($prop [ , \%opts ]);
173
174 Get property value, will die if property does not exist.
175
176 Known options:
177
178 * die
179
180 Bool. Default true. Whether to die when requested property is not
181 found.
182
183 * alt
184
185 Hashref.
186
187 * mark_different_lang
188
189 Bool. Default false. If set to true, then when a requested property
190 is found but differs (only) in the language it will be returned but
191 with a mark. For example, with this defhash:
192
193 {name=>"Chair", "name.alt.lang.id_ID"=>"Kursi"}
194
195 then:
196
197 $dh->prop("name", {lang=>"fr_FR"});
198
199 will die. But:
200
201 $dh->prop("name", {lang=>"fr_FR", mark_different_lang=>1});
202
203 will return:
204
205 "{en_US Chair}"
206
207 or:
208
209 "{id_ID Kursi}"
210
211 get_prop
212 Usage:
213
214 my $val = $dh->get_prop($prop [ , \%opts ]);
215
216 Like "prop"(), but will return undef if property does not exist.
217
218 prop_exists
219 Usage:
220
221 $exists = $dh->prop_exists;
222
223 add_prop
224 set_prop
225 del_prop
226 del_all_props
227 attrs
228 attr
229 get_attr
230 attr_exists
231 add_attr
232 set_attr
233 del_attr
234 del_all_attrs
235 defhash_v
236 v
237 name
238 summary
239 description
240 tags
241 get_prop_lang
242 Usage:
243
244 my $val = $dh->get_prop_lang($prop, $lang [ , \%opts ]);
245
246 This is just a special case for:
247
248 $dh->prop($prop, {alt=>{lang=>$lang}, mark_different_lang=>1, %opts});
249
250 get_prop_all_langs
251 set_prop_lang
252 HOMEPAGE
253 Please visit the project's homepage at
254 <https://metacpan.org/release/Hash-DefHash>.
255
256 SOURCE
257 Source repository is at
258 <https://github.com/perlancar/perl-Hash-DefHash>.
259
260 BUGS
261 Please report any bugs or feature requests on the bugtracker website
262 <https://rt.cpan.org/Public/Dist/Display.html?Name=Hash-DefHash>
263
264 When submitting a bug or request, please include a test-file or a patch
265 to an existing test-file that illustrates the bug or desired feature.
266
267 SEE ALSO
268 DefHash specification
269
270 AUTHOR
271 perlancar <[email protected]>
272
273 COPYRIGHT AND LICENSE
274 This software is copyright (c) 2021, 2020, 2018, 2016, 2015, 2014, 2012
275 by [email protected].
276
277 This is free software; you can redistribute it and/or modify it under
278 the same terms as the Perl 5 programming language system itself.
279