On Thu, 2004-06-24 at 11:34, Smylers wrote:
> Scott Bronson writes:
> But you're fine with 0 being false?  0 and '0' are pretty much
> interchangeable in Perl 5 -- wherever you can use one, you can use the
> other and it gets coerced to it.

Let's back up...  Strings and numbers are meant to be interchangeable in
Perls 1 through 5.  You should not care if a varaible is represented
internally using a string or an int or a float or a bignum or...  The
problem is, how should Perl compare two values when it doesn't know what
they are?  Larry solved this by creating a whole new set of operators to
explicitly force a context:

   num         str
   ----        ----
   ==    vs.   eq
   <     vs.   lt
   etc.

OK, comparison is handled.  But what about evaluating to true or false
without comparison?  The representation problem still exists.  If Larry
had solved this problem the same way he did comparison, he would have
created even more operators and control structures:

   num         str
   ----        ----
   !     vs.   not
   if    vs.   sif
   while vs.   swhile

For whatever reason, he chose to solve this problem differently. 
Instead of creating stringy evaluation, he decided to have "0" evaluate
false.  On the surface, and in practice, this trick seems to work pretty
well.

Unfortunately, it leaves a lot of weird behavior lying around the
edges.   What about "0.0"?  Or "00"?  And your example, "0x00"?  Those
are all perfectly valid ways of reperesenting 0.  If strings and ints
truly were interchangeable, all of these strings would be false,
woudln't they?

And what about when you KNOW an expression should be evaluated as a
string?  You need to remember to use "defined($s) && length($s)",
otherwise you will suffer from the '0' hole (where the string "0" is
handled differently from all other strings in your program).


>   So it really wouldn't make sense to
> have them being treated differently for truth purposes.

OK, name another language that does this.  Well, other than PHP, whose
string handling is even screwier than Perl's.  :)  I think that Ruby is
an example of a scripting language that has solved this problem very
well (and I think Python is similar...?).

So, in summary, though "0"==false appears to work, it leads to a number
of strange boundary conditions and, therefore, bugs.  It's hard for new
programmers to grasp and even old hacks are still sometimes tripped up
by it.  It just feels inconsistent.  That's why I'd love to see this
straightened out for Perl6.

    - Scott


Reply via email to