… | |
… | |
17 | |
17 | |
18 | package Types::Serialiser; |
18 | package Types::Serialiser; |
19 | |
19 | |
20 | use common::sense; # required to suppress annoying warnings |
20 | use common::sense; # required to suppress annoying warnings |
21 | |
21 | |
22 | our $VERSION = 0.02; |
22 | our $VERSION = 0.03; |
23 | |
23 | |
24 | =head1 SIMPLE SCALAR CONSTANTS |
24 | =head1 SIMPLE SCALAR CONSTANTS |
25 | |
25 | |
26 | 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 |
27 | 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 |
… | |
… | |
101 | |
101 | |
102 | BEGIN { |
102 | BEGIN { |
103 | # for historical reasons, and to avoid extra dependencies in JSON::PP, |
103 | # for historical reasons, and to avoid extra dependencies in JSON::PP, |
104 | # we alias *Types::Serialiser::Boolean with JSON::PP::Boolean. |
104 | # we alias *Types::Serialiser::Boolean with JSON::PP::Boolean. |
105 | package JSON::PP::Boolean; |
105 | package JSON::PP::Boolean; |
|
|
106 | |
106 | *Types::Serialiser::Boolean:: = *JSON::PP::Boolean::; |
107 | *Types::Serialiser::Boolean:: = *JSON::PP::Boolean::; |
|
|
108 | } |
|
|
109 | |
|
|
110 | { |
|
|
111 | # this must done before blessing to work around bugs |
|
|
112 | # in perl < 5.18 (it seems to be fixed in 5.18). |
|
|
113 | package Types::Serialiser::BooleanBase; |
|
|
114 | |
|
|
115 | use overload |
|
|
116 | "0+" => sub { ${$_[0]} }, |
|
|
117 | "++" => sub { $_[0] = ${$_[0]} + 1 }, |
|
|
118 | "--" => sub { $_[0] = ${$_[0]} - 1 }, |
|
|
119 | fallback => 1; |
|
|
120 | |
|
|
121 | @Types::Serialiser::Boolean::ISA = Types::Serialiser::BooleanBase::; |
107 | } |
122 | } |
108 | |
123 | |
109 | our $true = do { bless \(my $dummy = 1), Types::Serialiser::Boolean:: }; |
124 | our $true = do { bless \(my $dummy = 1), Types::Serialiser::Boolean:: }; |
110 | our $false = do { bless \(my $dummy = 0), Types::Serialiser::Boolean:: }; |
125 | our $false = do { bless \(my $dummy = 0), Types::Serialiser::Boolean:: }; |
111 | our $error = do { bless \(my $dummy ), Types::Serialiser::Error:: }; |
126 | our $error = do { bless \(my $dummy ), Types::Serialiser::Error:: }; |
… | |
… | |
116 | |
131 | |
117 | sub is_bool ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
132 | sub is_bool ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
118 | sub is_true ($) { $_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
133 | sub is_true ($) { $_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
119 | sub is_false ($) { !$_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
134 | sub is_false ($) { !$_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
120 | sub is_error ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Error:: } |
135 | sub is_error ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Error:: } |
121 | |
|
|
122 | package Types::Serialiser::BooleanBase; |
|
|
123 | |
|
|
124 | use overload |
|
|
125 | "0+" => sub { ${$_[0]} }, |
|
|
126 | "++" => sub { $_[0] = ${$_[0]} + 1 }, |
|
|
127 | "--" => sub { $_[0] = ${$_[0]} - 1 }, |
|
|
128 | fallback => 1; |
|
|
129 | |
|
|
130 | @Types::Serialiser::Boolean::ISA = Types::Serialiser::BooleanBase::; |
|
|
131 | |
136 | |
132 | package Types::Serialiser::Error; |
137 | package Types::Serialiser::Error; |
133 | |
138 | |
134 | sub error { |
139 | sub error { |
135 | require Carp; |
140 | require Carp; |