[oak perl] The mysterious "undef"

Mark Bole mark at bincomputing.com
Mon Mar 21 11:46:55 PST 2005


There are two uses of "undef", other than as a placeholder in an 
argument list, that merit a mention as well.

1) IIRC, it is the fastest way to "destroy" (free up the memory) used by 
an array or hash.  For example, this pseudo-code to collect things into 
groups:

my (@scratch, %permanent);
foreach $thing (@list_of_distinct_things) {
  push(@scratch, $thing);
  if (condition_involving_thing) {
  # save in a hash of lists, use last value in the list as hash key
      $permanent{$thing} =  [ @scratch ];
      undef @scratch;
      next;
  }
}
# handle whatever is left in @scratch here

2) more importantly, from a data modeling viewpoint, undef represents 
the "null" value as opposed to the "empty" or "false" value.  An example 
might be a password --  you can use undef  to distinguish between a 
valid password that consists of the empty string (defined) and an 
invalid or missing password (undef).

As an aside, given the editorial comments about MySQL, it is interesting 
to note that in Oracle, for reasons lost in the mists of time, a NULL is 
actually stored as an empty string, which doesn't seem right somehow, 
but they've kept it working all these years nonetheless.

-- 
Mark Bole
http://www.bincomputing.com
925-287-0366


Jon Seidel, CMC wrote:

>Sandy... One of the things that you'll use undef for is to act as a
>placeholder for parameters. [...]
>  
>
>>-----Original Message-----
>>From: oakland-bounces at pm.org [mailto:oakland-bounces at pm.org] 
>>On Behalf Of Sandy Santra
>>Sent: Friday, March 18, 2005 8:10 AM
>>To: Oakland Perl
>>Subject: [oak perl] The mysterious "undef"
>>
>>
>>[...]
>>
>>I wanted to learn the different between a zero value and 
>>undef because the 
>>llama book keeps saying there is one.  Is this a pointless 
>>issue to pursue?
>>
>>    
>>




More information about the Oakland mailing list