BigRedEO BigRedEO - 1 year ago 54
Perl Question

Perl "null" equivalent?

Confused by Perl's empty string, undefined, blank, et al (or of course NULL as it's known in some other languages).

Just parsing a CSV file and for one field, if it's blank, I need to put in 0000-00-00 (it's a date field). If it's left blank/null, another program that uses the .CSV file crashes with "Incorrect date value: '' for column 'updateTranDate'"

Would something like this work?

$_ = 0000-00-00 for @fields[23];

cjm cjm
Answer Source

It sounds like what you want is

if(!length $fields[23])
    $fields[23] = '0000-00-00';

This condition will be true if $fields[23] is either undef or the empty string (which are not quite the same thing). You also need to put quotes around the date, or it will be treated as a math expression (0 - 0 - 0 which equals 0).