Examples of binding functions from Inline::SLang
The following code and examples can be found in the Inline::SLang distribution available from CPAN. The output was created using version 1.00 of the module together with version 1.4.9 of the S-Lang library.
By default, only S-Lang functions in the "Global" namespace are bound to Perl (i.e, can be called from Perl). However, this can be changed by setting up the appropriate configuration options for Inline::SLang. See the Inline::SLang::Config document for more information - and examples - on this capability.
Binding to functions in all namespaces
use Inline 'SLang' => Config => BIND_NS => "All"; use Inline 'SLang' => <<'EOS1'; define fn_in_global(x) { "in global"; } implements( "foo" ); define fn_in_foo(x) { "in foo"; } EOS1 printf "I am %s\n", foo::fn_in_foo("dummyval"); printf "I am %s\n", fn_in_global("dummyval");
which, when run, produces
I am in foo I am in global
Binding to functions in many namespaces
use Inline 'SLang' => Config => BIND_NS => [ "Global", "foo" ]; use Inline 'SLang' => <<'EOS1'; define fn_in_global(x) { "in global"; } implements( "foo" ); define fn_in_foo(x) { "in foo"; } EOS1 printf "I am %s\n", foo::fn_in_foo("dummyval"); printf "I am %s\n", fn_in_global("dummyval");
which, when run, produces
I am in foo I am in global
Binding to functions in one namespaces
use Inline 'SLang' => Config => BIND_NS => ["foo"]; use Inline 'SLang' => <<'EOS1'; define fn_in_global(x) { "in global"; } implements( "foo" ); define fn_in_foo(x) { "in foo"; } EOS1 printf "I am %s\n", foo::fn_in_foo("dummyval"); # the following will not work since fn_in_global() is in the # Global namespace which was not listed in the BIND_NS option # printf "I am %s\n", fn_in_global("dummyval");
which, when run, produces
I am in foo
The call to fn_in_global() line results in the following error message:
Undefined subroutine &main::fn_in_global called at ...
Changing the Perl package to which a namespace is mapped
use Inline 'SLang' => Config => BIND_NS => [ "Global", "foo=bar" ]; use Inline 'SLang' => <<'EOS1'; define fn_in_global(x) { "in global"; } implements( "foo" ); define fn_in_foo(x) { "in foo"; } EOS1 printf "I am %s\n", bar::fn_in_foo("dummyval"); printf "I am %s\n", fn_in_global("dummyval");
which, when run, produces
I am in foo I am in global
Binding S-Lang intrinsic functions I
By default the S-Lang intrinsics - i.e. the functions from the S-Lang Run-Time Library - are not made available to Perl, since Perl has its own set of intrinsic functions with similar names and functionality. It can sometimes be useful to access a S-Lang intrinsic directly from Perl, as shown in the example below. The following example shows how you can bind the intrinsic function into a named Perl package.
use Inline 'SLang' => Config => BIND_SLFUNCS => ["typeof"]; use Inline 'SLang' => "define get_typeof(x) { typeof(x); }"; # both print # The S-Lang type of 'foo' is String_Type printf "The S-Lang type of 'foo' is %s\n", get_typeof("foo"); printf "The S-Lang type of 'foo' is %s\n", typeof("foo");
which, when run, produces
The S-Lang type of 'foo' is String_Type The S-Lang type of 'foo' is String_Type
Binding S-Lang intrinsic functions II
use Inline 'SLang' => Config => BIND_NS => "Global=foo", BIND_SLFUNCS => ["typeof"]; use Inline 'SLang' => " "; # This prints # The S-Lang type of 'foo' is String_Type printf "The S-Lang type of 'foo' is %s\n", foo::typeof("foo");
which, when run, produces
The S-Lang type of 'foo' is String_Type