1ExtUtils::Constant(3pm)Perl Programmers Reference GuideExtUtils::Constant(3pm)
2
3
4
6 ExtUtils::Constant - generate XS code to import C header constants
7
9 use ExtUtils::Constant qw (WriteConstants);
10 WriteConstants(
11 NAME => 'Foo',
12 NAMES => [qw(FOO BAR BAZ)],
13 );
14 # Generates wrapper code to make the values of the constants FOO BAR BAZ
15 # available to perl
16
18 ExtUtils::Constant facilitates generating C and XS wrapper code to
19 allow perl modules to AUTOLOAD constants defined in C library header
20 files. It is principally used by the "h2xs" utility, on which this
21 code is based. It doesn't contain the routines to scan header files to
22 extract these constants.
23
25 Generally one only needs to call the "WriteConstants" function, and
26 then
27
28 #include "const-c.inc"
29
30 in the C section of "Foo.xs"
31
32 INCLUDE: const-xs.inc
33
34 in the XS section of "Foo.xs".
35
36 For greater flexibility use "constant_types()", "C_constant" and
37 "XS_constant", with which "WriteConstants" is implemented.
38
39 Currently this module understands the following types. h2xs may only
40 know a subset. The sizes of the numeric types are chosen by the "Con‐
41 figure" script at compile time.
42
43 IV signed integer, at least 32 bits.
44
45 UV unsigned integer, the same size as IV
46
47 NV floating point type, probably "double", possibly "long double"
48
49 PV NUL terminated string, length will be determined with "strlen"
50
51 PVN A fixed length thing, given as a [pointer, length] pair. If you
52 know the length of a string at compile time you may use this
53 instead of PV
54
55 SV A mortal SV.
56
57 YES Truth. ("PL_sv_yes") The value is not needed (and ignored).
58
59 NO Defined Falsehood. ("PL_sv_no") The value is not needed (and
60 ignored).
61
62 UNDEF
63 "undef". The value of the macro is not needed.
64
66 constant_types
67 A function returning a single scalar with "#define" definitions for
68 the constants used internally between the generated C and XS func‐
69 tions.
70
71 XS_constant PACKAGE, TYPES, SUBNAME, C_SUBNAME
72 A function to generate the XS code to implement the perl subroutine
73 PACKAGE::constant used by PACKAGE::AUTOLOAD to load constants.
74 This XS code is a wrapper around a C subroutine usually generated
75 by "C_constant", and usually named "constant".
76
77 TYPES should be given either as a comma separated list of types
78 that the C subroutine "constant" will generate or as a reference to
79 a hash. It should be the same list of types as "C_constant" was
80 given. [Otherwise "XS_constant" and "C_constant" may have differ‐
81 ent ideas about the number of parameters passed to the C function
82 "constant"]
83
84 You can call the perl visible subroutine something other than "con‐
85 stant" if you give the parameter SUBNAME. The C subroutine it calls
86 defaults to the name of the perl visible subroutine, unless you
87 give the parameter C_SUBNAME.
88
89 autoload PACKAGE, VERSION, AUTOLOADER
90 A function to generate the AUTOLOAD subroutine for the module PACK‐
91 AGE VERSION is the perl version the code should be backwards com‐
92 patible with. It defaults to the version of perl running the sub‐
93 routine. If AUTOLOADER is true, the AUTOLOAD subroutine falls back
94 on AutoLoader::AUTOLOAD for all names that the constant() routine
95 doesn't recognise.
96
97 WriteMakefileSnippet
98 WriteMakefileSnippet ATTRIBUTE => VALUE [, ...]
99
100 A function to generate perl code for Makefile.PL that will regener‐
101 ate the constant subroutines. Parameters are named as passed to
102 "WriteConstants", with the addition of "INDENT" to specify the num‐
103 ber of leading spaces (default 2).
104
105 Currently only "INDENT", "NAME", "DEFAULT_TYPE", "NAMES", "C_FILE"
106 and "XS_FILE" are recognised.
107
108 WriteConstants ATTRIBUTE => VALUE [, ...]
109 Writes a file of C code and a file of XS code which you should
110 "#include" and "INCLUDE" in the C and XS sections respectively of
111 your module's XS code. You probably want to do this in your "Make‐
112 file.PL", so that you can easily edit the list of constants without
113 touching the rest of your module. The attributes supported are
114
115 NAME
116 Name of the module. This must be specified
117
118 DEFAULT_TYPE
119 The default type for the constants. If not specified "IV" is
120 assumed.
121
122 BREAKOUT_AT
123 The names of the constants are grouped by length. Generate
124 child subroutines for each group with this number or more names
125 in.
126
127 NAMES
128 An array of constants' names, either scalars containing names,
129 or hashrefs as detailed in "C_constant".
130
131 C_FILE
132 The name of the file to write containing the C code. The
133 default is "const-c.inc". The "-" in the name ensures that the
134 file can't be mistaken for anything related to a legitimate
135 perl package name, and not naming the file ".c" avoids having
136 to override Makefile.PL's ".xs" to ".c" rules.
137
138 XS_FILE
139 The name of the file to write containing the XS code. The
140 default is "const-xs.inc".
141
142 SUBNAME
143 The perl visible name of the XS subroutine generated which will
144 return the constants. The default is "constant".
145
146 C_SUBNAME
147 The name of the C subroutine generated which will return the
148 constants. The default is SUBNAME. Child subroutines have "_"
149 and the name length appended, so constants with 10 character
150 names would be in "constant_10" with the default XS_SUBNAME.
151
153 Nicholas Clark <nick@ccl4.org> based on the code in "h2xs" by Larry
154 Wall and others
155
156
157
158perl v5.8.8 2001-09-21 ExtUtils::Constant(3pm)