Draft. Plase post a comment on gist.
Most programming languages have a native Boolean data type. Perl does not.
Yet, when you need to serialize perl data structure into data formats like JSON, you need to figure out which variable represents boolean while others don't. Usually, this is done via defining special variables or modules such as JSON::true
or boolean.pm, but unfortunately these classes don't work well with each other.
I think if there is a protocol for boolean values, serializers can support other module's boolean value.
There is a lot of implementations on CPAN, already! These modules can't switch to new module, I guess.
Determine the boolean value communication protoocol.
Boolean value MUST be blessed Scalar reference. True value MAY be blessed scalar reference contains 1. False value MAY be blessed scalar reference contains 0.
my $true = bless \(do { my $x = 1 }), "Your::Boolean"
my $false = bless \(do { my $x = 0 }), "Your::Boolean"
And the value MUST overload bool
operator. True value should return true on !!$value
. False value should return on !$value
.
Serializer supporting PBVSP MUST serialize PBVSP value as boolean value. Serializer MUST serialize the value as boolean value if:
- The value is blessed scalar reference
- overload::Method($value, 'bool') returns true
And serializer MAY serialize \1
as true, \0
as false value.
You can bless the boolean value to your favorite boolean class supports PBVSP.
A. Data::Dumper should process PBVSP object as plain Perl object.
Please look https://gist.github.com/tokuhirom/5284038
You can use this code(Thanks to gfx++)
https://github.com/tokuhirom/Acme-Bool/blob/master/lib/Acme/Bool.xs#L20
Tokuhiro Matsuno
- Kenichi Ishigaki
- Tatsuhiko Miyagawa
- Goro Fuji X