Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!apple!agate!math.berkeley.edu!raymond From: raymond@math.berkeley.edu (Raymond Chen) Newsgroups: comp.lang.perl Subject: defined() on an undef array element created via slice Message-ID: <1990Nov8.005449.26158@agate.berkeley.edu> Date: 8 Nov 90 00:54:49 GMT Sender: usenet@agate.berkeley.edu (USENET Administrator) Reply-To: raymond@math.berkeley.edu Organization: U.C. Berkeley Lines: 41 [The question:] Consider the following script: @foo{"bar"} = (); print keys(%foo), "\n"; # See, it's listed as one of the keys if (defined($foo{"bar"})) { print "Patch level 18 prints this\n"; } else { print "Patch level 36 prints this\n"; } Observe that $foo{"bar"} was created via an associative array slice. Who is right, Patch level 18 or Patch level 36? The problem seems to be that, although $foo{"bar"} is defined, its value is undef. This schizophrenic state of affairs seems to lead to different interpretations depending on your patch level. (Maybe because PL16 initialized $foo{"bar"} = ""? Is this related to the change in semantics of local()?) [How I noticed this:] My current code initializes a list of commands via @commands{ split(/ /, "Scale Height Width Borders etc...") } = (); and tests if a particular token is a command via if (defined($commands{$token})) {...} This breaks under PL36 as described above. I know I can write for (split(/ /, "Scale Height Width etc...")) { $commands{$_} = 1; } but somehow, the slice looked spiffier to me at the time. Ah, the price one pays for a moment of folly. -- @japh{split(/:/, "hacker,:Just :erl :another p")} = (); print sort keys %japh; # Extra credit: What is $japh{"hacker,"}? What is defined($japh{"hacker,"})?