From: fw at cygnus.stuttgart.netsurf.de (Florian Weimer)
Date: 27 Apr 1999 21:10:42 +0200
Subject: converting perl to python - simple questions.
References: <7fvagp$8lm$1@nnrp1.dejanews.com> <m3u2u47hod.fsf@deneb.cygnus.stuttgart.netsurf.de> <lFLU2.3119$Ig1.434671@news1.rdc1.on.wave.home.com>
Message-ID: <m3so9lc219.fsf@deneb.cygnus.stuttgart.netsurf.de>
Content-Length: 1427
X-UID: 948                                                  

andrew-johnson at home.com (Andrew Johnson) writes:

> Just a point of clarification: Actually, perl programmer's
> traditionally use 'exists' to test if a key is in a hash ...

No, unfortunately that's wrong:

| defined(EXPR)
|
| defined EXPR
|         Returns a boolean value saying whether the  lvalue
|         EXPR  has  a  real  value or not.  Many operations
|         return the undefined value under exceptional  con-
|         ditions,  such as end of file, uninitialized vari-
|         able, system error and such.  This function allows
|         you  to  distinguish  between  an  undefined  null
|         string and a defined null string  with  operations
|         that  might return a real null string, in particu-
|         lar referencing elements of  an  array.   You  may
|         also  check to see if arrays or subroutines exist.
|         Use on predefined variables is not  guaranteed  to
|         produce intuitive results.  Examples:
|
|              print if defined $switch{'D'};
|              [...]

(Quotation from the Perl 4 manpage.  This version doesn't have an
`exists' function.)

>  using
> 'defined' to test for key existence is a mistake---'defined' will
> only tell you if that key exists and has a defined value associated
> with it: Witness the defined test on key2 in the following:

Of course you are right.  That's the reason why `exists' was introduced
-- I guess in Perl 5.




