1FFI::Platypus::ConstantU(s3e)r Contributed Perl DocumentaFtFiIo:n:Platypus::Constant(3)
2
3
4
6 FFI::Platypus::Constant - Define constants in C space for Perl
7
9 version 1.10
10
12 "ffi/foo.c":
13
14 #include <ffi_platypus_bundle.h>
15
16 void
17 ffi_pl_bundle_constant(const char *package, ffi_platypus_constant_t *c)
18 {
19 c->set_str("FOO", "BAR"); /* sets $package::FOO to "BAR" */
20 c->set_str("ABC::DEF", "GHI"); /* sets ABC::DEF to GHI */
21 }
22
23 "lib/Foo.pm":
24
25 package Foo;
26
27 use strict;
28 use warnings;
29 use FFI::Platypus;
30 use base qw( Exporter );
31
32 my $ffi = FFI::Platypus->new;
33 # sets constatns Foo::FOO and ABC::DEF from C
34 $ffi->bundle;
35
36 1;
37
39 The Platypus bundle interface (see FFI::Platypus::Bundle) has an entry
40 point "ffi_pl_bundle_constant" that lets you define constants in Perl
41 space from C.
42
43 void ffi_pl_bundle_constant(const char *package, ffi_platypus_constant_t *c);
44
45 The first argument "package" is the name of the Perl package. The
46 second argument "c" is a struct with function pointers that lets you
47 define constants of different types. The first argument for each
48 function is the name of the constant and the second is the value. If
49 "::" is included in the constant name then it will be defined in that
50 package space. If it isn't then the constant will be defined in
51 whichever package called "bundle".
52
53 set_str
54 c->set_str(name, value);
55
56 Sets a string constant.
57
58 set_sint
59 c->set_sint(name, value);
60
61 Sets a 64-bit signed integer constant.
62
63 set_uint
64 c->set_uint(name, value);
65
66 Sets a 64-bit unsigned integer constant.
67
68 set_double
69 c->set_double(name, value);
70
71 Sets a double precision floating point constant.
72
73 Example
74 Suppose you have a header file "myheader.h":
75
76 #ifndef MYHEADER_H
77 #define MYHEADER_H
78
79 #define MYVERSION_STRING "1.2.3"
80 #define MYVERSION_MAJOR 1
81 #define MYVERSION_MINOR 2
82 #define MYVERSION_PATCH 3
83
84 enum {
85 MYBAD = -1,
86 MYOK = 1
87 };
88
89 #define MYPI 3.14
90
91 #endif
92
93 You can define these constants from C:
94
95 #include <ffi_platypus_bundle.h>
96 #include "myheader.h"
97
98 void ffi_pl_bundle_constant(const char *package, ffi_platypus_constant_t *c)
99 {
100 c->set_str("MYVERSION_STRING", MYVERSION_STRING);
101 c->set_uint("MYVERSION_MAJOR", MYVERSION_MAJOR);
102 c->set_uint("MYVERSION_MINOR", MYVERSION_MINOR);
103 c->set_uint("MYVERSION_PATCH", MYVERSION_PATCH);
104 c->set_sint("MYBAD", MYBAD);
105 c->set_sint("MYOK", MYOK);
106 c->set_double("MYPI", MYPI);
107 }
108
109 Your Perl code doesn't have to do anything when calling bundle:
110
111 package Const;
112
113 use strict;
114 use warnings;
115 use FFI::Platypus;
116
117 {
118 my $ffi = FFI::Platypus->new( api => 1 );
119 $ffi->bundle;
120 }
121
122 1;
123
125 Author: Graham Ollis <plicease@cpan.org>
126
127 Contributors:
128
129 Bakkiaraj Murugesan (bakkiaraj)
130
131 Dylan Cali (calid)
132
133 pipcet
134
135 Zaki Mughal (zmughal)
136
137 Fitz Elliott (felliott)
138
139 Vickenty Fesunov (vyf)
140
141 Gregor Herrmann (gregoa)
142
143 Shlomi Fish (shlomif)
144
145 Damyan Ivanov
146
147 Ilya Pavlov (Ilya33)
148
149 Petr Pisar (ppisar)
150
151 Mohammad S Anwar (MANWAR)
152
153 Håkon Hægland (hakonhagland, HAKONH)
154
155 Meredith (merrilymeredith, MHOWARD)
156
157 Diab Jerius (DJERIUS)
158
160 This software is copyright (c) 2015,2016,2017,2018,2019 by Graham
161 Ollis.
162
163 This is free software; you can redistribute it and/or modify it under
164 the same terms as the Perl 5 programming language system itself.
165
166
167
168perl v5.30.1 2020-02-06 FFI::Platypus::Constant(3)