source: trunk/essentials/dev-lang/perl/pod/perlcall.pod@ 3951

Last change on this file since 3951 was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 52.3 KB
Line 
1=head1 NAME
2
3perlcall - Perl calling conventions from C
4
5=head1 DESCRIPTION
6
7The purpose of this document is to show you how to call Perl subroutines
8directly from C, i.e., how to write I<callbacks>.
9
10Apart from discussing the C interface provided by Perl for writing
11callbacks the document uses a series of examples to show how the
12interface actually works in practice. In addition some techniques for
13coding callbacks are covered.
14
15Examples where callbacks are necessary include
16
17=over 5
18
19=item * An Error Handler
20
21You have created an XSUB interface to an application's C API.
22
23A fairly common feature in applications is to allow you to define a C
24function that will be called whenever something nasty occurs. What we
25would like is to be able to specify a Perl subroutine that will be
26called instead.
27
28=item * An Event Driven Program
29
30The classic example of where callbacks are used is when writing an
31event driven program like for an X windows application. In this case
32you register functions to be called whenever specific events occur,
33e.g., a mouse button is pressed, the cursor moves into a window or a
34menu item is selected.
35
36=back
37
38Although the techniques described here are applicable when embedding
39Perl in a C program, this is not the primary goal of this document.
40There are other details that must be considered and are specific to
41embedding Perl. For details on embedding Perl in C refer to
42L<perlembed>.
43
44Before you launch yourself head first into the rest of this document,
45it would be a good idea to have read the following two documents -
46L<perlxs> and L<perlguts>.
47
48=head1 THE CALL_ FUNCTIONS
49
50Although this stuff is easier to explain using examples, you first need
51be aware of a few important definitions.
52
53Perl has a number of C functions that allow you to call Perl
54subroutines. They are
55
56 I32 call_sv(SV* sv, I32 flags);
57 I32 call_pv(char *subname, I32 flags);
58 I32 call_method(char *methname, I32 flags);
59 I32 call_argv(char *subname, I32 flags, register char **argv);
60
61The key function is I<call_sv>. All the other functions are
62fairly simple wrappers which make it easier to call Perl subroutines in
63special cases. At the end of the day they will all call I<call_sv>
64to invoke the Perl subroutine.
65
66All the I<call_*> functions have a C<flags> parameter which is
67used to pass a bit mask of options to Perl. This bit mask operates
68identically for each of the functions. The settings available in the
69bit mask are discussed in L<FLAG VALUES>.
70
71Each of the functions will now be discussed in turn.
72
73=over 5
74
75=item call_sv
76
77I<call_sv> takes two parameters, the first, C<sv>, is an SV*.
78This allows you to specify the Perl subroutine to be called either as a
79C string (which has first been converted to an SV) or a reference to a
80subroutine. The section, I<Using call_sv>, shows how you can make
81use of I<call_sv>.
82
83=item call_pv
84
85The function, I<call_pv>, is similar to I<call_sv> except it
86expects its first parameter to be a C char* which identifies the Perl
87subroutine you want to call, e.g., C<call_pv("fred", 0)>. If the
88subroutine you want to call is in another package, just include the
89package name in the string, e.g., C<"pkg::fred">.
90
91=item call_method
92
93The function I<call_method> is used to call a method from a Perl
94class. The parameter C<methname> corresponds to the name of the method
95to be called. Note that the class that the method belongs to is passed
96on the Perl stack rather than in the parameter list. This class can be
97either the name of the class (for a static method) or a reference to an
98object (for a virtual method). See L<perlobj> for more information on
99static and virtual methods and L<Using call_method> for an example
100of using I<call_method>.
101
102=item call_argv
103
104I<call_argv> calls the Perl subroutine specified by the C string
105stored in the C<subname> parameter. It also takes the usual C<flags>
106parameter. The final parameter, C<argv>, consists of a NULL terminated
107list of C strings to be passed as parameters to the Perl subroutine.
108See I<Using call_argv>.
109
110=back
111
112All the functions return an integer. This is a count of the number of
113items returned by the Perl subroutine. The actual items returned by the
114subroutine are stored on the Perl stack.
115
116As a general rule you should I<always> check the return value from
117these functions. Even if you are expecting only a particular number of
118values to be returned from the Perl subroutine, there is nothing to
119stop someone from doing something unexpected--don't say you haven't
120been warned.
121
122=head1 FLAG VALUES
123
124The C<flags> parameter in all the I<call_*> functions is a bit mask
125which can consist of any combination of the symbols defined below,
126OR'ed together.
127
128
129=head2 G_VOID
130
131Calls the Perl subroutine in a void context.
132
133This flag has 2 effects:
134
135=over 5
136
137=item 1.
138
139It indicates to the subroutine being called that it is executing in
140a void context (if it executes I<wantarray> the result will be the
141undefined value).
142
143=item 2.
144
145It ensures that nothing is actually returned from the subroutine.
146
147=back
148
149The value returned by the I<call_*> function indicates how many
150items have been returned by the Perl subroutine - in this case it will
151be 0.
152
153
154=head2 G_SCALAR
155
156Calls the Perl subroutine in a scalar context. This is the default
157context flag setting for all the I<call_*> functions.
158
159This flag has 2 effects:
160
161=over 5
162
163=item 1.
164
165It indicates to the subroutine being called that it is executing in a
166scalar context (if it executes I<wantarray> the result will be false).
167
168=item 2.
169
170It ensures that only a scalar is actually returned from the subroutine.
171The subroutine can, of course, ignore the I<wantarray> and return a
172list anyway. If so, then only the last element of the list will be
173returned.
174
175=back
176
177The value returned by the I<call_*> function indicates how many
178items have been returned by the Perl subroutine - in this case it will
179be either 0 or 1.
180
181If 0, then you have specified the G_DISCARD flag.
182
183If 1, then the item actually returned by the Perl subroutine will be
184stored on the Perl stack - the section I<Returning a Scalar> shows how
185to access this value on the stack. Remember that regardless of how
186many items the Perl subroutine returns, only the last one will be
187accessible from the stack - think of the case where only one value is
188returned as being a list with only one element. Any other items that
189were returned will not exist by the time control returns from the
190I<call_*> function. The section I<Returning a list in a scalar
191context> shows an example of this behavior.
192
193
194=head2 G_ARRAY
195
196Calls the Perl subroutine in a list context.
197
198As with G_SCALAR, this flag has 2 effects:
199
200=over 5
201
202=item 1.
203
204It indicates to the subroutine being called that it is executing in a
205list context (if it executes I<wantarray> the result will be true).
206
207
208=item 2.
209
210It ensures that all items returned from the subroutine will be
211accessible when control returns from the I<call_*> function.
212
213=back
214
215The value returned by the I<call_*> function indicates how many
216items have been returned by the Perl subroutine.
217
218If 0, then you have specified the G_DISCARD flag.
219
220If not 0, then it will be a count of the number of items returned by
221the subroutine. These items will be stored on the Perl stack. The
222section I<Returning a list of values> gives an example of using the
223G_ARRAY flag and the mechanics of accessing the returned items from the
224Perl stack.
225