/tags/rel-1-3-25/SWIG/Examples/perl5/variables/index.html

# · HTML · 65 lines · 50 code · 15 blank · 0 comment · 0 complexity · 41b5653827514565f443cc719105f48e MD5 · raw file

  1. <html>
  2. <head>
  3. <title>SWIG:Examples:perl5:variables</title>
  4. </head>
  5. <body bgcolor="#ffffff">
  6. <tt>SWIG/Examples/perl5/variables/</tt>
  7. <hr>
  8. <H2>Wrapping C Global Variables</H2>
  9. <tt>$Header$</tt><br>
  10. <p>
  11. When a C global variable appears in an interface file, SWIG tries to
  12. wrap it using a technique known as "variable linking." The idea is
  13. pretty simple---we try to create a Perl variable that magically
  14. retrieves or updates the value of the underlying C variable when it is
  15. accessed. Click <a href="example.i">here</a> to see a SWIG interface with some variable
  16. declarations in it.
  17. <h2>Manipulating Variables from Perl</h2>
  18. Accessing a C global variable from Perl is easy---just reference it like a normal Perl variable.
  19. Click <a href="runme.pl">here</a> to see a script that updates and prints some global variables.
  20. <h2>Creating read-only variables</h2>
  21. The <tt>%immutable</tt> and <tt>%mutable</tt> directives can be used to
  22. specify a collection of read-only variables. For example:
  23. <blockquote>
  24. <pre>
  25. %immutable;
  26. int status;
  27. double blah;
  28. ...
  29. %mutable;
  30. </pre>
  31. </blockquote>
  32. The <tt>%immutable</tt> directive remains in effect until it is explicitly disabled
  33. using the <tt>%mutable</tt> directive.
  34. <h2>Notes:</h2>
  35. <ul>
  36. <li>When a global variable has the type "<tt>char *</tt>", SWIG manages it as a character
  37. string. However, whenever the value of such a variable is set from Perl, the old
  38. value is destroyed using <tt>free()</tt> or <tt>delete</tt> (the choice of which depends
  39. on whether or not SWIG was run with the -c++ option).
  40. <li><tt>signed char</tt> and <tt>unsigned char</tt> are handled as small 8-bit integers.
  41. <li>String array variables such as '<tt>char name[256]</tt>' are managed as Perl strings, but
  42. when setting the value, the result is truncated to the maximum length of the array. Furthermore, the string is assumed to be null-terminated.
  43. <li>When structures and classes are used as global variables, they are mapped into pointers.
  44. Getting the "value" returns a pointer to the global variable. Setting the value of a structure results in a memory copy from a pointer to the global.
  45. <li>Variables are linked using Perl's magic mechanism. Take a look at the Advanced Perl Programming book to
  46. find out more about this feature.
  47. </ul>
  48. </body>
  49. </html>
  50. <hr>