[Python-Dev] [OffTopic] Re: Unicode experience

Thomas Wouters thomas@xs4all.net
Sat, 8 Jul 2000 01:32:16 +0200


On Fri, Jul 07, 2000 at 06:04:20PM -0500, Paul Prescod wrote:

> According to the definition used in Java, Javascript and C++ (sometimes)

> x+y

> If y is a string then x+y is well-defined no matter what the type of x
> or the content of y.

The right hand side of the operation determines what the result is ? Somehow
I find that confusing and counter-intuitive, but perhaps I'm too used to
__add__ vs. __radd__ ;) Anyway, I've tried to learn C++ and Java both twice
now, buying books by *good* authors (like Peter v/d Linden) and I still
manage to run screaming after reading a single page of feature listings ;)

> Under the Perl definition, it totally depends on the type of y and the
> contents of x. Insane!

Nono, absolutely not. Perl is weirdly-typed: the type is determined by the
*operator*, not the operands. If you do '$x + $y', and both $x and $y are
strings, they will both be converted to numbers (ending up 0 if it fails)
and added as numbers, and the result is a number. (But since perl is
weirdly-typed, if you need it to result in a string, the number will be
converted to a string as necessary.) If you want string-concatenation, use
'.', which will turn numbers into strings.

But Perl wouldn't be Perl if it hadn't *some* exceptions to this kind of
rule ;) '$x++', where $x is a string, can return a string. The
'string-auto-increment' operator is Magic(tm): if the string matches
/^[a-zA-Z]*[0-9]*$/, that is, something like 'spam001', the ++ operator adds
one to the number at the end of the string. so 'spam001' would become
'spam002'. If you do it to 'spam999', it'll be incremented to 'span000',
'spaz999' becomes 'spba000', and so on.

However, Perl wouldn't be Perl if it hadn't some excpetion to *this* too ;)
Well, not really an exception, but... The string *must* exactly match the
regexp above, or the '++' operator will turn it into a number '1' instead.
So your string cannot be '-spam01', or 'sp0m01', or anything like that.
Rendering this magic behaviour pretty useless in general, because it's
easier, more readable and less buggy to do the work the magic ++ does by
hand.

(We actually had some database 'corruption' in our old user-database because
of this... a tool accepted a 'template' username and tried to create a
specified number of accounts with a name in sequence to that template... The
original code had a safeguard (the above regexp) but it got removed somehow,
and eventually someone used a wrong template, and we ended up with accounts
named '1' through '8' ;P)

> I advocate special casing of strings (which are already special cased in
> various ways) whereas Perl special cases particular string values.
> Insane!

Well, yes. But Perl special cases about everything and their dog. Not Good.

(Okay, I promise to stay on-topic for a while now ;)

-- 
Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!