Re: TRUE

2004-02-07 Thread Michael G Schwern
On Fri, Feb 06, 2004 at 08:24:21PM +0100, Abigail wrote:
> > Would you do "if (returns_a_true_value() eq TRUE)"? "== TRUE"?
> > Neither would work, and you can just do "if (returns_a_true_value())"
> > directly anyway.
> 
> 
> Not a problem for the Fun With Perl mailinglist, is it?
> 
> if (returns_a_true_value () xor FALSE) {
> print "It returned true.\n";
> }

I myself prefer:

print "It returned true\n" if !!returns_a_true_value() == TRUE

!! is the "convert to boolean" operator. :)


-- 
Michael G Schwern[EMAIL PROTECTED]  http://www.pobox.com/~schwern/
Monkey tennis


Re: TRUE

2004-02-06 Thread Abigail
On Wed, Jan 21, 2004 at 06:43:26PM +0200, Gaal Yahas wrote:
> On Wed, Jan 21, 2004 at 11:09:29AM -0500, Andrew Pimlott wrote:
> > I like to use symbolic constants.  Wha can remember what all those
> > 1, 0, undef, and ''s mean anyway?  So I start off all my programs
> > with
> > 
> > use constant FALSE => !TRUE;
> > use constant TRUE => !FALSE;
> 
> I start off all my programs with use strict :-)
> 
> But more to the point, how do you use your TRUE and FALSE? Suppose you
> want to test the return value of this sub:
> 
> sub returns_a_true_value { 8.2 }
> 
> Would you do "if (returns_a_true_value() eq TRUE)"? "== TRUE"?
> Neither would work, and you can just do "if (returns_a_true_value())"
> directly anyway.


Not a problem for the Fun With Perl mailinglist, is it?

if (returns_a_true_value () xor FALSE) {
print "It returned true.\n";
}


Abigail


Re: TRUE

2004-01-22 Thread Peter Haworth
On Wed, 21 Jan 2004 18:43:26 +0200, Gaal Yahas wrote:
> On Wed, Jan 21, 2004 at 11:09:29AM -0500, Andrew Pimlott wrote:
> > I like to use symbolic constants.  Wha can remember what all those
> > 1, 0, undef, and ''s mean anyway?  So I start off all my programs
> > with
> > 
> > use constant FALSE => !TRUE;
> > use constant TRUE => !FALSE;
> 
> I start off all my programs with use strict :-)

So do I, but for some reason, it's always the last C. Under these
circumstances, the above code works.

-- 
Peter Haworth   [EMAIL PROTECTED]
``True, it returns "" for false,
  but "" is an even more interesting number than 0.''
-- Larry Wall


Re: TRUE

2004-01-22 Thread Michael G Schwern
On Wed, Jan 21, 2004 at 11:09:29AM -0500, Andrew Pimlott wrote:
> I like to use symbolic constants.  Wha can remember what all those
> 1, 0, undef, and ''s mean anyway?  So I start off all my programs
> with
> 
> use constant FALSE => !TRUE;
> use constant TRUE => !FALSE;

You may be joking, but I used to do things like this:

use constant TRUE  => 1==1;
use constant FALSE => !TRUE;
use constant SUCCESS => TRUE;
use constant FAILURE => !SUCCESS;
use constant ERROR => undef;

and used it like...

sub is_foo {
...
return TRUE if $bleh eq 'foo';
}

on the grounds that "return 1" is ambiguous, are you returning the number 1
or indicating truth? 

I admit its rather overdoing it a bit.  I got it from a notable bit of 
dubious advice in a book that's otherwise full of good advice, "Code 
Complete".  Their logic was that different programming languages have 
different ideas of what truth is (for some, 0 is true) and you might forget 
as you moved from one language to another.

You can still see vestiages of this in one of my earliest modules,
Tree, to the point where I toyed with having a Truth.pm.


-- 
Michael G Schwern[EMAIL PROTECTED]  http://www.pobox.com/~schwern/


Re: TRUE

2004-01-21 Thread James E. Tilley
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^>


