PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1.3.35/Examples/perl5/simple/index.html

#
HTML | 99 lines | 76 code | 23 blank | 0 comment | 0 complexity | f8ed18928511d41e8cd26ebcb3d8a788 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. <html>
  2. <head>
  3. <title>SWIG:Examples:perl5:simple</title>
  4. </head>
  5. <body bgcolor="#ffffff">
  6. <tt>SWIG/Examples/perl5/simple/</tt>
  7. <hr>
  8. <H2>Simple Perl5 Example</H2>
  9. <p>
  10. This example illustrates how you can hook Perl to a very simple C program containing
  11. a function and a global variable.
  12. <h2>The C Code</h2>
  13. Suppose you have the following C code:
  14. <blockquote>
  15. <pre>
  16. /* File : example.c */
  17. /* A global variable */
  18. double Foo = 3.0;
  19. /* Compute the greatest common divisor of positive integers */
  20. int gcd(int x, int y) {
  21. int g;
  22. g = y;
  23. while (x &gt; 0) {
  24. g = x;
  25. x = y % x;
  26. y = g;
  27. }
  28. return g;
  29. }
  30. </pre>
  31. </blockquote>
  32. <h2>The SWIG interface</h2>
  33. Here is a simple SWIG interface file:
  34. <blockquote>
  35. <pre>
  36. /* File: example.i */
  37. %module example
  38. extern int gcd(int x, int y);
  39. extern double Foo;
  40. </pre>
  41. </blockquote>
  42. <h2>Compilation</h2>
  43. <ol>
  44. <li><tt>swig -perl5 <a href="example.i">example.i</a></tt>
  45. <p>
  46. <li>This produces two files: <tt><a href="example_wrap.c">example_wrap.c</a></tt> and <tt><a href="example.pm">example.pm</a></tt>.
  47. <p>
  48. <li>Compile <tt><a href="example_wrap.c">example_wrap.c</a></tt> and <tt><a href="example.c">example.c</a></tt>
  49. to create the extension <tt>example.so</tt>.
  50. </ol>
  51. <h2>Using the extension</h2>
  52. Click <a href="runme.pl">here</a> to see a script that calls our C functions from Perl.
  53. <h2>Key points</h2>
  54. <ul>
  55. <li>Use the <tt>use</tt> statement to load your extension module from Perl. For example:
  56. <blockquote>
  57. <pre>
  58. use example;
  59. </pre>
  60. </blockquote>
  61. <li>C functions work just like Perl functions. For example:
  62. <blockquote>
  63. <pre>
  64. $g = example::gcd(42,105);
  65. </pre>
  66. </blockquote>
  67. <li>C global variables are accessed like ordinary Perl variables. For example:
  68. <blockquote>
  69. <pre>
  70. $a = $example::Foo;
  71. </pre>
  72. </blockquote>
  73. </ul>
  74. <hr>
  75. </body>
  76. </html>