A COM interface to some S functions

The Server

We define the COM server as having 3 methods that generate samples from different distributions. The S functions we expose are rnorm, rpois and rbinom for the Normal, Poisson and Binomial distributions. Rather than expose these using the S function names, we can use more informative names to non-S users such as "normal", "poisson", and "binomial". We do this by providing these as the names of the character vector identifying the functions to expose.

We use the SCOMNamedFunctionClass class to define the COM class since we are exposing top-level function names as COM methods. We use the name "RNG" to identify the COM class to clients and register the definition with both R and the Windows registry.
def = SCOMNamedFunctionClass(functions = c(normal="rnorm", poisson="rpois", binomial="rbinom"),
     			     name = "RNG",
			     help = "Generate values from probability distributions"
At this point, we are ready to use the class in client applications.

A Perl client

We can easily use this class in a Perl application by loading the Perl COM module (Win32::OLE) and using that to create a new "RNG" object.
use Win32::OLE;

$r = Win32::OLE->new("RNG");
Now that we have the RNG object, we can invoke any of the methods. We first generate a sample of size 10 from a standard normal and then another sample but this time specifying the mean of the distribution as 50.
 # 10 values from N(0, 1)
@x = @{$r->normal(10)};
print "Normal: @x\n";

# Now generate 10 values from a N(50, 1)
@x = @{$r->normal(10, 50)};
print "Normal: @x\n";
If we wanted to specify just the standard deviation, we could use named arguments. We do this in perl by giving all the arguments within a table/associate array as shown below.
# Now generate 10 values from a N(0, 20)
@x = @{$r->normal(''=> 10, 'sd' => 20)};
print "Normal: @x\n";
Note that we can provide arguments by position to the S function without a name and simply using ''. However, one should almost never use unnamed arguments after named arguments. So
@x = @{$r->normal(''=> 10, 'sd' => 20, 'mean' => 100)};
print "Normal: @x\n";
is fine, but
@x = @{$r->normal(''=> 10, 'sd' => 20, '' => 100)};
print "Normal: @x\n";
gets very confused and uses 100 as the value for n

Finally, we illustrate the use of the other two methods for generating a sample from a Poisson and a Binomial distribution.
@x = @{$r->poisson(5, 3)};
print "Poisson: @x\n";

@x = @{$r->binomial(7, 20, .2)};
print "Binomial: @x\n";
The arguments are passed to the corresponding S functions and one can consult the help for these to understand their meaning.