Re: TRUE

2004-01-21 Thread Ian Phillipps
On Wed, 21 Jan 2004 at 16:48:47 -0500, Andrew Pimlott wrote:

> But such code
> can sometimes be improved even more by defining a specific constant
> for each use.

> foo($bar, $baz, !VERBOSE, STOP_ON_ERROR);

And the real Fun with Perl is that this code will work perfectly well
without your bothering to define the constants.

Use strict? What's that?

Ian


Re: TRUE

2004-01-21 Thread Andrew Pimlott
On Wed, Jan 21, 2004 at 06:43:26PM +0200, Gaal Yahas wrote:
> On Wed, Jan 21, 2004 at 11:09:29AM -0500, Andrew Pimlott wrote:
> > use constant FALSE => !TRUE;
> > use constant TRUE => !FALSE;
> 
> I start off all my programs with use strict :-)

Wimp.  :-)

> But more to the point, how do you use your TRUE and FALSE? Suppose you
> want to test the return value of this sub:
> 
> sub returns_a_true_value { 8.2 }
> 
> Would you do "if (returns_a_true_value() eq TRUE)"? "== TRUE"?

Aieee!

> Neither would work, and you can just do "if (returns_a_true_value())"
> directly anyway.

Of course, even if it weren't broken, "eq TRUE" is logically
redundant and indicates the programmer was asleep, drunk, or
clueless.  (Not that I haven't publically posted code with this
shameful wart.  I couldn't look another programmer in the eyes for
weeks!)

The constants do improve code where it otherwise wouldn't be clear
to the reader whether 1 was an integer or a boolean.  But such code
can sometimes be improved even more by defining a specific constant
for each use.  Say a function takes a couple flags, one indicating
whether it should output verbose message, another whether it should
stop on the first error.  If you write

foo($bar, $baz, 0, 1);

you might forget what they mean.  If you write

foo($bar, $baz, !VERBOSE, STOP_ON_ERROR);

it's obvious.

Andrew


Re: TRUE

2004-01-21 Thread John Douglas Porter

"Steven W. Orr" <[EMAIL PROTECTED]> wrote:
> Umm, I think what you were trying for was . . .

Thank you, but I wrote exactly what I meant to.

-- 
John D. Porter




__
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus


Re: TRUE

2004-01-21 Thread Steven W. Orr
On Wednesday, Jan 21st 2004 at 09:08 -0800, quoth John Douglas Porter:

=>
=>On a related, less non-serious note, I seem recall it was fairly
=>standard in C, back in the old days, to write something like
=>
=>  #define FALSE !1
=>  #define TRUE !FALSE
=>
=>Since 1 is "a" true value, but TRUE is "the" true value.
=>(And of course FALSE is "the" false value.)

Umm, I think what you were trying for was

#define FALSE 0
#define TRUE (!FALSE)

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net


Re: TRUE

2004-01-21 Thread Steven W. Orr
On Wednesday, Jan 21st 2004 at 11:09 -0500, quoth Andrew Pimlott:

=>I like to use symbolic constants.  Wha can remember what all those
=>1, 0, undef, and ''s mean anyway?  So I start off all my programs
=>with
=>
=>use constant FALSE => !TRUE;
=>use constant TRUE => !FALSE;
=>
=>Andrew
=>
Sounds logical even to a Vulcan. :^)

Listen to me. Everything I say is false.

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net


Re: TRUE

2004-01-21 Thread Gaal Yahas
On Wed, Jan 21, 2004 at 05:52:02PM +0100, Honza Pazdziora wrote:
> > But more to the point, how do you use your TRUE and FALSE? Suppose you
> > want to test the return value of this sub:
> > 
> > sub returns_a_true_value { 8.2 }
> > 
> > Would you do "if (returns_a_true_value() eq TRUE)"? "== TRUE"?
> > Neither would work, and you can just do "if (returns_a_true_value())"
> 
> But 8.2 is not TRUE value. If returns_a_true_value is supposed to
> return a true true value, it should end with
> 
>   return TRUE;
> 
> shouldn't it?

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.

