ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.pm
(Generate patch)

Comparing CBOR-XS/XS.pm (file contents):
Revision 1.77 by root, Fri Dec 4 02:57:14 2020 UTC vs.
Revision 1.80 by root, Fri Dec 11 06:05:33 2020 UTC

64 64
65package CBOR::XS; 65package CBOR::XS;
66 66
67use common::sense; 67use common::sense;
68 68
69our $VERSION = 1.82; 69our $VERSION = 1.83;
70our @ISA = qw(Exporter); 70our @ISA = qw(Exporter);
71 71
72our @EXPORT = qw(encode_cbor decode_cbor); 72our @EXPORT = qw(encode_cbor decode_cbor);
73 73
74use Exporter; 74use Exporter;
712 712
713Type forcing is done by calling a special "cast" function which keeps a 713Type forcing is done by calling a special "cast" function which keeps a
714copy of the value and returns a new value that can be handed over to any 714copy of the value and returns a new value that can be handed over to any
715CBOR encoder function. 715CBOR encoder function.
716 716
717The following casts are currently available (all of which are unary operators): 717The following casts are currently available (all of which are unary
718operators, that is, have a prototype of C<$>):
718 719
719=over 720=over
720 721
721=item CBOR::XS::as_int $value 722=item CBOR::XS::as_int $value
722 723
765 766
766=item CBOR::XS::as_map [key => value...] 767=item CBOR::XS::as_map [key => value...]
767 768
768Treat the array reference as key value pairs and output a CBOR map. This 769Treat the array reference as key value pairs and output a CBOR map. This
769allows you to generate CBOR maps with arbitrary key types (or, if you 770allows you to generate CBOR maps with arbitrary key types (or, if you
770don't care about semantics, duplicate keys or prairs in a custom order), 771don't care about semantics, duplicate keys or pairs in a custom order),
771which is otherwise hard to do with Perl. 772which is otherwise hard to do with Perl.
772 773
773The single argument must be an array reference with an even number of 774The single argument must be an array reference with an even number of
774elements. 775elements.
776
777Note that only the reference to the array is copied, the array itself is
778not. Modifications done to the array before calling an encoding fuinction
779will be reflected in the encoded output.
775 780
776Example: encode a CBOR map with a string and an integer as keys. 781Example: encode a CBOR map with a string and an integer as keys.
777 782
778 encode_cbor CBOR::XS::as_map [string => "value", 5 => "value"] 783 encode_cbor CBOR::XS::as_map [string => "value", 5 => "value"]
779 784
792sub CBOR::XS::as_bool ($) { $_[0] ? $Types::Serialiser::true : $Types::Serialiser::false } 797sub CBOR::XS::as_bool ($) { $_[0] ? $Types::Serialiser::true : $Types::Serialiser::false }
793 798
794sub CBOR::XS::as_map ($) { 799sub CBOR::XS::as_map ($) {
795 ARRAY:: eq ref $_[0] 800 ARRAY:: eq ref $_[0]
796 and $#{ $_[0] } & 1 801 and $#{ $_[0] } & 1
797 or do { require Carp; Carp::croak ("CBOR::XS::as_map only acepts array references with an even number of elements, found ") }; 802 or do { require Carp; Carp::croak ("CBOR::XS::as_map only acepts array references with an even number of elements, caught") };
798 803
799 bless [$_[0], 7, undef], CBOR::XS::Tagged:: 804 bless [$_[0], 7, undef], CBOR::XS::Tagged::
800} 805}
801 806
802=head2 OBJECT SERIALISATION 807=head2 OBJECT SERIALISATION

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines