develooper Front page | perl.perl5.porters | Postings from February 2000

[PATCH 5.5.650] extend optional XS parameters

Thread Next
February 21, 2000 08:26
[PATCH 5.5.650] extend optional XS parameters
Message ID:
This patch permits optional parameters that do not take a default for
XS subs. Currently you can say, for example:

funcname(var = "default")
	char *var

.. where the default value is required to be an integer or a quoted string.
This enhancement additionally allows the literal text 'undef', in which
case the parameter is treated as optional but no attempt is made to
initialise it to any default value - it is up to the XS writer to ensure
it is properly initialised, or to avoid accessing it, in this case.

This makes it possible to have optional arguments that are not integers
or strings, which does not otherwise seem possible without writing
quite horrible CASE code, or using ... and managing the type handling
(and some of the prototype handling) yourself. For example, a polymorphic
get/set attribute function may look like:

attribute(object, value = undef)
	ObjectType object
	ValueType value
	if (items > 1)
	    object->value = value;
	RETVAL = object->value;

--- lib/ExtUtils/xsubpp.old	Sun Jan 23 12:08:32 2000
+++ lib/ExtUtils/xsubpp	Mon Feb 21 15:40:41 2000
@@ -1469,7 +1469,11 @@
 	      eval qq/print "\\t$var;\\n"/;
 	      warn $@   if  $@;
-	    $deferred .= eval qq/"\\n\\tif (items < $num)\\n\\t    $var = $defaults{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/;
+	    if ($defaults{$var} eq 'undef') {
+		$deferred .= eval qq/"\\n\\tif (items >= $num) {\\n$expr;\\n\\t}\\n"/;
+	    } else {
+		$deferred .= eval qq/"\\n\\tif (items < $num)\\n\\t    $var = $defaults{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/;
+	    }
 	    warn $@   if  $@;
     } elsif ($ScopeThisXSUB or $expr !~ /^\t\$var =/) {
 	    if ($name_printed) {
--- pod/perlxs.pod.old	Sun Jan 23 13:07:25 2000
+++ pod/perlxs.pod	Mon Feb 21 16:19:44 2000
@@ -502,9 +502,9 @@
 =head2 Default Parameter Values
-Default values for XSUB arguments can be specified by
-placing an assignment statement in the parameter list.  The
-default value may be a number or a string.  Defaults should
+Default values for XSUB arguments can be specified by placing an
+assignment statement in the parameter list.  The default value may
+be a number, a string or the special string C<undef>.  Defaults should
 always be used on the right-most parameters only.
 To allow the XSUB for rpcb_gettime() to have a default host
@@ -1313,6 +1313,19 @@
      RETVAL = THIS->blue();
      THIS->set_blue( val );
+You could also write a single get/set method using an optional argument:
+     int
+     color::blue( val = undef )
+         int val
+         PROTOTYPE $;$
+         CODE:
+             if (items > 1)
+                 THIS->set_blue( val );
+             RETVAL = THIS->blue();
+         OUTPUT:
+             RETVAL
 If the function's name is B<DESTROY> then the C++ C<delete> function will be
 called and C<THIS> will be given as its parameter.  The generated C++ code for

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About