Документ взят из кэша поисковой машины. Адрес оригинального документа : http://hea-www.harvard.edu/~dburke/perl-slang/examples_general.html
Дата изменения: Unknown
Дата индексирования: Tue Oct 2 04:40:59 2012
Кодировка:

Поисковые слова: р р р р п п п п п п п п п п
Doug Burke's Perl/S-Lang pages: General examples
Jump to: main text

General examples 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, using the PDL support, together with version 1.4.9 of the S-Lang library.

Order of evaluation

#
# Use this via 'perl -Mblib examples/order.pl'
#

use Inline 'SLang' => 'message("This is S-Lang");';

print "This is Perl\n";

which, when run, produces

This is S-Lang
This is Perl

Trapping errors

#
# $Id: trap_errors.pl,v 1.1 2005/01/04 16:18:07 dburke Exp $
#
# Evaluate a S-Lang statement which contains an error
#

use strict;
use Inline 'SLang';

# Call the S-Lang function
#
my $ans;
eval { $ans = mydiv (0.0); };
print "The S-Lang error was:\n$@\n";

# Evaluate S-:ang code directly
#
eval { Inline::SLang::sl_eval( "10.0/0.0;" ); };
print "The S-Lang error was:\n$@\n";

__END__
__SLang__

define mydiv(y) { return 10.0 / y; }

which, when run, produces

The S-Lang error was:
S-Lang Error: Divide by zero: Error while executing mydiv

The S-Lang error was:
S-Lang Error: Divide by zero: called from line 1, file: ***string***

What types are available?

The INFO option of the Inline module can be used to find out what S-Lang functions and datatypes are recognised by the module. The following example:

#
# Use this via 'perl -Mblib -MInline=info example/info.pl'
#

use Inline SLang;
# let's not actually do anything

__END__
__SLang__

typedef struct { foo, bar } FooBarStruct_Type;

variable foobar = "a string";
define foo()  { return foobar; }
define bar(x) { foobar = x; }

produces the following output when the '-MInline=info' option is added to the Perl interpreter:

<-----------------------Information Section----------------------------------->

Information about the processing of your Inline SLang code:

Your source code needs to be compiled. I'll use this build directory:
/data/dburke2/www/perl-slang/examples/_Inline/build/info_pl_9cef

and I'll install the executable as:
/data/dburke2/www/perl-slang/examples/_Inline/lib/auto/info_pl_9cef/info_pl_9cef.sldat

Configuration details
---------------------

Version of S-Lang: 1.4.9
Perl module version is 1.00 and supports PDL

The following S-Lang types are recognised:
 Int32_Type UInteger_Type _IntegerP_Type
 FooBarStruct_Type[Struct_Type] Int_Type Struct_Type ULong_Type
 FD_Type Long_Type Float_Type Array_Type UInt32_Type File_Type
 UInt_Type UChar_Type UShort_Type Double_Type Float64_Type Int16_Type
 Float32_Type Null_Type Integer_Type BString_Type Char_Type
 Undefined_Type Short_Type Any_Type Assoc_Type Ref_Type Complex_Type
 String_Type UInt16_Type DataType_Type

The following S-Lang namespaces have been bound to Perl:

  2 functions from namespace Global are bound to package main
	bar()
	foo()


<-----------------------End of Information Section---------------------------->