… | |
… | |
15 | |
15 | |
16 | =cut |
16 | =cut |
17 | |
17 | |
18 | package Types::Serialiser; |
18 | package Types::Serialiser; |
19 | |
19 | |
|
|
20 | use common::sense; # required to suppress annoying warnings |
|
|
21 | |
20 | our $VERSION = 0.01; |
22 | our $VERSION = 0.02; |
21 | |
23 | |
22 | =head1 SIMPLE SCALAR CONSTANTS |
24 | =head1 SIMPLE SCALAR CONSTANTS |
23 | |
25 | |
24 | Simple scalar constants are values that are overloaded to act like simple |
26 | Simple scalar constants are values that are overloaded to act like simple |
25 | Perl values, but have (class) type to differentiate them from normal Perl |
27 | Perl values, but have (class) type to differentiate them from normal Perl |
… | |
… | |
95 | |
97 | |
96 | =back |
98 | =back |
97 | |
99 | |
98 | =cut |
100 | =cut |
99 | |
101 | |
|
|
102 | BEGIN { |
|
|
103 | # for historical reasons, and to avoid extra dependencies in JSON::PP, |
|
|
104 | # we alias *Types::Serialiser::Boolean with JSON::PP::Boolean. |
|
|
105 | package JSON::PP::Boolean; |
|
|
106 | *Types::Serialiser::Boolean:: = *JSON::PP::Boolean::; |
|
|
107 | } |
|
|
108 | |
100 | our $true = do { bless \(my $dummy = 1), Types::Serialiser::Boolean:: }; |
109 | our $true = do { bless \(my $dummy = 1), Types::Serialiser::Boolean:: }; |
101 | our $false = do { bless \(my $dummy = 0), Types::Serialiser::Boolean:: }; |
110 | our $false = do { bless \(my $dummy = 0), Types::Serialiser::Boolean:: }; |
102 | our $error = do { bless \(my $dummy ), Types::Serialiser::Error:: }; |
111 | our $error = do { bless \(my $dummy ), Types::Serialiser::Error:: }; |
103 | |
112 | |
104 | sub true () { $true } |
113 | sub true () { $true } |
… | |
… | |
108 | sub is_bool ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
117 | sub is_bool ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
109 | sub is_true ($) { $_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
118 | sub is_true ($) { $_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
110 | sub is_false ($) { !$_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
119 | sub is_false ($) { !$_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
111 | sub is_error ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Error:: } |
120 | sub is_error ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Error:: } |
112 | |
121 | |
113 | package Types::Serialiser::Boolean; |
122 | package Types::Serialiser::BooleanBase; |
114 | |
123 | |
115 | use overload |
124 | use overload |
116 | "0+" => sub { ${$_[0]} }, |
125 | "0+" => sub { ${$_[0]} }, |
117 | "++" => sub { $_[0] = ${$_[0]} + 1 }, |
126 | "++" => sub { $_[0] = ${$_[0]} + 1 }, |
118 | "--" => sub { $_[0] = ${$_[0]} - 1 }, |
127 | "--" => sub { $_[0] = ${$_[0]} - 1 }, |
119 | fallback => 1; |
128 | fallback => 1; |
120 | |
129 | |
|
|
130 | @Types::Serialiser::Boolean::ISA = Types::Serialiser::BooleanBase::; |
|
|
131 | |
121 | package Types::Serialiser::Error; |
132 | package Types::Serialiser::Error; |
122 | |
133 | |
123 | sub error { |
134 | sub error { |
124 | require Carp; |
135 | require Carp; |
125 | Carp::croak ("caught attempt to use the Types::Serialiser::error value"); |
136 | Carp::croak ("caught attempt to use the Types::Serialiser::error value"); |
… | |
… | |
139 | see if it's C<1> (true), C<0> (false) or C<undef> (error). |
150 | see if it's C<1> (true), C<0> (false) or C<undef> (error). |
140 | |
151 | |
141 | While it is possible to use an isa test, directly comparing stash pointers |
152 | While it is possible to use an isa test, directly comparing stash pointers |
142 | is faster and guaranteed to work. |
153 | is faster and guaranteed to work. |
143 | |
154 | |
|
|
155 | For historical reasons, the C<Types::Serialiser::Boolean> stash is |
|
|
156 | just an alias for C<JSON::PP::Boolean>. When printed, the classname |
|
|
157 | withh usually be C<JSON::PP::Boolean>, but isa tests and stash pointer |
|
|
158 | comparison will normally work correctly (i.e. Types::Serialiser::true ISA |
|
|
159 | JSON::PP::Boolean, but also ISA Types::Serialiser::Boolean). |
|
|
160 | |
144 | =head1 A GENERIC OBJECT SERIALIATION PROTOCOL |
161 | =head1 A GENERIC OBJECT SERIALIATION PROTOCOL |
145 | |
162 | |
146 | This section explains the object serialisation protocol used by |
163 | This section explains the object serialisation protocol used by |
147 | L<CBOR::XS>. It is meant to be generic enough to support any kind of |
164 | L<CBOR::XS>. It is meant to be generic enough to support any kind of |
148 | generic object serialiser. |
165 | generic object serialiser. |