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 ndarrays 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 ndarray
17 arguments to it.
18
19 It provides a reasonably portable way of doing this, including
20 compiling the code with the right flags, though it requires simple perl
21 and C wrapper routines to be written. You may prefer to use PP, which
22 is much more portable. See PDL::PP. You should definitely use the
23 latter for a 'proper' PDL module, or if you run in to the limitations
24 of this module.
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 ndarray
31 structures as arguments, there is now way of doing portable general
32 argument 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 ndarray 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
44 applications.
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 representation of ndarrays in
60 PDL which is more complicated because of threading, dataflow, etc. It
61 will usually be found somewhere like
62 /usr/local/lib/perl5/site_perl/PDL/pdlsimple.h
63
64 Thus to actually use this to call real functions one would need to
65 write 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
76 checking.
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
88 correct 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 ndarrays are correct - if
96 not peculiar results or SEGVs will result.
97
99 callext
100 Call a function in an external library using Perl dynamic loading
101
102 callext('file.so', 'foofunc', $x, $y); # pass ndarrays to foofunc()
103
104 The file must be compiled with dynamic loading options (see
105 "callext_cc"). See the module docs "PDL::Callext" for a description of
106 the API.
107
108 callext_cc
109 Compile external C code for dynamic loading
110
111 Usage:
112
113 % perl -MPDL::CallExt -e callext_cc file.c -o file.so
114
115 This works portably because when Perl has built in knowledge of how to
116 do dynamic loading on the system on which it was installed. See the
117 module docs "PDL::Callext" for a description of the API.
118
120 Copyright (C) Karl Glazebrook 1997. All rights reserved. There is no
121 warranty. You are allowed to redistribute this software / documentation
122 under certain conditions. For details, see the file COPYING in the PDL
123 distribution. If this file is separated from the PDL distribution, the
124 copyright notice should be included in the file.
125
126
127
128perl v5.34.0 2021-08-16 CallExt(3)