Subject: | support for the nil extension |
Lots of RPC-XML libraries support an extension for a nil data type
(corresponding to undef). The extension is documented here:
<http://ontosys.com/xml-rpc/extensions.php>
For example, python supports it via an "allow_none" flag, and I think
the RPC-Xmlrpc_c module supports it by default.
I would really like to be able to use this in my XML::RPC using program,
so I wrote the attached patch. I parameterised it so that nil is only
emitted or parsed once $RPC::XML::ALLOW_NIL is set.
I understand that there might be a release coming up. Please consider
adding this to the release.
Subject: | nil.patch |
diff -ur old/librpc-xml-perl-0.59/lib/RPC/XML/Parser.pm librpc-xml-perl-0.59/lib/RPC/XML/Parser.pm
--- old/librpc-xml-perl-0.59/lib/RPC/XML/Parser.pm 2006-06-04 03:44:41.000000000 -0400
+++ librpc-xml-perl-0.59/lib/RPC/XML/Parser.pm 2008-03-16 13:34:06.000000000 -0400
@@ -205,7 +205,7 @@
{
push(@{$robj->{stack}}, TAG2TOKEN->{$elem});
}
- elsif (VALIDTYPES->{$elem})
+ elsif (VALIDTYPES->{$elem} || ($elem eq 'nil' && $RPC::XML::ALLOW_NIL))
{
# All datatypes are represented on the stack by this generic token
push(@{$robj->{stack}}, DATATYPE);
@@ -283,7 +283,7 @@
$op = pop(@{$robj->{stack}});
# Decide what to do from here
- if (VALIDTYPES->{$elem})
+ if (VALIDTYPES->{$elem} || ($elem eq 'nil' && $RPC::XML::ALLOW_NIL))
{
# This is the closing tag of one of the data-types.
$class = $elem;
diff -ur old/librpc-xml-perl-0.59/lib/RPC/XML.pm librpc-xml-perl-0.59/lib/RPC/XML.pm
--- old/librpc-xml-perl-0.59/lib/RPC/XML.pm 2006-06-30 03:36:29.000000000 -0400
+++ librpc-xml-perl-0.59/lib/RPC/XML.pm 2008-03-16 13:39:27.000000000 -0400
@@ -28,7 +28,8 @@
use 5.005;
use strict;
use vars qw(@EXPORT @EXPORT_OK %EXPORT_TAGS @ISA $VERSION $ERROR
- %xmlmap $xmlre $ENCODING $FORCE_STRING_ENCODING);
+ %xmlmap $xmlre $ENCODING $FORCE_STRING_ENCODING
+ $ALLOW_NIL);
use subs qw(time2iso8601 smart_encode bytelength);
# The following is cribbed from SOAP::Lite, tidied up to suit my tastes
@@ -57,6 +58,9 @@
# force strings?
$FORCE_STRING_ENCODING = 0;
+
+ # allow use of the nonstandard nil type?
+ $ALLOW_NIL = 0;
}
require Exporter;
@@ -121,7 +125,14 @@
{
if (!defined $_)
{
- $type = RPC::XML::string->new('');
+ if (! $ALLOW_NIL)
+ {
+ $type = RPC::XML::string->new('');
+ }
+ else
+ {
+ $type = RPC::XML::nil->new();
+ }
}
elsif (ref $_)
{
@@ -403,6 +414,39 @@
###############################################################################
#
+# Package: RPC::XML::nil
+#
+# Description: The "nil" type-class extension
+#
+###############################################################################
+package RPC::XML::nil;
+
+use strict;
+use vars qw(@ISA);
+
+@ISA = qw(RPC::XML::simple_type);
+
+# no value need be passed to this method
+sub new
+{
+ my $class = shift;
+ my $value = undef;
+
+ if (! $RPC::XML::ALLOW_NIL) {
+ die "\$RPC::XML::ALLOW_NIL must be set for RPC::XML::nil objects to be supported";
+ }
+
+ bless \$value, $class;
+}
+
+# serialsation is trivial..
+sub as_string
+{
+ "<nil/>";
+}
+
+###############################################################################
+#
# Package: RPC::XML::array
#
# Description: This class encapsulates the array data type. Each element
@@ -1464,6 +1508,15 @@
Creates an instance of the XML-RPC C<dateTime.iso8601> type. The specification
for ISO 8601 may be found elsewhere. No processing is done to the data.
+=item RPC::XML::nil
+
+Creates a nil value. The value returned will always be undef. No value
+needs to be passed when calling the constructor.
+
+Note that nil is an extension to B<XML-RPC>, which is not supported by all
+implementations. $ALLOW_NIL must be set before objects of this type can be
+constructed.
+
=item RPC::XML::base64
Creates an object that encapsulates a chunk of data that will be treated as
@@ -1658,6 +1711,11 @@
Defaults to C<false>.
+=item $ALLOW_NIL
+
+By default, the XML-RPC nil extension is not supported. Set to allow
+use of nil values, which will be represented as perl undef values.
+
=back
=head1 CAVEATS