| 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 |
|
|---|
| 226 | =head2 G_DISCARD
|
|---|
| 227 |
|
|---|
| 228 | By default, the I<call_*> functions place the items returned from
|
|---|
| 229 | by the Perl subroutine on the stack. If you are not interested in
|
|---|
| 230 | these items, then setting this flag will make Perl get rid of them
|
|---|
| 231 | automatically for you. Note that it is still possible to indicate a
|
|---|
| 232 | context to the Perl subroutine by using either G_SCALAR or G_ARRAY.
|
|---|
| 233 |
|
|---|
| 234 | If you do not set this flag then it is I<very> important that you make
|
|---|
| 235 | sure that any temporaries (i.e., parameters passed to the Perl
|
|---|
| 236 | subroutine and values returned from the subroutine) are disposed of
|
|---|
| 237 | yourself. The section I<Returning a Scalar> gives details of how to
|
|---|
| 238 | dispose of these temporaries explicitly and the section I<Using Perl to
|
|---|
| 239 | dispose of temporaries> discusses the specific circumstances where you
|
|---|
| 240 | can ignore the problem and let Perl deal with it for you.
|
|---|
| 241 |
|
|---|
| 242 | =head2 G_NOARGS
|
|---|
| 243 |
|
|---|
| 244 | Whenever a Perl subroutine is called using one of the I<call_*>
|
|---|
| 245 | functions, it is assumed by default that parameters are to be passed to
|
|---|
| 246 | the subroutine. If you are not passing any parameters to the Perl
|
|---|
| 247 | subroutine, you can save a bit of time by setting this flag. It has
|
|---|
| 248 | the effect of not creating the C<@_> array for the Perl subroutine.
|
|---|
| 249 |
|
|---|
| 250 | Although the functionality provided by this flag may seem
|
|---|
| 251 | straightforward, it should be used only if there is a good reason to do
|
|---|
| 252 | so. The reason for being cautious is that even if you have specified
|
|---|
| 253 | the G_NOARGS flag, it is still possible for the Perl subroutine that
|
|---|
| 254 | has been called to think that you have passed it parameters.
|
|---|
| 255 |
|
|---|
| 256 | In fact, what can happen is that the Perl subroutine you have called
|
|---|
| 257 | can access the C<@_> array from a previous Perl subroutine. This will
|
|---|
| 258 | occur when the code that is executing the I<call_*> function has
|
|---|
| 259 | itself been called from another Perl subroutine. The code below
|
|---|
| 260 | illustrates this
|
|---|
| 261 |
|
|---|
| 262 | sub fred
|
|---|
| 263 | { print "@_\n" }
|
|---|
| 264 |
|
|---|
| 265 | sub joe
|
|---|
| 266 | { &fred }
|
|---|
| 267 |
|
|---|
| 268 | &joe(1,2,3);
|
|---|
| 269 |
|
|---|
| 270 | This will print
|
|---|
| 271 |
|
|---|
| 272 | 1 2 3
|
|---|
| 273 |
|
|---|
| 274 | What has happened is that C<fred> accesses the C<@_> array which
|
|---|
| 275 | belongs to C<joe>.
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 | =head2 G_EVAL
|
|---|
| 279 |
|
|---|
| 280 | It is possible for the Perl subroutine you are calling to terminate
|
|---|
| 281 | abnormally, e.g., by calling I<die> explicitly or by not actually
|
|---|
| 282 | existing. By default, when either of these events occurs, the
|
|---|
| 283 | process will terminate immediately. If you want to trap this
|
|---|
| 284 | type of event, specify the G_EVAL flag. It will put an I<eval { }>
|
|---|
| 285 | around the subroutine call.
|
|---|
| 286 |
|
|---|
| 287 | Whenever control returns from the I<call_*> function you need to
|
|---|
| 288 | check the C<$@> variable as you would in a normal Perl script.
|
|---|
| 289 |
|
|---|
| 290 | The value returned from the I<call_*> function is dependent on
|
|---|
| 291 | what other flags have been specified and whether an error has
|
|---|
| 292 | occurred. Here are all the different cases that can occur:
|
|---|
| 293 |
|
|---|
| 294 | =over 5
|
|---|
| 295 |
|
|---|
| 296 | =item *
|
|---|
| 297 |
|
|---|
| 298 | If the I<call_*> function returns normally, then the value
|
|---|
| 299 | returned is as specified in the previous sections.
|
|---|
| 300 |
|
|---|
| 301 | =item *
|
|---|
| 302 |
|
|---|
| 303 | If G_DISCARD is specified, the return value will always be 0.
|
|---|
| 304 |
|
|---|
| 305 | =item *
|
|---|
| 306 |
|
|---|
| 307 | If G_ARRAY is specified I<and> an error has occurred, the return value
|
|---|
| 308 | will always be 0.
|
|---|
| 309 |
|
|---|
| 310 | =item *
|
|---|
| 311 |
|
|---|
| 312 | If G_SCALAR is specified I<and> an error has occurred, the return value
|
|---|
| 313 | will be 1 and the value on the top of the stack will be I<undef>. This
|
|---|
| 314 | means that if you have already detected the error by checking C<$@> and
|
|---|
| 315 | you want the program to continue, you must remember to pop the I<undef>
|
|---|
| 316 | from the stack.
|
|---|
| 317 |
|
|---|
| 318 | =back
|
|---|
| 319 |
|
|---|
| 320 | See I<Using G_EVAL> for details on using G_EVAL.
|
|---|
| 321 |
|
|---|
| 322 | =head2 G_KEEPERR
|
|---|
| 323 |
|
|---|
| 324 | You may have noticed that using the G_EVAL flag described above will
|
|---|
| 325 | B<always> clear the C<$@> variable and set it to a string describing
|
|---|
| 326 | the error iff there was an error in the called code. This unqualified
|
|---|
| 327 | resetting of C<$@> can be problematic in the reliable identification of
|
|---|
| 328 | errors using the C<eval {}> mechanism, because the possibility exists
|
|---|
| 329 | that perl will call other code (end of block processing code, for
|
|---|
| 330 | example) between the time the error causes C<$@> to be set within
|
|---|
| 331 | C<eval {}>, and the subsequent statement which checks for the value of
|
|---|
| 332 | C<$@> gets executed in the user's script.
|
|---|
| 333 |
|
|---|
| 334 | This scenario will mostly be applicable to code that is meant to be
|
|---|
| 335 | called from within destructors, asynchronous callbacks, signal
|
|---|
| 336 | handlers, C<__DIE__> or C<__WARN__> hooks, and C<tie> functions. In
|
|---|
| 337 | such situations, you will not want to clear C<$@> at all, but simply to
|
|---|
| 338 | append any new errors to any existing value of C<$@>.
|
|---|
| 339 |
|
|---|
| 340 | The G_KEEPERR flag is meant to be used in conjunction with G_EVAL in
|
|---|
| 341 | I<call_*> functions that are used to implement such code. This flag
|
|---|
| 342 | has no effect when G_EVAL is not used.
|
|---|
| 343 |
|
|---|
| 344 | When G_KEEPERR is used, any errors in the called code will be prefixed
|
|---|
| 345 | with the string "\t(in cleanup)", and appended to the current value
|
|---|
| 346 | of C<$@>. an error will not be appended if that same error string is
|
|---|
| 347 | already at the end of C<$@>.
|
|---|
| 348 |
|
|---|
| 349 | In addition, a warning is generated using the appended string. This can be
|
|---|
| 350 | disabled using C<no warnings 'misc'>.
|
|---|
| 351 |
|
|---|
| 352 | The G_KEEPERR flag was introduced in Perl version 5.002.
|
|---|
| 353 |
|
|---|
| 354 | See I<Using G_KEEPERR> for an example of a situation that warrants the
|
|---|
| 355 | use of this flag.
|
|---|
| 356 |
|
|---|
| 357 | =head2 Determining the Context
|
|---|
| 358 |
|
|---|
| 359 | As mentioned above, you can determine the context of the currently
|
|---|
| 360 | executing subroutine in Perl with I<wantarray>. The equivalent test
|
|---|
| 361 | can be made in C by using the C<GIMME_V> macro, which returns
|
|---|
| 362 | C<G_ARRAY> if you have been called in a list context, C<G_SCALAR> if
|
|---|
| 363 | in a scalar context, or C<G_VOID> if in a void context (i.e. the
|
|---|
| 364 | return value will not be used). An older version of this macro is
|
|---|
| 365 | called C<GIMME>; in a void context it returns C<G_SCALAR> instead of
|
|---|
| 366 | C<G_VOID>. An example of using the C<GIMME_V> macro is shown in
|
|---|
| 367 | section I<Using GIMME_V>.
|
|---|
| 368 |
|
|---|
| 369 | =head1 EXAMPLES
|
|---|
| 370 |
|
|---|
| 371 | Enough of the definition talk, let's have a few examples.
|
|---|
| 372 |
|
|---|
| 373 | Perl provides many macros to assist in accessing the Perl stack.
|
|---|
| 374 | Wherever possible, these macros should always be used when interfacing
|
|---|
| 375 | to Perl internals. We hope this should make the code less vulnerable
|
|---|
| 376 | to any changes made to Perl in the future.
|
|---|
| 377 |
|
|---|
| 378 | Another point worth noting is that in the first series of examples I
|
|---|
| 379 | have made use of only the I<call_pv> function. This has been done
|
|---|
| 380 | to keep the code simpler and ease you into the topic. Wherever
|
|---|
| 381 | possible, if the choice is between using I<call_pv> and
|
|---|
| 382 | I<call_sv>, you should always try to use I<call_sv>. See
|
|---|
| 383 | I<Using call_sv> for details.
|
|---|
| 384 |
|
|---|
| 385 | =head2 No Parameters, Nothing returned
|
|---|
| 386 |
|
|---|
| 387 | This first trivial example will call a Perl subroutine, I<PrintUID>, to
|
|---|
| 388 | print out the UID of the process.
|
|---|
| 389 |
|
|---|
| 390 | sub PrintUID
|
|---|
| 391 | {
|
|---|
| 392 | print "UID is $<\n";
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | and here is a C function to call it
|
|---|
| 396 |
|
|---|
| 397 | static void
|
|---|
| 398 | call_PrintUID()
|
|---|
| 399 | {
|
|---|
| 400 | dSP;
|
|---|
| 401 |
|
|---|
| 402 | PUSHMARK(SP);
|
|---|
| 403 | call_pv("PrintUID", G_DISCARD|G_NOARGS);
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | Simple, eh.
|
|---|
| 407 |
|
|---|
| 408 | A few points to note about this example.
|
|---|
| 409 |
|
|---|
| 410 | =over 5
|
|---|
| 411 |
|
|---|
| 412 | =item 1.
|
|---|
| 413 |
|
|---|
| 414 | Ignore C<dSP> and C<PUSHMARK(SP)> for now. They will be discussed in
|
|---|
| 415 | the next example.
|
|---|
| 416 |
|
|---|
| 417 | =item 2.
|
|---|
| 418 |
|
|---|
| 419 | We aren't passing any parameters to I<PrintUID> so G_NOARGS can be
|
|---|
| 420 | specified.
|
|---|
| 421 |
|
|---|
| 422 | =item 3.
|
|---|
| 423 |
|
|---|
| 424 | We aren't interested in anything returned from I<PrintUID>, so
|
|---|
| 425 | G_DISCARD is specified. Even if I<PrintUID> was changed to
|
|---|
| 426 | return some value(s), having specified G_DISCARD will mean that they
|
|---|
| 427 | will be wiped by the time control returns from I<call_pv>.
|
|---|
| 428 |
|
|---|
| 429 | =item 4.
|
|---|
| 430 |
|
|---|
| 431 | As I<call_pv> is being used, the Perl subroutine is specified as a
|
|---|
| 432 | C string. In this case the subroutine name has been 'hard-wired' into the
|
|---|
| 433 | code.
|
|---|
| 434 |
|
|---|
| 435 | =item 5.
|
|---|
| 436 |
|
|---|
| 437 | Because we specified G_DISCARD, it is not necessary to check the value
|
|---|
| 438 | returned from I<call_pv>. It will always be 0.
|
|---|
| 439 |
|
|---|
| 440 | =back
|
|---|
| 441 |
|
|---|
| 442 | =head2 Passing Parameters
|
|---|
| 443 |
|
|---|
| 444 | Now let's make a slightly more complex example. This time we want to
|
|---|
| 445 | call a Perl subroutine, C<LeftString>, which will take 2 parameters--a
|
|---|
| 446 | string ($s) and an integer ($n). The subroutine will simply
|
|---|
| 447 | print the first $n characters of the string.
|
|---|
| 448 |
|
|---|
| 449 | So the Perl subroutine would look like this
|
|---|
| 450 |
|
|---|
| 451 | sub LeftString
|
|---|
| 452 | {
|
|---|
| 453 | my($s, $n) = @_;
|
|---|
| 454 | print substr($s, 0, $n), "\n";
|
|---|
| 455 | }
|
|---|
| 456 |
|
|---|
| 457 | The C function required to call I<LeftString> would look like this.
|
|---|
| 458 |
|
|---|
| 459 | static void
|
|---|
| 460 | call_LeftString(a, b)
|
|---|
| 461 | char * a;
|
|---|
| 462 | int b;
|
|---|
| 463 | {
|
|---|
|
|---|