… | |
… | |
191 | sharing extension. This also makes it possible to encode cyclic data |
191 | sharing extension. This also makes it possible to encode cyclic data |
192 | structures. |
192 | structures. |
193 | |
193 | |
194 | It is recommended to leave it off unless you know your |
194 | It is recommended to leave it off unless you know your |
195 | communication partner supports the value sharing extensions to CBOR |
195 | communication partner supports the value sharing extensions to CBOR |
196 | (http://cbor.schmorp.de/value-sharing), as without decoder support, the |
196 | (L<http://cbor.schmorp.de/value-sharing>), as without decoder support, the |
197 | resulting data structure might be unusable. |
197 | resulting data structure might be unusable. |
198 | |
198 | |
199 | Detecting shared values incurs a runtime overhead when values are encoded |
199 | Detecting shared values incurs a runtime overhead when values are encoded |
200 | that have a reference counter large than one, and might unnecessarily |
200 | that have a reference counter large than one, and might unnecessarily |
201 | increase the encoded size, as potentially shared values are encode as |
201 | increase the encoded size, as potentially shared values are encode as |
… | |
… | |
224 | also results in a very large runtime overhead (expect encoding times to be |
224 | also results in a very large runtime overhead (expect encoding times to be |
225 | 2-4 times as high as without). |
225 | 2-4 times as high as without). |
226 | |
226 | |
227 | It is recommended to leave it off unless you know your |
227 | It is recommended to leave it off unless you know your |
228 | communications partner supports the stringref extension to CBOR |
228 | communications partner supports the stringref extension to CBOR |
229 | (http://cbor.schmorp.de/stringref), as without decoder support, the |
229 | (L<http://cbor.schmorp.de/stringref>), as without decoder support, the |
230 | resulting data structure might not be usable. |
230 | resulting data structure might not be usable. |
231 | |
231 | |
232 | If C<$enable> is false (the default), then C<encode> will encode strings |
232 | If C<$enable> is false (the default), then C<encode> will encode strings |
233 | the standard CBOR way. |
233 | the standard CBOR way. |
234 | |
234 | |
… | |
… | |
323 | CBOR integers become (numeric) perl scalars. On perls without 64 bit |
323 | CBOR integers become (numeric) perl scalars. On perls without 64 bit |
324 | support, 64 bit integers will be truncated or otherwise corrupted. |
324 | support, 64 bit integers will be truncated or otherwise corrupted. |
325 | |
325 | |
326 | =item byte strings |
326 | =item byte strings |
327 | |
327 | |
328 | Byte strings will become octet strings in Perl (the byte values 0..255 |
328 | Byte strings will become octet strings in Perl (the Byte values 0..255 |
329 | will simply become characters of the same value in Perl). |
329 | will simply become characters of the same value in Perl). |
330 | |
330 | |
331 | =item UTF-8 strings |
331 | =item UTF-8 strings |
332 | |
332 | |
333 | UTF-8 strings in CBOR will be decoded, i.e. the UTF-8 octets will be |
333 | UTF-8 strings in CBOR will be decoded, i.e. the UTF-8 octets will be |
… | |
… | |
424 | # dump as number |
424 | # dump as number |
425 | encode_cbor [2] # yields [2] |
425 | encode_cbor [2] # yields [2] |
426 | encode_cbor [-3.0e17] # yields [-3e+17] |
426 | encode_cbor [-3.0e17] # yields [-3e+17] |
427 | my $value = 5; encode_cbor [$value] # yields [5] |
427 | my $value = 5; encode_cbor [$value] # yields [5] |
428 | |
428 | |
429 | # used as string, so dump as string |
429 | # used as string, so dump as string (either byte or text) |
430 | print $value; |
430 | print $value; |
431 | encode_cbor [$value] # yields ["5"] |
431 | encode_cbor [$value] # yields ["5"] |
432 | |
432 | |
433 | # undef becomes null |
433 | # undef becomes null |
434 | encode_cbor [undef] # yields [null] |
434 | encode_cbor [undef] # yields [null] |
… | |
… | |
437 | |
437 | |
438 | my $x = 3.1; # some variable containing a number |
438 | my $x = 3.1; # some variable containing a number |
439 | "$x"; # stringified |
439 | "$x"; # stringified |
440 | $x .= ""; # another, more awkward way to stringify |
440 | $x .= ""; # another, more awkward way to stringify |
441 | print $x; # perl does it for you, too, quite often |
441 | print $x; # perl does it for you, too, quite often |
|
|
442 | |
|
|
443 | You can force whether a string ie encoded as byte or text string by using |
|
|
444 | C<utf8::upgrade> and C<utf8::downgrade>): |
|
|
445 | |
|
|
446 | utf8::upgrade $x; # encode $x as text string |
|
|
447 | utf8::downgrade $x; # encode $x as byte string |
|
|
448 | |
|
|
449 | Perl doesn't define what operations up- and downgrade strings, so if the |
|
|
450 | difference between byte and text is important, you should up- or downgrade |
|
|
451 | your string as late as possible before encoding. |
442 | |
452 | |
443 | You can force the type to be a CBOR number by numifying it: |
453 | You can force the type to be a CBOR number by numifying it: |
444 | |
454 | |
445 | my $x = "3"; # some variable containing a string |
455 | my $x = "3"; # some variable containing a string |
446 | $x += 0; # numify it, ensuring it will be dumped as a number |
456 | $x += 0; # numify it, ensuring it will be dumped as a number |
… | |
… | |
679 | These tags are always handled when decoding, and their handling cannot be |
689 | These tags are always handled when decoding, and their handling cannot be |
680 | overriden by the user. |
690 | overriden by the user. |
681 | |
691 | |
682 | =over 4 |
692 | =over 4 |
683 | |
693 | |
684 | =item <unassigned> (perl-object, L<http://cbor.schmorp.de/perl-object>) |
694 | =item 26 (perl-object, L<http://cbor.schmorp.de/perl-object>) |
685 | |
695 | |
686 | These tags are automatically created (and decoded) for serialisable |
696 | These tags are automatically created (and decoded) for serialisable |
687 | objects using the C<FREEZE/THAW> methods (the L<Types::Serialier> object |
697 | objects using the C<FREEZE/THAW> methods (the L<Types::Serialier> object |
688 | serialisation protocol). See L<OBJECT SERIALISATION> for details. |
698 | serialisation protocol). See L<OBJECT SERIALISATION> for details. |
689 | |
699 | |
690 | =item <unassigned>, <unassigned> (sharable, sharedref, L <http://cbor.schmorp.de/value-sharing>) |
700 | =item 28, 29 (sharable, sharedref, L <http://cbor.schmorp.de/value-sharing>) |
691 | |
701 | |
692 | These tags are automatically decoded when encountered, resulting in |
702 | These tags are automatically decoded when encountered, resulting in |
693 | shared values in the decoded object. They are only encoded, however, when |
703 | shared values in the decoded object. They are only encoded, however, when |
694 | C<allow_sharable> is enabled. |
704 | C<allow_sharable> is enabled. |
695 | |
705 | |
696 | =item <unassigned>, <unassigned> (stringref-namespace, stringref, L <http://cbor.schmorp.de/stringref>) |
706 | =item 256, 25 (stringref-namespace, stringref, L <http://cbor.schmorp.de/stringref>) |
697 | |
707 | |
698 | These tags are automatically decoded when encountered. They are only |
708 | These tags are automatically decoded when encountered. They are only |
699 | encoded, however, when C<pack_strings> is enabled. |
709 | encoded, however, when C<pack_strings> is enabled. |
700 | |
710 | |
701 | =item 22098 (indirection, L<http://cbor.schmorp.de/indirection>) |
711 | =item 22098 (indirection, L<http://cbor.schmorp.de/indirection>) |