Jump to: main text

Calling S-Lang routines from Perl

I have written a Perl module (Inline::SLang) that - using the framework provided by the Perl Inline module - allows you to define S-Lang functions and call them from your Perl code. Whilst the module allows you to embed any bit of S-Lang code into Perl, there is a section devoted to using it with the CIAO software package, since it's the reason why I wrote the module in the first place.

Version 1.00 was released on January 4 2005!

Rather than spend a lot of time regurgitating the contents of the Inline::SLang documentation, this page contains a couple of simple examples that will hopefully give you a flavour of the module. There are also a set of examples reachable from this page which highlight various features of the package. Finally,when in doubt, you could try reading the module's documentation, which is available from the CPAN page.

The PySL package is similar to Inline::SLang but for Python rather than Perl.

A simple example


use Inline 'SLang' => '
  % this is S-Lang
  define add      (a,b) { return a + b; }
  define subtract (a,b) { return a - b; }
  message("I have just defined two S-Lang functions");
';

# and this is Perl
print "And now to use them from Perl\n";
print "  9 + 16 = ", add(9,16), "\n";
print "  9 - 16 = ", subtract(9,16), "\n";

which, when run, produces

I have just defined two S-Lang functions
And now to use them from Perl
  9 + 16 = 25
  9 - 16 = -7

If you have not used the Perl Inline module before then I should briefly explain what happened above. When the Perl code is loaded, the Inline::SLang module evaluates the S-Lang code, determines what functions and data types have been added to the S-Lang interpreter, and then binds the functions to Perl. Some things to note are:

Once the code is running, the main job of the Inline::SLang module is to convert between the variable types of the two languages. Since the module finds out all it needs to know from the S-Lang interpreter, it can handle importable modules; even those that define their own variables. In other words it does not have to be "taught" about a S-Lang module before it can use it. This can come in rather handy at times.

A number of examples are available:

Thanks to - in no particular order - John Davis, Neil Watkiss, and Brian Ingerson.