git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10005 626c5289-ae23-0410-ae9c-e8d60b6d4f22
63 lines
2.2 KiB
HTML
63 lines
2.2 KiB
HTML
<html>
|
|
<head>
|
|
<title>SWIG:Examples:perl5:variables</title>
|
|
</head>
|
|
|
|
<body bgcolor="#ffffff">
|
|
|
|
<tt>SWIG/Examples/perl5/variables/</tt>
|
|
<hr>
|
|
|
|
<H2>Wrapping C Global Variables</H2>
|
|
|
|
<p>
|
|
When a C global variable appears in an interface file, SWIG tries to
|
|
wrap it using a technique known as "variable linking." The idea is
|
|
pretty simple---we try to create a Perl variable that magically
|
|
retrieves or updates the value of the underlying C variable when it is
|
|
accessed. Click <a href="example.i">here</a> to see a SWIG interface with some variable
|
|
declarations in it.
|
|
|
|
<h2>Manipulating Variables from Perl</h2>
|
|
|
|
Accessing a C global variable from Perl is easy---just reference it like a normal Perl variable.
|
|
Click <a href="runme.pl">here</a> to see a script that updates and prints some global variables.
|
|
|
|
<h2>Creating read-only variables</h2>
|
|
|
|
The <tt>%immutable</tt> and <tt>%mutable</tt> directives can be used to
|
|
specify a collection of read-only variables. For example:
|
|
|
|
<blockquote>
|
|
<pre>
|
|
%immutable;
|
|
int status;
|
|
double blah;
|
|
...
|
|
%mutable;
|
|
</pre>
|
|
</blockquote>
|
|
|
|
The <tt>%immutable</tt> directive remains in effect until it is explicitly disabled
|
|
using the <tt>%mutable</tt> directive.
|
|
|
|
|
|
<h2>Notes:</h2>
|
|
|
|
<ul>
|
|
<li>When a global variable has the type "<tt>char *</tt>", SWIG manages it as a character
|
|
string. However, whenever the value of such a variable is set from Perl, the old
|
|
value is destroyed using <tt>free()</tt> or <tt>delete</tt> (the choice of which depends
|
|
on whether or not SWIG was run with the -c++ option).
|
|
<li><tt>signed char</tt> and <tt>unsigned char</tt> are handled as small 8-bit integers.
|
|
<li>String array variables such as '<tt>char name[256]</tt>' are managed as Perl strings, but
|
|
when setting the value, the result is truncated to the maximum length of the array. Furthermore, the string is assumed to be null-terminated.
|
|
<li>When structures and classes are used as global variables, they are mapped into pointers.
|
|
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.
|
|
<li>Variables are linked using Perl's magic mechanism. Take a look at the Advanced Perl Programming book to
|
|
find out more about this feature.
|
|
</ul>
|
|
|
|
</body>
|
|
</html>
|
|
<hr>
|