diff --git a/Examples/perl5/constants/Makefile b/Examples/perl5/constants/Makefile new file mode 100644 index 000000000..47fd1eb6d --- /dev/null +++ b/Examples/perl5/constants/Makefile @@ -0,0 +1,17 @@ +TOP = ../.. +SWIG = $(TOP)/../swig +SRCS = +TARGET = example +INTERFACE = example.i +SWIGOPT = +all:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' perl5 + +static:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='myperl' INTERFACE='$(INTERFACE)' perl5_static + +clean:: + rm -f *_wrap* *.o core *~ *.so *.pm myperl + diff --git a/Examples/perl5/constants/example.i b/Examples/perl5/constants/example.i new file mode 100644 index 000000000..9b04ebc1d --- /dev/null +++ b/Examples/perl5/constants/example.i @@ -0,0 +1,24 @@ +/* File : example.i */ +%module example + +/* A few preprocessor macros */ + +#define ICONST 42 +#define FCONST 2.1828 +#define CCONST 'x' +#define SCONST "Hello World" + +/* This should work just fine */ +#define EXPR ICONST + 3*(FCONST) + +/* This shouldn't do anything */ +#define EXTERN extern + +/* Neither should this (BAR isn't defined) */ +#define FOO (ICONST + BAR) + +/* The following statements also produce constants */ +const int iconst = 37; +const double fconst = 3.14; + + diff --git a/Examples/perl5/constants/example.pl b/Examples/perl5/constants/example.pl new file mode 100644 index 000000000..0cf207c3c --- /dev/null +++ b/Examples/perl5/constants/example.pl @@ -0,0 +1,27 @@ +# file: example.pl + +use example; + +print "ICONST = ", $example::ICONST, " (should be 42)\n"; +print "FCONST = ", $example::FCONST, " (should be 2.1828)\n"; +print "CCONST = ", $example::CCONST, " (should be 'x')\n"; +print "SCONST = ", $example::SCONST, " (should be 'Hello World')\n"; +print "EXPR = ", $example::EXPR, " (should be 48.5484)\n"; +print "iconst = ", $example::iconst, " (should be 37)\n"; +print "fconst = ", $example::fconst, " (should be 3.14)\n"; + + +if ($example::EXTERN) { + print "EXTERN = ", example.EXTERN, " (Arg! This shouldn't print anything)\n"; +} else { + print "EXTERN isn't defined (good)\n"; +} + +if ($example::FOO) { + print "FOO = ", example.FOO, "(Arg! This shouldn't print anything)\n"; +} else { + print "FOO isn't defined (good)\n"; +} + + + diff --git a/Examples/perl5/constants/index.html b/Examples/perl5/constants/index.html new file mode 100644 index 000000000..267d5b365 --- /dev/null +++ b/Examples/perl5/constants/index.html @@ -0,0 +1,55 @@ + + +SWIG:Examples:perl5:constants + + + + +SWIG/Examples/perl5/constants/ +
+ +

Wrapping C Constants

+ +$Header$
+ +

+When SWIG encounters C preprocessor macros and C declarations that look like constants, +it creates Perl5 variables with an identical value. Click here +to see a SWIG interface with some constant declarations in it. + +

Accessing Constants from Perl

+ +Click here to see a script that prints out the values +of the constants contained in the above file. + +

Key points

+ + + +
+ + + + diff --git a/Examples/perl5/index.html b/Examples/perl5/index.html new file mode 100644 index 000000000..007477ab8 --- /dev/null +++ b/Examples/perl5/index.html @@ -0,0 +1,74 @@ + + +SWIG:Examples:perl5 + + + +

SWIG Perl Examples

+ +$Header$
+ +

+The following examples illustrate the use of SWIG with Perl. + +

+ +

Compilation Issues

+ + + +

Compatibility

+ +The examples have been extensively tested on the following platforms: + +

+

+ +

