Skip to content
Browse files

Released version 0.28

  • Loading branch information...
1 parent 51166a3 commit 6ba9b01d41139c3bec13559627b2cb30c8b9fd20 @ingydotnet committed Sep 12, 2011
Showing with 102 additions and 7 deletions.
  1. +8 −0 Changes
  2. +1 −1 Makefile.PL
  3. +21 −1 README
  4. +44 −4 lib/boolean.pm
  5. +1 −1 t/export.t
  6. +27 −0 t/truth.t
View
8 Changes
@@ -1,4 +1,12 @@
---
+version: 0.28
+date: Mon Sep 12 13:51:47 CEST 2011
+changes:
+- Added -truth option.
+- This idea was inspired by https://rt.cpan.org/Ticket/Display.html?id=65536
+- Awesome idea!
+- Released from Liz++&Wendy++ downstairs bathroom
+---
version: 0.27
date: Tue Jun 28 10:47:01 EDT 2011
changes:
View
2 Makefile.PL
@@ -1,3 +1,3 @@
-use inc::Module::Package 'Ingy:modern 0.13';
+use inc::Module::Package 'Ingy:modern 0.15';
recommends 'Readonly';
View
22 README
@@ -18,6 +18,13 @@ SYNOPSIS
do &something if isTrue($guess);
do &something_else if isFalse($guess);
+ and:
+
+ use boolean -truth;
+
+ die unless ref(42 == 42) eq 'boolean';
+ die unless ("foo" =~ /bar/) eq '0';
+
DESCRIPTION
Most programming languages have a native "Boolean" data type. Perl does
not.
@@ -88,7 +95,7 @@ METHODS
$boolean->isFalse
Same as isFalse($boolean).
-EXPORTABLES
+USE OPTIONS
By default this module exports the "true", "false" and "boolean"
functions.
@@ -97,6 +104,19 @@ EXPORTABLES
:all
Exports "true", "false", "boolean", "isTrue", "isFalse", "isBoolean"
+ -truth
+ You can specify the "-truth" option to override truth operators to
+ return "boolean" values.
+
+ use boolean -truth;
+ print ref("hello" eq "world"), "\n";
+
+ Prints:
+
+ boolean
+
+ "-truth" can be used with the other import options.
+
AUTHOR
Ingy döt Net <ingy@cpan.org>
View
48 lib/boolean.pm
@@ -1,8 +1,8 @@
-package boolean;
use 5.005003;
+package boolean;
use strict;
-# use warnings;
-$boolean::VERSION = '0.27';
+
+$boolean::VERSION = '0.28';
my ($true, $false);
@@ -19,6 +19,13 @@ use base 'Exporter';
test => [qw(isTrue isFalse isBoolean)],
);
+sub import {
+ my @options = grep $_ ne '-truth', @_;
+ $_[0]->truth if @options != @_;
+ @_ = @options;
+ goto &Exporter::import;
+}
+
my ($true_val, $false_val, $bool_vals);
BEGIN {
@@ -60,6 +67,18 @@ sub isBoolean($) {
(exists $bool_vals->{overload::StrVal($_[0])}) ? true : false;
}
+sub truth {
+ # enable modifying true and false
+ &Internals::SvREADONLY( \ !!0, 0);
+ &Internals::SvREADONLY( \ !!1, 0);
+ # turn perl internal booleans into blessed booleans:
+ ${ \ !!0 } = $false;
+ ${ \ !!1 } = $true;
+ # make true and false read-only again
+ &Internals::SvREADONLY( \ !!0, 1);
+ &Internals::SvREADONLY( \ !!1, 1);
+}
+
1;
=encoding utf8
@@ -86,6 +105,13 @@ and:
do &something if isTrue($guess);
do &something_else if isFalse($guess);
+and:
+
+ use boolean -truth;
+
+ die unless ref(42 == 42) eq 'boolean';
+ die unless ("foo" =~ /bar/) eq '0';
+
=head1 DESCRIPTION
Most programming languages have a native C<Boolean> data type.
@@ -174,7 +200,7 @@ Same as isFalse($boolean).
=back
-=head1 EXPORTABLES
+=head1 USE OPTIONS
By default this module exports the C<true>, C<false> and C<boolean> functions.
@@ -188,6 +214,20 @@ Exports C<true>, C<false>, C<boolean>, C<isTrue>, C<isFalse>, C<isBoolean>
=back
+=head2 -truth
+
+You can specify the C<-truth> option to override truth operators to return
+C<boolean> values.
+
+ use boolean -truth;
+ print ref("hello" eq "world"), "\n";
+
+Prints:
+
+ boolean
+
+C<-truth> can be used with the other import options.
+
=head1 AUTHOR
Ingy döt Net <ingy@cpan.org>
View
2 t/export.t
@@ -13,7 +13,7 @@ package All;
use boolean ':all';
package Test;
-use boolean ':test';
+use boolean -truth, ':test';
package main;
View
27 t/truth.t
@@ -0,0 +1,27 @@
+use Test::More tests => 8;
+
+use boolean -truth;
+
+is ref(1 == 0), 'boolean',
+ 'Turn false == value into boolean';
+
+is ref(1 == 1), 'boolean',
+ 'Turn true == value into boolean';
+
+is ref("foo" eq "bar"), 'boolean',
+ 'Turn false "eq" value into boolean';
+
+is ref("foo" ne "bar"), 'boolean',
+ 'Turn true "ne" value into boolean';
+
+is ref("foo" =~ /o/), 'boolean',
+ 'Turn true regex into boolean';
+
+is ref("foo" =~ /bar/), 'boolean',
+ 'Turn false regex into boolean';
+
+is 1 == 0, '0',
+ 'False is 0';
+
+is 1 == 1, '1',
+ 'True is 1';

0 comments on commit 6ba9b01

Please sign in to comment.
Something went wrong with that request. Please try again.