-- 
Gaal Yahas <[EMAIL PROTECTED]>
http://gaal.livejournal.com/


Re: TRUE

2004-01-21 Thread John Douglas Porter

Honza Pazdziora <[EMAIL PROTECTED]> wrote:
> But 8.2 is not TRUE value. If returns_a_true_value is supposed to
> return a true true value, it should end with
> 
>   return TRUE;
> 
> shouldn't it?

Well, no.  If it is supposed to return "a" true value, then it
could return any true value, and 8.2 falls in that category.

On a related, less non-serious note, I seem recall it was fairly
standard in C, back in the old days, to write something like

  #define FALSE !1
  #define TRUE !FALSE

Since 1 is "a" true value, but TRUE is "the" true value.
(And of course FALSE is "the" false value.)

But that was in the days before standard C, which demanded
that FALSE always be zero.

Constants like this aren't (or shouldn't be) for testing,
only for setting.

-- 
John D. Porter


__
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus


Re: TRUE

2004-01-21 Thread Honza Pazdziora
On Wed, Jan 21, 2004 at 06:43:26PM +0200, Gaal Yahas wrote:
> 
> But more to the point, how do you use your TRUE and FALSE? Suppose you
> want to test the return value of this sub:
> 
> sub returns_a_true_value { 8.2 }
> 
> Would you do "if (returns_a_true_value() eq TRUE)"? "== TRUE"?
> Neither would work, and you can just do "if (returns_a_true_value())"

But 8.2 is not TRUE value. If returns_a_true_value is supposed to
return a true true value, it should end with

return TRUE;

shouldn't it?

-- 

 Honza Pazdziora | [EMAIL PROTECTED] | http://www.fi.muni.cz/~adelton/
 .project: Perl, mod_perl, DBI, Oracle, large Web systems, XML/XSL, ...
Only self-confident people can be simple.


Re: TRUE

2004-01-21 Thread Andrew Molyneux
I think Mr Pimlott was joking, although I must admit it was pretty subtle
:-)

Another Andrew

- Original Message - 
From: "Gaal Yahas" <[EMAIL PROTECTED]>
To: "Andrew Pimlott" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Wednesday, January 21, 2004 4:43 PM
Subject: Re: TRUE


> On Wed, Jan 21, 2004 at 11:09:29AM -0500, Andrew Pimlott wrote:
> > I like to use symbolic constants.  Wha can remember what all those
> > 1, 0, undef, and ''s mean anyway?  So I start off all my programs
> > with
> >
> > use constant FALSE => !TRUE;
> > use constant TRUE => !FALSE;
>
> I start off all my programs with use strict :-)
>
> But more to the point, how do you use your TRUE and FALSE? Suppose you
> want to test the return value of this sub:
>
> sub returns_a_true_value { 8.2 }
>
> Would you do "if (returns_a_true_value() eq TRUE)"? "== TRUE"?
> Neither would work, and you can just do "if (returns_a_true_value())"
> directly anyway.
>
> -- 
> Gaal Yahas <[EMAIL PROTECTED]>
> http://gaal.livejournal.com/



Re: TRUE

2004-01-21 Thread Gaal Yahas
On Wed, Jan 21, 2004 at 11:09:29AM -0500, Andrew Pimlott wrote:
> I like to use symbolic constants.  Wha can remember what all those
> 1, 0, undef, and ''s mean anyway?  So I start off all my programs
> with
> 
> use constant FALSE => !TRUE;
> use constant TRUE => !FALSE;

I start off all my programs with use strict :-)

But more to the point, how do you use your TRUE and FALSE? Suppose you
want to test the return value of this sub:

sub returns_a_true_value { 8.2 }

Would you do "if (returns_a_true_value() eq TRUE)"? "== TRUE"?
Neither would work, and you can just do "if (returns_a_true_value())"
directly anyway.

-- 
Gaal Yahas <[EMAIL PROTECTED]>
http://gaal.livejournal.com/