[Xml-compile] NIL vs. undef?

Mark Overmeer mark at overmeer.net
Wed Oct 24 07:14:25 GMT 2012


* E R (pc88mxer at gmail.com) [121024 03:06]:
> 1. How do you disambiguate the NIL value from a string value of 'NIL'
> (the three letter string)?

I have been thinking about this since Ivan Šimoník suggested some
changes in August this year.  My current idea is to replace that NIL
with an object which stringifies to "NIL".

  use constant NIL { bless \(my $x), 'NIL' }

  package NIL;
  use overload '""' => sub {'NIL'}, fallback => 1;

But I am afraid this will break some running code.
The current problem is only with nillable simpleTypes.

> 2. When I get an $answer back from a SOAP call, I'd like to translate
> all NIL values to undef. Is there a standard way to do this?

You should not.
There are different kinds of undef.  In XML:
   <element name="tic" minOccurs="0">       # silently missing
   <element name="tac" nillable="true">     # explicitly missing
and some people even combine both
   <element name="toe" minOccurs="0" nillable="true">

The difference is subtile between these three:

   # address silently missing
   <person id="1">            { id => 1, name => 'One' }
     <name>One</name>         { id => 1, name => 'One', address => undef }
   </person>

   # has no address
   <person id="2">            { id => 2, name => 'Two', address => {}}
     <name>Two</name>
     <address />
   </person>

   # there should be an address but I do not tell you/do have it (yet)
   <person id="3">
     <name>Three</name>       { id => 3, name => 'Three', address => 'NIL'}
     <address xsi:nil="true" /> 
   </person>

There are even commercial applications which interpret nillable in a
schema as optional... so many people do not see these distinctions, or
interpret them differently.  I see above interpretation to be consequent
with programming languages which I learned, however.

In the third example, you see that there is no chance for mis-interpretation
for 'NIL' in complexTypes.  Only the simpleTypes.
-- 
Regards,

               MarkOv

------------------------------------------------------------------------
       Mark Overmeer MSc                                MARKOV Solutions
       Mark at Overmeer.net                          solutions at overmeer.net
http://Mark.Overmeer.net                   http://solutions.overmeer.net




More information about the Xml-compile mailing list