.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Inline::C 3" .TH Inline::C 3 2024-09-01 "perl v5.40.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Inline::C \- C Language Support for Inline .SH VERSION .IX Header "VERSION" This document describes Inline::C version \fB0.82\fR. .SH DESCRIPTION .IX Header "DESCRIPTION" \&\f(CW\*(C`Inline::C\*(C'\fR is a module that allows you to write Perl subroutines in C. Since version 0.30 the Inline module supports multiple programming languages and each language has its own support module. This document describes how to use Inline with the C programming language. It also goes a bit into Perl C internals. .PP If you want to start working with programming examples right away, check out Inline::C::Cookbook. For more information on Inline in general, see Inline. .SH USAGE .IX Header "USAGE" You never actually use \f(CW\*(C`Inline::C\*(C'\fR directly. It is just a support module for using \f(CW\*(C`Inline.pm\*(C'\fR with C. So the usage is always: .PP .Vb 1 \& use Inline C => ...; .Ve .PP or .PP .Vb 1 \& bind Inline C => ...; .Ve .SH "FUNCTION DEFINITIONS" .IX Header "FUNCTION DEFINITIONS" The Inline grammar for C recognizes certain function definitions (or signatures) in your C code. If a signature is recognized by Inline, then it will be available in Perl-space. That is, Inline will generate the "glue" necessary to call that function as if it were a Perl subroutine. If the signature is not recognized, Inline will simply ignore it, with no complaints. It will not be available from Perl-space, although it \fIwill\fR be available from C\-space. .PP Inline looks for ANSI/prototype style function definitions. They must be of the form: .PP .Vb 1 \& return\-type function\-name ( type\-name\-pairs ) { ... } .Ve .PP The most common types are: \f(CW\*(C`int\*(C'\fR, \f(CW\*(C`long\*(C'\fR, \f(CW\*(C`double\*(C'\fR, \f(CW\*(C`char*\*(C'\fR, and \f(CW\*(C`SV*\*(C'\fR. But you can use any type for which Inline can find a typemap. Inline uses the \&\f(CW\*(C`typemap\*(C'\fR file distributed with Perl as the default. You can specify more typemaps with the \f(CW\*(C`typemaps\*(C'\fR configuration option. .PP A return type of \f(CW\*(C`void\*(C'\fR may also be used. The following are examples of valid function definitions. .PP .Vb 9 \& int Foo(double num, char* str) { \& void Foo(double num, char* str) { \& void Foo(SV*, ...) { \& long Foo(int i, int j, ...) { \& SV* Foo(void) { # \*(Aqvoid\*(Aq arg invalid with the ParseRecDescent parser. \& # Works only with the ParseRegExp parser. \& # See the section on \`using\` (below). \& SV* Foo() { # Alternative to specifying \*(Aqvoid\*(Aq arg. Is valid with \& # both the ParseRecDescent and ParseRegExp parsers. .Ve .PP The following definitions would not be recognized: .PP .Vb 3 \& Foo(int i) { # no return type \& int Foo(float f) { # no (default) typemap for float \& int Foo(num, str) double num; char* str; { .Ve .PP Notice that Inline only looks for function \fIdefinitions\fR, not function \&\fIprototypes\fR. Definitions are the syntax directly preceding a function body. Also Inline does not scan external files, like headers. Only the code passed to Inline is used to create bindings; although other libraries can linked in, and called from C\-space. .SH "C CONFIGURATION OPTIONS" .IX Header "C CONFIGURATION OPTIONS" For information on how to specify Inline configuration options, see Inline. This section describes each of the configuration options available for C. Most of the options correspond either to MakeMaker or XS options of the same name. See ExtUtils::MakeMaker and perlxs. .ie n .IP """auto_include""" 4 .el .IP \f(CWauto_include\fR 4 .IX Item "auto_include" Specifies extra statements to automatically included. They will be added onto the defaults. A newline char will be automatically added. .Sp .Vb 1 \& use Inline C => config => auto_include => \*(Aq#include "yourheader.h"\*(Aq; .Ve .ie n .IP """autowrap""" 4 .el .IP \f(CWautowrap\fR 4 .IX Item "autowrap" If you \f(CW\*(C`enable => autowrap\*(C'\fR, Inline::C will parse function declarations (prototype statements) in your C code. For each declaration it can bind to, it will create a dummy wrapper that will call the real function which may be in an external library. This is a nice convenience for functions that would otherwise just require an empty wrapper function. .Sp This is similar to the base functionality you get from \f(CW\*(C`h2xs\*(C'\fR. It can be very useful for binding to external libraries. .ie n .IP """boot""" 4 .el .IP \f(CWboot\fR 4 .IX Item "boot" Specifies C code to be executed in the XS \f(CW\*(C`BOOT\*(C'\fR section. Corresponds to the XS parameter. .ie n .IP """cc""" 4 .el .IP \f(CWcc\fR 4 .IX Item "cc" Specify which compiler to use. .ie n .IP """ccflags""" 4 .el .IP \f(CWccflags\fR 4 .IX Item "ccflags" Specify compiler flags \- same as ExtUtils::MakeMaker's \f(CW\*(C`CCFLAGS\*(C'\fR option. Whatever gets specified here replaces the default \f(CW$Config{ccflags}\fR. Often, you'll want to add an extra flag or two without clobbering the default flags in which case you could instead use \f(CW\*(C`ccflagsex\*(C'\fR (see below) or, if Config.pm has already been loaded: .Sp .Vb 1 \& use Inline C => Config => ccflags => $Config{ccflags} . " \-DXTRA \-DTOO"; .Ve .ie n .IP """ccflagsex""" 4 .el .IP \f(CWccflagsex\fR 4 .IX Item "ccflagsex" Extend compiler flags. Sets \f(CW\*(C`CCFLAGS\*(C'\fR to \f(CW$Config\fR{ccflags} followed by a space, followed by the specified value: .Sp .Vb 1 \& use Inline C => config => ccflagsex => "\-DXTRA \-DTOO"; .Ve .ie n .IP """cppflags""" 4 .el .IP \f(CWcppflags\fR 4 .IX Item "cppflags" .PP Specify preprocessor flags. Passed to \f(CW\*(C`cpp\*(C'\fR C preprocessor by \f(CWPreprocess()\fR in Inline::Filters. .PP .Vb 6 \& use Inline C => <<\*(AqEND\*(Aq, \& CPPFLAGS => \*(Aq \-DPREPROCESSOR_DEFINE\*(Aq, \& FILTERS => \*(AqPreprocess\*(Aq; \& use Inline C => <<\*(AqEND\*(Aq, \& CPPFLAGS => \*(Aq \-DPREPROCESSOR_DEFINE=4321\*(Aq, \& FILTERS => \*(AqPreprocess\*(Aq; .Ve .ie n .IP """filters""" 4 .el .IP \f(CWfilters\fR 4 .IX Item "filters" Allows you to specify a list of source code filters. If more than one is requested, be sure to group them with an array ref. The filters can either be subroutine references or names of filters provided by the supplementary Inline::Filters module. .Sp Your source code will be filtered just before it is parsed by Inline. The MD5 fingerprint is generated before filtering. Source code filters can be used to do things like stripping out POD documentation, pre-expanding \f(CW\*(C`#include\*(C'\fR statements or whatever else you please. For example: .Sp .Vb 2 \& use Inline C => DATA => \& filters => [Strip_POD => \e&MyFilter => Preprocess ]; .Ve .Sp Filters are invoked in the order specified. See Inline::Filters for more information. .Sp If a filter is an array reference, it is assumed to be a usage of a filter plug\- in named by the first element of that array reference. The rest of the elements of the array reference are used as arguments to the filter. For example, consider a \f(CW\*(C`filters\*(C'\fR parameter like this: .Sp .Vb 1 \& use Inline C => DATA => filters => [ [ Ragel => \*(Aq\-G2\*(Aq ] ]; .Ve .Sp In order for Inline::C to process this filter, it will attempt to require the module Inline::Filters::Ragel and will then call the \f(CW\*(C`filter\*(C'\fR function in that package with the argument \f(CW\*(Aq\-G2\*(Aq\fR. This function will return the actual filtering function. .ie n .IP """inc""" 4 .el .IP \f(CWinc\fR 4 .IX Item "inc" Specifies an include path to use. Corresponds to the MakeMaker parameter. Expects a fully qualified path. .Sp .Vb 1 \& use Inline C => config => inc => \*(Aq\-I/inc/path\*(Aq; .Ve .ie n .IP """ld""" 4 .el .IP \f(CWld\fR 4 .IX Item "ld" Specify which linker to use. .ie n .IP """lddlflags""" 4 .el .IP \f(CWlddlflags\fR 4 .IX Item "lddlflags" Specify which linker flags to use. .Sp NOTE: These flags will completely override the existing flags, instead of just adding to them. So if you need to use those too, you must respecify them here. .ie n .IP """libs""" 4 .el .IP \f(CWlibs\fR 4 .IX Item "libs" Specifies external libraries that should be linked into your code. Corresponds to the MakeMaker parameter. Provide a fully qualified path with the \f(CW\*(C`\-L\*(C'\fR switch if the library is in a location where it won't be found automatically. .Sp .Vb 1 \& use Inline C => config => libs => \*(Aq\-lyourlib\*(Aq; .Ve .Sp or .Sp .Vb 1 \& use Inline C => config => libs => \*(Aq\-L/your/path \-lyourlib\*(Aq; .Ve .ie n .IP """make""" 4 .el .IP \f(CWmake\fR 4 .IX Item "make" Specify the name of the 'make' utility to use. .ie n .IP """myextlib""" 4 .el .IP \f(CWmyextlib\fR 4 .IX Item "myextlib" Specifies a user compiled object that should be linked in. Corresponds to the MakeMaker parameter. Expects a fully qualified path. .Sp .Vb 1 \& use Inline C => config => myextlib => \*(Aq/your/path/yourmodule.so\*(Aq; .Ve .ie n .IP """optimize""" 4 .el .IP \f(CWoptimize\fR 4 .IX Item "optimize" This controls the MakeMaker \f(CW\*(C`OPTIMIZE\*(C'\fR setting. By setting this value to \&\f(CW\*(Aq\-g\*(Aq\fR, you can turn on debugging support for your Inline extensions. This will allow you to be able to set breakpoints in your C code using a debugger like gdb. .ie n .IP """prefix""" 4 .el .IP \f(CWprefix\fR 4 .IX Item "prefix" Specifies a prefix that will be automatically stripped from C functions when they are bound to Perl. Useful for creating wrappers for shared library API-s, and binding to the original names in Perl. Also useful when names conflict with Perl internals. Corresponds to the XS parameter. .Sp .Vb 1 \& use Inline C => config => prefix => \*(AqZLIB_\*(Aq; .Ve .ie n .IP """pre_head""" 4 .el .IP \f(CWpre_head\fR 4 .IX Item "pre_head" Specifies code that will precede the inclusion of all files specified in \&\f(CW\*(C`auto_include\*(C'\fR (ie \f(CW\*(C`EXTERN.h\*(C'\fR, \f(CW\*(C`perl.h\*(C'\fR, \f(CW\*(C`XSUB.h\*(C'\fR, \f(CW\*(C`INLINE.h\*(C'\fR and anything else that might have been added to \f(CW\*(C`auto_include\*(C'\fR by the user). If the specified value identifies a file, the contents of that file will be inserted, otherwise the specified value is inserted. .Sp .Vb 1 \& use Inline C => config => pre_head => $code_or_filename; .Ve .ie n .IP """prototype""" 4 .el .IP \f(CWprototype\fR 4 .IX Item "prototype" Corresponds to the XS keyword 'PROTOTYPE'. See the perlxs documentation for both 'PROTOTYPES' and 'PROTOTYPE'. As an example, the following will set the PROTOTYPE of the 'foo' function to '$', and disable prototyping for the \&'bar' function. .Sp .Vb 1 \& use Inline C => config => prototype => {foo => \*(Aq$\*(Aq, bar => \*(AqDISABLE\*(Aq} .Ve .ie n .IP """prototypes""" 4 .el .IP \f(CWprototypes\fR 4 .IX Item "prototypes" Corresponds to the XS keyword 'PROTOTYPES'. Can take only values of 'ENABLE' or 'DISABLE'. (Contrary to XS, default value is 'DISABLE'). See the perlxs documentation for both 'PROTOTYPES' and 'PROTOTYPE'. .Sp .Vb 1 \& use Inline C => config => prototypes => \*(AqENABLE\*(Aq; .Ve .ie n .IP """typemaps""" 4 .el .IP \f(CWtypemaps\fR 4 .IX Item "typemaps" Specifies extra typemap files to use. These types will modify the behaviour of the C parsing. Corresponds to the MakeMaker parameter. Specify either a fully qualified path or a path relative to the cwd (ie relative to what the cwd is at the time the script is loaded). .Sp .Vb 1 \& use Inline C => config => typemaps => \*(Aq/your/path/typemap\*(Aq; .Ve .ie n .IP """using""" 4 .el .IP \f(CWusing\fR 4 .IX Item "using" Specifies which parser to use. The default is Inline::C::Parser::RecDescent, which uses the Parse::RecDescent module. .Sp The other options are \f(CW\*(C`::Parser::Pegex\*(C'\fR and \f(CW\*(C`::Parser::RegExp\*(C'\fR, which uses the Inline::C::Parser::Pegex and Inline::C::Parser::RegExp modules that ship with Inline::C. .Sp .Vb 1 \& use Inline C => config => using => \*(Aq::Parser::Pegex\*(Aq; .Ve .Sp Note that the following old options are deprecated, but still work at this time: .RS 4 .IP \(bu 4 \&\f(CW\*(C`ParseRecDescent\*(C'\fR .IP \(bu 4 \&\f(CW\*(C`ParseRegExp\*(C'\fR .IP \(bu 4 \&\f(CW\*(C`ParsePegex\*(C'\fR .RE .RS 4 .RE .SH "C\-PERL BINDINGS" .IX Header "C-PERL BINDINGS" This section describes how the \f(CW\*(C`Perl\*(C'\fR variables get mapped to \f(CW\*(C`C\*(C'\fR variables and back again. .PP First, you need to know how \f(CW\*(C`Perl\*(C'\fR passes arguments back and forth to subroutines. Basically it uses a stack (also known as the \fBStack\fR). When a sub is called, all of the parenthesized arguments get expanded into a list of scalars and pushed onto the \fBStack\fR. The subroutine then pops all of its parameters off of the \fBStack\fR. When the sub is done, it pushes all of its return values back onto the \fBStack\fR. .PP The \fBStack\fR is an array of scalars known internally as \f(CW\*(C`SV\*(C'\fR's. The \fBStack\fR is actually an array of \fBpointers to SV\fR or \f(CW\*(C`SV*\*(C'\fR; therefore every element of the \fBStack\fR is natively a \f(CW\*(C`SV*\*(C'\fR. For \fIFMTYEWTK\fR about this, read \&\f(CW\*(C`perldoc perlguts\*(C'\fR. .PP So back to variable mapping. XS uses a thing known as "typemaps" to turn each \&\f(CW\*(C`SV*\*(C'\fR into a \f(CW\*(C`C\*(C'\fR type and back again. This is done through various XS macro calls, casts and the Perl API. See \f(CW\*(C`perldoc perlapi\*(C'\fR. XS allows you to define your own typemaps as well for fancier non-standard types such as \f(CW\*(C`typedef\*(C'\fR\- ed structs. .PP Inline uses the default Perl typemap file for its default types. This file is called \f(CW\*(C`/usr/local/lib/perl5/5.6.1/ExtUtils/typemap\*(C'\fR, or something similar, depending on your Perl installation. It has definitions for over 40 types, which are automatically used by Inline. (You should probably browse this file at least once, just to get an idea of the possibilities.) .PP Inline parses your code for these types and generates the XS code to map them. The most commonly used types are: .IP \(bu 4 \&\f(CW\*(C`int\*(C'\fR .IP \(bu 4 \&\f(CW\*(C`long\*(C'\fR .IP \(bu 4 \&\f(CW\*(C`double\*(C'\fR .IP \(bu 4 \&\f(CW\*(C`char*\*(C'\fR .IP \(bu 4 \&\f(CW\*(C`void\*(C'\fR .IP \(bu 4 \&\f(CW\*(C`SV*\*(C'\fR .PP If you need to deal with a type that is not in the defaults, just use the generic \f(CW\*(C`SV*\*(C'\fR type in the function definition. Then inside your code, do the mapping yourself. Alternatively, you can create your own typemap files and specify them using the \f(CW\*(C`typemaps\*(C'\fR configuration option. .PP A return type of \f(CW\*(C`void\*(C'\fR has a special meaning to Inline. It means that you plan to push the values back onto the \fBStack\fR yourself. This is what you need to do to return a list of values. If you really don't want to return anything (the traditional meaning of \f(CW\*(C`void\*(C'\fR) then simply don't push anything back. .PP If ellipsis or \f(CW\*(C`...\*(C'\fR is used at the end of an argument list, it means that any number of \f(CW\*(C`SV*\*(C'\fRs may follow. Again you will need to pop the values off of the \f(CW\*(C`Stack\*(C'\fR yourself. .PP See "EXAMPLES" below. .SH "THE INLINE STACK MACROS" .IX Header "THE INLINE STACK MACROS" When you write Inline C, the following lines are automatically prepended to your code (by default): .PP .Vb 4 \& #include "EXTERN.h" \& #include "perl.h" \& #include "XSUB.h" \& #include "INLINE.h" .Ve .PP The file \f(CW\*(C`INLINE.h\*(C'\fR defines a set of macros that are useful for handling the Perl Stack from your C functions. .ie n .IP """Inline_Stack_Vars""" 4 .el .IP \f(CWInline_Stack_Vars\fR 4 .IX Item "Inline_Stack_Vars" You'll need to use this one, if you want to use the others. It sets up a few local variables: \f(CW\*(C`sp\*(C'\fR, \f(CW\*(C`items\*(C'\fR, \f(CW\*(C`ax\*(C'\fR and \f(CW\*(C`mark\*(C'\fR, for use by the other macros. It's not important to know what they do, but I mention them to avoid possible name conflicts. .Sp NOTE: Since this macro declares variables, you'll need to put it with your other variable declarations at the top of your function. It must come before any executable statements and before any other \f(CW\*(C`Inline_Stack\*(C'\fR macros. .ie n .IP """Inline_Stack_Items""" 4 .el .IP \f(CWInline_Stack_Items\fR 4 .IX Item "Inline_Stack_Items" Returns the number of arguments passed in on the Stack. .ie n .IP Inline_Stack_Item(i) 4 .el .IP \f(CWInline_Stack_Item(i)\fR 4 .IX Item "Inline_Stack_Item(i)" Refers to a particular \f(CW\*(C`SV*\*(C'\fR in the Stack, where \f(CW\*(C`i\*(C'\fR is an index number starting from zero. Can be used to get or set the value. .ie n .IP """Inline_Stack_Reset""" 4 .el .IP \f(CWInline_Stack_Reset\fR 4 .IX Item "Inline_Stack_Reset" Use this before pushing anything back onto the Stack. It resets the internal Stack pointer to the beginning of the Stack. .ie n .IP Inline_Stack_Push(sv) 4 .el .IP \f(CWInline_Stack_Push(sv)\fR 4 .IX Item "Inline_Stack_Push(sv)" Push a return value back onto the Stack. The value must be of type \f(CW\*(C`SV*\*(C'\fR. .ie n .IP """Inline_Stack_Done""" 4 .el .IP \f(CWInline_Stack_Done\fR 4 .IX Item "Inline_Stack_Done" After you have pushed all of your return values, you must call this macro. .ie n .IP Inline_Stack_Return(n) 4 .el .IP \f(CWInline_Stack_Return(n)\fR 4 .IX Item "Inline_Stack_Return(n)" Return \f(CW\*(C`n\*(C'\fR items on the Stack. .ie n .IP """Inline_Stack_Void""" 4 .el .IP \f(CWInline_Stack_Void\fR 4 .IX Item "Inline_Stack_Void" A special macro to indicate that you really don't want to return anything. Same as: .Sp .Vb 1 \& Inline_Stack_Return(0); .Ve .Sp Please note that this macro actually \fBreturns\fR from your function. .PP Each of these macros is available in 3 different styles to suit your coding tastes. The following macros are equivalent. .PP .Vb 3 \& Inline_Stack_Vars \& inline_stack_vars \& INLINE_STACK_VARS .Ve .PP All of this functionality is available through XS macro calls as well. So why duplicate the functionality? There are a few reasons why I decided to offer this set of macros. First, as a convenient way to access the Stack. Second, for consistent, self documenting, non-cryptic coding. Third, for future compatibility. It occurred to me that if a lot of people started using XS macros for their C code, the interface might break under Perl6. By using this set, hopefully I will be able to insure future compatibility of argument handling. .PP Of course, if you use the rest of the Perl API, your code will most likely break under Perl6. So this is not a 100% guarantee. But since argument handling is the most common interface you're likely to use, it seemed like a wise thing to do. .SH "WRITING C SUBROUTINES" .IX Header "WRITING C SUBROUTINES" The definitions of your C functions will fall into one of the following four categories. For each category there are special considerations. .ie n .IP """int Foo(int arg1, char* arg2, SV* arg3) {""" 4 .el .IP "\f(CWint Foo(int arg1, char* arg2, SV* arg3) {\fR" 4 .IX Item "int Foo(int arg1, char* arg2, SV* arg3) {" This is the simplest case. You have a non \f(CW\*(C`void\*(C'\fR return type and a fixed length argument list. You don't need to worry about much. All the conversions will happen automatically. .ie n .IP """void Foo(int arg1, char* arg2, SV* arg3) {""" 4 .el .IP "\f(CWvoid Foo(int arg1, char* arg2, SV* arg3) {\fR" 4 .IX Item "void Foo(int arg1, char* arg2, SV* arg3) {" In this category you have a \f(CW\*(C`void\*(C'\fR return type. This means that either you want to return nothing, or that you want to return a list. In the latter case you'll need to push values onto the \fBStack\fR yourself. There are a few Inline macros that make this easy. Code something like this: .Sp .Vb 6 \& int i, max; SV* my_sv[10]; \& Inline_Stack_Vars; \& Inline_Stack_Reset; \& for (i = 0; i < max; i++) \& Inline_Stack_Push(my_sv[i]); \& Inline_Stack_Done; .Ve .Sp After resetting the Stack pointer, this code pushes a series of return values. At the end it uses \f(CW\*(C`Inline_Stack_Done\*(C'\fR to mark the end of the return stack. .Sp If you really want to return nothing, then don't use the \f(CW\*(C`Inline_Stack_\*(C'\fR macros. If you must use them, then set use \f(CW\*(C`Inline_Stack_Void\*(C'\fR at the end of your function. .ie n .IP """char* Foo(SV* arg1, ...) {""" 4 .el .IP "\f(CWchar* Foo(SV* arg1, ...) {\fR" 4 .IX Item "char* Foo(SV* arg1, ...) {" In this category you have an unfixed number of arguments. This means that you'll have to pop values off the \fBStack\fR yourself. Do it like this: .Sp .Vb 4 \& int i; \& Inline_Stack_Vars; \& for (i = 0; i < Inline_Stack_Items; i++) \& handle_sv(Inline_Stack_Item(i)); .Ve .Sp The return type of \f(CWInline_Stack_Item(i)\fR is \f(CW\*(C`SV*\*(C'\fR. .ie n .IP """void* Foo(SV* arg1, ...) {""" 4 .el .IP "\f(CWvoid* Foo(SV* arg1, ...) {\fR" 4 .IX Item "void* Foo(SV* arg1, ...) {" In this category you have both a \f(CW\*(C`void\*(C'\fR return type and an unfixed number of arguments. Just combine the techniques from Categories 3 and 4. .SH EXAMPLES .IX Header "EXAMPLES" Here are a few examples. Each one is a complete program that you can try running yourself. For many more examples see Inline::C::Cookbook. .SS "Example #1 \- Greetings" .IX Subsection "Example #1 - Greetings" This example will take one string argument (a name) and print a greeting. The function is called with a string and with a number. In the second case the number is forced to a string. .PP Notice that you do not need to \f(CW\*(C`#include . The \f(CW\*(C`perl.h\*(C'\fR header file which gets included by default, automatically loads the standard C header files for you. .PP .Vb 8 \& use Inline \*(AqC\*(Aq; \& greet(\*(AqIngy\*(Aq); \& greet(42); \& _\|_END_\|_ \& _\|_C_\|_ \& void greet(char* name) { \& printf("Hello %s!\en", name); \& } .Ve .SS "Example #2 \- and Salutations" .IX Subsection "Example #2 - and Salutations" This is similar to the last example except that the name is passed in as a \&\f(CW\*(C`SV*\*(C'\fR (pointer to Scalar Value) rather than a string (\f(CW\*(C`char*\*(C'\fR). That means we need to convert the \f(CW\*(C`SV\*(C'\fR to a string ourselves. This is accomplished using the \f(CW\*(C`SvPVX\*(C'\fR function which is part of the \f(CW\*(C`Perl\*(C'\fR internal API. See \f(CW\*(C`perldocperlapi\*(C'\fR for more info. .PP One problem is that \f(CW\*(C`SvPVX\*(C'\fR doesn't automatically convert strings to numbers, so we get a little surprise when we try to greet \f(CW42\fR. The program segfaults, a common occurrence when delving into the guts of Perl. .PP .Vb 8 \& use Inline \*(AqC\*(Aq; \& greet(\*(AqIngy\*(Aq); \& greet(42); \& _\|_END_\|_ \& _\|_C_\|_ \& void greet(SV* sv_name) { \& printf("Hello %s!\en", SvPVX(sv_name)); \& } .Ve .SS "Example #3 \- Fixing the problem" .IX Subsection "Example #3 - Fixing the problem" We can fix the problem in Example #2 by using the \f(CW\*(C`SvPV\*(C'\fR function instead. This function will stringify the \f(CW\*(C`SV\*(C'\fR if it does not contain a string. \&\f(CW\*(C`SvPV\*(C'\fR returns the length of the string as it's second parameter. Since we don't care about the length, we can just put \f(CW\*(C`PL_na\*(C'\fR there, which is a special variable designed for that purpose. .PP .Vb 8 \& use Inline \*(AqC\*(Aq; \& greet(\*(AqIngy\*(Aq); \& greet(42); \& _\|_END_\|_ \& _\|_C_\|_ \& void greet(SV* sv_name) { \& printf("Hello %s!\en", SvPV(sv_name, PL_na)); \& } .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" For general information about Inline see Inline. .PP For sample programs using Inline with C see Inline::C::Cookbook. .PP For information on supported languages and platforms see Inline-Support. .PP For information on writing your own Inline Language Support Module, see Inline-API. .PP Inline's mailing list is inline@perl.org .PP To subscribe, send email to inline\-subscribe@perl.org .SH "BUGS AND DEFICIENCIES" .IX Header "BUGS AND DEFICIENCIES" If you use C function names that happen to be used internally by Perl, you will get a load error at run time. There is currently no functionality to prevent this or to warn you. For now, a list of Perl's internal symbols is packaged in the Inline module distribution under the filename \&\f(CW\*(Aqsymbols.perl\*(Aq\fR. Avoid using these in your code. .SH AUTHORS .IX Header "AUTHORS" Ingy döt Net .PP Sisyphus .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright 2000\-2022. Ingy döt Net. .PP Copyright 2008, 2010\-2014. Sisyphus. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .PP See