On Wed, Jan 21, 2004 at 07:48:21PM +0200, Gaal Yahas wrote:
> 8.2 is not TRUE as Andrew defined it, but it is true under Perl's idea
> of truth. That is in fact my point: using the non-native notion of what
> truth is, you can't use any code from other people because you can't
> assume it returns what you expect to be called true to signal truth.
Somebody had to do it. . . .
#!/usr/bin/perl -w
use strict;
BEGIN
{
package True;
use overload
'eq' => \&equals,
'==' => \&equals,
bool => sub { !!1 },
'!' => sub { False->new() },
;
use base 'Exporter';
our @EXPORT = qw( &TRUE );
sub new
{
my ($class) = @_;
my $self = 1;
return bless \$self, $class;
}
sub TRUE
{
return True->new();
}
sub equals
{
my ($x, $y, $swap) = @_;
$y ? True->new() : False->new();
}
}
BEGIN
{
package False;
use overload
'eq' => \&equals,
'==' => \&equals,
bool => sub { !!0 },
'!' => sub { True->new() },
;
use base 'Exporter';
our @EXPORT = qw( FALSE );
sub new
{
my ($class) = @_;
my $self = 0;
return bless \$self, $class;
}
sub FALSE
{
return False->new();
}
sub equals
{
my ($x, $y, $swap) = @_;
return $y ? False->new() : True->new();
}
}
BEGIN
{
import True;
import False;
}
my $n;
BEGIN { $n = 0 }
use Test;
plan tests => $n;
BEGIN { $n += 2 }
ok TRUE;
ok not FALSE;
BEGIN { $n += 4 }
ok not TRUE == FALSE;
ok not TRUE eq FALSE;
ok not FALSE == TRUE;
ok not FALSE eq TRUE;
BEGIN { $n += 4 }
{
my $a = "true";
ok $a;
ok $a == TRUE;
ok $a eq TRUE;
ok $a, TRUE;
}
BEGIN { $n += 4 }
{
my $b = 0;
ok !$b;
ok $b eq FALSE;
ok $b == FALSE;
ok $b, FALSE;
}
BEGIN { $n += 1 }
{
my $c;
if ($c) { ok $c == TRUE }
else { ok $c == FALSE }
}
BEGIN { $n += 8 }
{
ok TRUE == TRUE;
ok FALSE == FALSE;
ok FALSE == FALSE; TRUE;
ok TRUE == FALSE, FALSE;
ok FALSE == TRUE, FALSE;
ok((TRUE == TRUE) == TRUE);
ok((FALSE == FALSE) == TRUE);
ok((TRUE == FALSE) == FALSE);
}
BEGIN { $n += 4 }
{
ok 8.2 == TRUE;
ok 8.2, TRUE;
ok 8.2, not FALSE;
ok 8.2, not not TRUE;
}
__END__
LP^>