1CallExt(3) User Contributed Perl Documentation CallExt(3)
2
3
4
6 PDL::CallExt - call functions in external shared libraries
7
9 use PDL::CallExt;
10 callext('file.so', 'foofunc', $x, $y); # pass piddles to foofunc()
11
12 % perl -MPDL::CallExt -e callext_cc file.c
13
15 callext() loads in a shareable object (i.e. compiled code) using Perl's
16 dynamic loader, calls the named function and passes a list of piddle
17 arguments to it.
18
19 It provides a reasonably portable way of doing this, including compil‐
20 ing the code with the right flags, though it requires simple perl and C
21 wrapper routines to be written. You may prefer to use PP, which is much
22 more portable. See PDL::PP. You should definitely use the latter for a
23 'proper' PDL module, or if you run in to the limitations of this mod‐
24 ule.
25
27 callext_cc() allows one to compile the shared objects using Perl's
28 knowledge of compiler flags.
29
30 The named function (e.g. 'foofunc') must take a list of piddle struc‐
31 tures as arguments, there is now way of doing portable general argument
32 construction hence this limitation.
33
34 In detail the code in the original file.c would look like this:
35
36 #include "pdlsimple.h" /* Declare simple piddle structs - note this .h file
37 contains NO perl/PDL dependencies so can be used
38 standalone */
39
40 int foofunc(int nargs, pdlsimple **args); /* foofunc prototype */
41
42 i.e. foofunc() takes an array of pointers to pdlsimple structs. The use
43 is similar to that of "main(int nargs, char **argv)" in UNIX C applica‐
44 tions.
45
46 pdlsimple.h defines a simple N-dimensional data structure which looks
47 like this:
48
49 struct pdlsimple {
50 int datatype; /* whether byte/int/float etc. */
51 void *data; /* Generic pointer to the data block */
52 int nvals; /* Number of data values */
53 PDL_Long *dims; /* Array of data dimensions */
54 int ndims; /* Number of data dimensions */
55 };
56
57 (PDL_Long is always a 4 byte int and is defined in pdlsimple.h)
58
59 This is a simplification of the internal reprensation of piddles in PDL
60 which is more complicated because of threading, dataflow, etc. It will
61 usually be found somewhere like /usr/local/lib/perl5/site_perl/PDL/pdl‐
62 simple.h
63
64 Thus to actually use this to call real functions one would need to
65 right a wrapper. e.g. to call a 2D image processing routine:
66
67 void myimage_processer(double* image, int nx, int ny);
68
69 int foofunc(int nargs, pdlsimple **args) {
70 pdlsimple* image = pdlsimple[0];
71 myimage_processer( image->data, *(image->dims), *(image->dims+1) );
72 ...
73 }
74
75 Obviously a real wrapper would include more error and argument check‐
76 ing.
77
78 This might be compiled (e.g. Linux):
79
80 cc -shared -o mycode.so mycode.c
81
82 In general Perl knows how to do this, so you should be able to get away
83 with:
84
85 perl -MPDL::CallExt -e callext_cc file.c
86
87 callext_cc() is a function defined in PDL::CallExt to generate the cor‐
88 rect compilation flags for shared objects.
89
90 If their are problems you will need to refer to you C compiler manual
91 to find out how to generate shared libraries.
92
93 See t/callext.t in the distribution for a working example.
94
95 It is up to the caller to ensure datatypes of piddles are correct - if
96 not peculiar results or SEGVs will result.
97
99 callext
100
101 Call a function in an external library using Perl dynamic loading
102
103 callext('file.so', 'foofunc', $x, $y); # pass piddles to foofunc()
104
105 The file must be compiled with dynamic loading options (see
106 "callext_cc"). See the module docs "PDL::Callext" for a description of
107 the API.
108
109 callext_cc
110
111 Compile external C code for dynamic loading
112
113 Usage:
114
115 % perl -MPDL::CallExt -e callext_cc file.c -o file.so
116
117 This works portably because when Perl has built in knowledge of how to
118 do dynamic loading on the system on which it was installed. See the
119 module docs "PDL::Callext" for a description of the API.
120
122 Copyright (C) Karl Glazebrook 1997. All rights reserved. There is no
123 warranty. You are allowed to redistribute this software / documentation
124 under certain conditions. For details, see the file COPYING in the PDL
125 distribution. If this file is separated from the PDL distribution, the
126 copyright notice should be included in the file.
127
128
129
130perl v5.8.8 2005-02-03 CallExt(3)