Subject: | NIL constant needs option of using undef |
Using the constant string of 'NIL' doesn't seem to be the best
translation into a Perl hash tree. It means that programmers have to
detect both zero-based checks (!$a) and the string 'NIL' for any checks.
Code like this would not work:
$XML->{a} ||= "test"; # still keeps a NIL field as NIL
However, other options may be inappropriate. Using 0 or '' is not the
same as a NULL value. But, undef works. It's more or less the NULL
field for Perl.
However, there is still a difference between a non-existent tag and
nillable data. So, using the interpret_nillable_as_optional option is
NOT the same as defining NIL as undef. If the NIL constant was set to
undef, you can still tell the difference in Perl:
<root><a>test</a><b></b><c xsi:nil="true" /></root>
say 'A = True' if ($XML->{a}); # True
say 'B = True' if ($XML->{b}); # False
say 'B = Defined' if (defined $XML->{b}); # True
say 'C = True' if ($XML->{c}); # False
say 'C = Defined' if (defined $XML->{c}); # False
say 'C = Exists' if (exists $XML->{c}); # True
say 'D = True' if ($XML->{d}); # False
say 'D = Defined' if (defined $XML->{d}); # False
say 'D = Exists' if (exists $XML->{d}); # False
Therefore, there should be a nil_value option for the reader/writer. It
would default to 'NIL' (for compatibility), but you can set it to
whatever value you want, including undef. Say somebody is used to NULL
in databases, or need to feed the reader to some sort of DB writer that
uses that string. They could set it to 'NULL', instead. Or fix Perl
matching problem by setting it to undef.
I can write a patch, but I'll need a hour to digest the code. Also
wanted your option on the manner.