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