… | |
… | |
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 |