Skip to content

Instantly share code, notes, and snippets.

@tokuhirom
Last active December 15, 2015 15:38
Show Gist options
  • Save tokuhirom/5283087 to your computer and use it in GitHub Desktop.
Save tokuhirom/5283087 to your computer and use it in GitHub Desktop.
Perl5 Boolean Value Serializer Protocol(PBVSP)

Perl5 Boolean Value Serializer Protocol(PBVSP)

Status

Draft. Plase post a comment on gist.

Why?

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.

Why do you write protocol instead of implementation?

There is a lot of implementations on CPAN, already! These modules can't switch to new module, I guess.

Solution

Determine the boolean value communication protoocol.

Spec

The boolean value

Types

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"

Overload bool operator

And the value MUST overload bool operator. True value should return true on !!$value. False value should return on !$value.

How serializer works

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.

How deserializer works

You can bless the boolean value to your favorite boolean class supports PBVSP.

FAQ

How do Data::Dumper serialize PBVSP object?

A. Data::Dumper should process PBVSP object as plain Perl object.

Where is a reference implementation?

Please look https://gist.github.com/tokuhirom/5284038

How can I implement is_bool in XS?

You can use this code(Thanks to gfx++)

https://github.com/tokuhirom/Acme-Bool/blob/master/lib/Acme/Bool.xs#L20

AUTHOR

Tokuhiro Matsuno

CONTRIBUTORS

  • Kenichi Ishigaki
  • Tatsuhiko Miyagawa
  • Goro Fuji X
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment