… | |
… | |
71 | |
71 | |
72 | package CBOR::XS; |
72 | package CBOR::XS; |
73 | |
73 | |
74 | use common::sense; |
74 | use common::sense; |
75 | |
75 | |
76 | our $VERSION = 0.08; |
76 | our $VERSION = 0.09; |
77 | our @ISA = qw(Exporter); |
77 | our @ISA = qw(Exporter); |
78 | |
78 | |
79 | our @EXPORT = qw(encode_cbor decode_cbor); |
79 | our @EXPORT = qw(encode_cbor decode_cbor); |
80 | |
80 | |
81 | use Exporter; |
81 | use Exporter; |
… | |
… | |
233 | |
233 | |
234 | =item $cbor = $cbor->filter ([$cb->($tag, $value)]) |
234 | =item $cbor = $cbor->filter ([$cb->($tag, $value)]) |
235 | |
235 | |
236 | =item $cb_or_undef = $cbor->get_filter |
236 | =item $cb_or_undef = $cbor->get_filter |
237 | |
237 | |
238 | TODO |
238 | Sets or replaces the tagged value decoding filter (when C<$cb> is |
|
|
239 | specified) or clears the filter (if no argument or C<undef> is provided). |
|
|
240 | |
|
|
241 | The filter callback is called only during decoding, when a non-enforced |
|
|
242 | tagged value has been decoded (see L<TAG HANDLING AND EXTENSIONS> for a |
|
|
243 | list of enforced tags). For specific tags, it's often better to provide a |
|
|
244 | default converter using the C<%CBOR::XS::FILTER> hash (see below). |
|
|
245 | |
|
|
246 | The first argument is the numerical tag, the second is the (decoded) value |
|
|
247 | that has been tagged. |
|
|
248 | |
|
|
249 | The filter function should return either exactly one value, which will |
|
|
250 | replace the tagged value in the decoded data structure, or no values, |
|
|
251 | which will result in default handling, which currently means the decoder |
|
|
252 | creates a C<CBOR::XS::Tagged> object to hold the tag and the value. |
|
|
253 | |
|
|
254 | When the filter is cleared (the default state), the default filter |
|
|
255 | function, C<CBOR::XS::default_filter>, is used. This function simply looks |
|
|
256 | up the tag in the C<%CBOR::XS::FILTER> hash. If an entry exists it must be |
|
|
257 | a code reference that is called with tag and value, and is responsible for |
|
|
258 | decoding the value. If no entry exists, it returns no values. |
|
|
259 | |
|
|
260 | Example: decode all tags not handled internally into CBOR::XS::Tagged |
|
|
261 | objects, with no other special handling (useful when working with |
|
|
262 | potentially "unsafe" CBOR data). |
|
|
263 | |
|
|
264 | CBOR::XS->new->filter (sub { })->decode ($cbor_data); |
|
|
265 | |
|
|
266 | Example: provide a global filter for tag 1347375694, converting the value |
|
|
267 | into some string form. |
|
|
268 | |
|
|
269 | $CBOR::XS::FILTER{1347375694} = sub { |
|
|
270 | my ($tag, $value); |
|
|
271 | |
|
|
272 | "tag 1347375694 value $value" |
|
|
273 | }; |
239 | |
274 | |
240 | =item $cbor_data = $cbor->encode ($perl_scalar) |
275 | =item $cbor_data = $cbor->encode ($perl_scalar) |
241 | |
276 | |
242 | Converts the given Perl data structure (a scalar value) to its CBOR |
277 | Converts the given Perl data structure (a scalar value) to its CBOR |
243 | representation. |
278 | representation. |
… | |
… | |
670 | This value is not generated on encoding (unless explicitly requested by |
705 | This value is not generated on encoding (unless explicitly requested by |
671 | the user), and is simply ignored when decoding. |
706 | the user), and is simply ignored when decoding. |
672 | |
707 | |
673 | =back |
708 | =back |
674 | |
709 | |
675 | =head2 OPTIONAL TAGS |
710 | =head2 NON-ENFORCED TAGS |
676 | |
711 | |
677 | These tags have default filters provided when decoding. Their handling can |
712 | These tags have default filters provided when decoding. Their handling can |
678 | be overriden by changing the C<%CBOR::XS::FILTER> entry for the tag, or by |
713 | be overriden by changing the C<%CBOR::XS::FILTER> entry for the tag, or by |
679 | providing a custom C<filter> function when decoding. |
714 | providing a custom C<filter> callback when decoding. |
680 | |
715 | |
681 | When they result in decoding into a specific Perl class, the module |
716 | When they result in decoding into a specific Perl class, the module |
682 | usually provides a corresponding C<TO_CBOR> method as well. |
717 | usually provides a corresponding C<TO_CBOR> method as well. |
683 | |
718 | |
684 | When any of these need to load additional modules that are not part of the |
719 | When any of these need to load additional modules that are not part of the |