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 @@ + +
++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. + +
++is ignored because SWIG has no idea what type of variable this would be. + ++#define EXTERN extern ++
+
+
+The following examples illustrate the use of SWIG with Perl. + +
++ ++% swig -perl5 interface.i ++
++ ++% swig -perl5 interface.i +% gcc -fpic -c -Dbool=char -I/usr/lib/perl5/5.00503/i386-linux/CORE interface_wrap.c +% gcc -shared interface_wrap.o $(OBJS) -o interface.so +% perl +use interface; +... ++
+
+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
+This example illustrates how you can hook Perl to a very simple C program containing
+a function and a global variable.
+
+
+
+
+
+Simple Perl5 Example
+
+$Header$
+
+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
+
+
+
+
+Using the extension
+
+Click here to see a script that calls our C functions from Perl.
+
+Key points
+
+
+
+
+
+
+
+
+use example;
+
+
+
+
+
+$g = example::gcd(42,105);
+
+
+
+
+$a = $example::Foo;
+
+
+
+