+Due to wide variations in the Perl C API and differences between versions such as the ActivePerl release for Windows, +the code generated by SWIG is extremely messy. We have made every attempt to maintain compatibility with +many Perl releases going as far back as 5.003 and as recent as 5.6. However, your mileage may vary. +If you experience a problem, please let us know by +sending a message to swig-dev@cs.uchicago.edu. +Better yet, send us a patch. + + + + + diff --git a/Examples/perl5/simple/README b/Examples/perl5/simple/README deleted file mode 100644 index 07e8da069..000000000 --- a/Examples/perl5/simple/README +++ /dev/null @@ -1 +0,0 @@ -Simple example from users manual. diff --git a/Examples/perl5/simple/example.c b/Examples/perl5/simple/example.c index f2b074781..1c2af789c 100644 --- a/Examples/perl5/simple/example.c +++ b/Examples/perl5/simple/example.c @@ -1,24 +1,18 @@ -/* Simple example from documentation */ /* File : example.c */ -#include +/* A global variable */ +double Foo = 3.0; -double My_variable = 3.0; - -/* Compute factorial of n */ -int fact(int n) { - if (n <= 1) return 1; - else return n*fact(n-1); -} - -/* Compute n mod m */ -int my_mod(int n, int m) { - return (n % m); +/* Compute the greatest common divisor of positive integers */ +int gcd(int x, int y) { + int g; + g = y; + while (x > 0) { + g = x; + x = y % x; + y = g; + } + return g; } -char *get_time() { - long ltime; - time(<ime); - return ctime(<ime); -} diff --git a/Examples/perl5/simple/example.i b/Examples/perl5/simple/example.i index b7b678776..6702abb1e 100644 --- a/Examples/perl5/simple/example.i +++ b/Examples/perl5/simple/example.i @@ -1,11 +1,5 @@ /* File : example.i */ %module example -%{ -/* Put headers and other declarations here */ -%} - -extern double My_variable; -extern int fact(int); -%name(mod) extern int my_mod(int n, int m); -extern char *get_time(); +extern int gcd(int x, int y); +extern double Foo; diff --git a/Examples/perl5/simple/example.pl b/Examples/perl5/simple/example.pl index a65031a5a..b8655be8a 100755 --- a/Examples/perl5/simple/example.pl +++ b/Examples/perl5/simple/example.pl @@ -2,22 +2,22 @@ # Perl5 script for testing simple example use example; -package example; -print get_time(); -print "My Variable = $My_variable","\n"; -for ($i = 0; $i < 14; $i++) { - $n = fact($i); - print "$i factorial is $n\n"; -} +# Call our gcd() function -for ($i = 1; $i < 250; $i++) { - for ($j = 1; $j < 250; $j++) { - $n = mod($i,$j); - $My_variable = $My_variable + $n; - } -} +$x = 42; +$y = 105; +$g = example::gcd($x,$y); +print "The gcd of $x and $y is $g\n"; -print "My_variable = ", $My_variable, "\n"; +# Manipulate the Foo global variable +# Output its current value +print "Foo = $example::Foo\n"; + +# Change its value +$example::Foo = 3.1415926; + +# See if the change took effect +print "Foo = $example::Foo\n"; diff --git a/Examples/perl5/simple/index.html b/Examples/perl5/simple/index.html new file mode 100644 index 000000000..7fca017cc --- /dev/null +++ b/Examples/perl5/simple/index.html @@ -0,0 +1,101 @@ + + +SWIG:Examples:perl5:simple + + + + + +SWIG/Examples/perl5/simple/ +


+ +

Simple Perl5 Example

+ +$Header$
+ +

+This example illustrates how you can hook Perl to a very simple C program containing +a function and a global variable. + +

The C Code

+ +Suppose you have the following C code: + +
+
+/* File : example.c */
+
+/* A global variable */
+double Foo = 3.0;
+
+/* Compute the greatest common divisor of positive integers */
+int gcd(int x, int y) {
+  int g;
+  g = y;
+  while (x > 0) {
+    g = x;
+    x = y % x;
+    y = g;
+  }
+  return g;
+}
+
+
+ +

The SWIG interface

+ +Here is a simple SWIG interface file: + +
+
+/* File: example.i */
+%module example
+
+extern int gcd(int x, int y);
+extern double Foo;
+
+
+ +

Compilation

+ +
    +
  1. swig -perl5 example.i +

    +

  2. This produces two files: example_wrap.c and example.pm. +

    +

  3. Compile example_wrap.c and example.c +to create the extension example.so. +
+ +

Using the extension

+ +Click here to see a script that calls our C functions from Perl. + +

Key points

+ + + +
+ +