Douglas Burke > Inline-SLang > Inline::SLang::Assoc


Annotate this POD


Open  0
View/Report Bugs


Inline::SLang::Assoc - Support for associative arrays


  use Inline 'SLang';

  # you can send hash references to S-Lang
  print_in_slang( { a => 23, "b b" => "foo" } );

  # and get them back from S-Lang
  $href = get_from_slang();
  print "The assoc array contains:\n" .
    join( "", map { "\t$_ => $$href{$_}\n"; } keys %$href );


  define print_in_slang (assoc) {
    message( "SLang thinks you sent it an assoc. array with:" );
    foreach ( assoc ) using ( "keys", "values" ) {
      variable k, v;
      ( k, v ) = ();
      vmessage( " key %s = %S", k, v );
  define get_from_slang() {
    variable x = Assoc_Type [String_Type];
    x["a key"] = "a value";
    x["another key"] = "another value";
    return x;

The output of this code - which can be found in the source-code distribution as examples/ - is:

  SLang thinks you sent it an assoc. array with:
   key a = 23
   key b b = foo
  The assoc array contains:
          another key => another value
          a key => a value


Since S-Lang's associative arrays are similar to Perl's associative (aka "hash") arrays then the conversion is fairly easy. It is not completely trivial since S-Lang's assoc. arrays know about the datatype of the variables they contain.

When a S-Lang associative array is sent to Perl, it is converted into a Assoc_Type Perl object. As discussed below, this behaves just like a hash reference with a few additional method calls.

When sending an associative array to a S-Lang function from Perl, two different (but not very different) techniques can be used:


The Perl Assoc_Type class stores the datatype of the array along with the data. Once you have created the object you can use it as a hash reference to get and set fields in the array. As described below the object also has a number of methods based on the S-Lang language which provides similar functionality.

The Assoc_Type class inherit the default methods of all the Inline::SLang objects, namely:

As with the other object classes these methods can only be called using the $object->method( args ) syntax. There are also a number of additional methods for this class:


Inline::SLang::Array, Inline::SLang, Math::Complex, PDL

syntax highlighting: