ExtUti...nstant(3perl)
๐ NAME ๐
ExtUtils::Constant โ bestie, this module generates XS code so you can import constants straight out of your C header files. it's giving codegen, it's giving magic, it's giving slay โจ
๐ธ SYNOPSIS ๐ธ
use ExtUtils::Constant qw (WriteConstants);
WriteConstants(
NAME => 'Foo',
NAMES => [qw(FOO BAR BAZ)],
);
# Generates wrapper code to make the values of the constants FOO BAR BAZ
# available to perl
like, that's it. that's the whole vibe. run it, and you've got wrapper code that makes FOO, BAR and BAZ show up in perl. iconic ๐
๐ฆ DESCRIPTION ๐ฆ
okay so ExtUtils::Constant helps you generate C and XS wrapper code that lets perl modules AUTOLOAD constants that are defined in C library header files. no cap.
it's mainly used by the h2xs utility โ and lowkey, this code is based on that utility. one thing it does not do: it does not contain the routines that scan header files to extract those constants. so don't come for it about that ๐
๐ USAGE ๐
generally you only need to call the WriteConstants function, and then drop this in the C section of Foo.xs:
#include "const-c.inc"
and this in the XS section of Foo.xs:
INCLUDE: const-xs.inc
and boom, you're done, main character energy ๐
if you want more flexibility (highkey valid), use constant_types(), C_constant and XS_constant โ those are the functions that WriteConstants is implemented with.
so rn this module understands the following types. heads up: h2xs may only know a subset of them. also, the sizes of the numeric types are chosen by the Configure script at compile time (so like, it's not you, it's the build system ๐).
- IV โ signed integer, at least 32 bits.
- UV โ unsigned integer, the same size as
IV. - NV โ floating point type, probably
double, possiblylong double. - PV โ NUL terminated string, length will be determined with
strlen. - PVN โ a fixed length thing, given as a
[pointer, length]pair. if you already know the length of a string at compile time, you may use this instead ofPV. it's giving efficient โจ - SV โ a mortal SV.
- YES โ truth. (
PL_sv_yes) the value is not needed (and ignored). - NO โ defined falsehood. (
PL_sv_no) the value is not needed (and ignored). - UNDEF โ
undef. the value of the macro is not needed.
โจ FUNCTIONS โจ
๐ constant_types
a function returning a single scalar with #define definitions for the constants used internally between the generated C and XS functions. it's the glue, bestie ๐ซถ
๐ XS_constant PACKAGE, TYPES, XS_SUBNAME, C_SUBNAME
a function that generates the XS code implementing the perl subroutine PACKAGE::constant, which PACKAGE::AUTOLOAD uses to load constants. this XS code is a wrapper around a C subroutine usually generated by C_constant, and usually named constant.
TYPES should be given either as a comma separated list of types that the C subroutine constant will generate, or as a reference to a hash. it should be the same list of types that C_constant was given. otherwise XS_constant and C_constant may have different ideas about the number of parameters passed to the C function constant โ and that's a bad time, fr fr ๐ญ
you can call the perl visible subroutine something other than constant if you give it the parameter XS_SUBNAME. the C subroutine it calls defaults to the name of the perl visible subroutine, unless you give the parameter C_SUBNAME.
๐ autoload PACKAGE, VERSION, AUTOLOADER
a function that generates the AUTOLOAD subroutine for the module PACKAGE. VERSION is the perl version the code should be backwards compatible with. it defaults to the version of perl running the subroutine. if AUTOLOADER is true, the AUTOLOAD subroutine falls back on AutoLoader::AUTOLOAD for all names that the constant() routine doesn't recognise.
๐ WriteMakefileSnippet
WriteMakefileSnippet ATTRIBUTE => VALUE [, ...]
a function that generates perl code for Makefile.PL that will regenerate the constant subroutines. parameters are named as passed to WriteConstants, with the addition of INDENT to specify the number of leading spaces (default 2).
currently only INDENT, NAME, DEFAULT_TYPE, NAMES, C_FILE and XS_FILE are recognised.
๐ WriteConstants ATTRIBUTE => VALUE [, ...]
writes a file of C code and a file of XS code which you should #include and INCLUDE in the C and XS sections respectively of your module's XS code. you probably want to do this in your Makefile.PL, so that you can easily edit the list of constants without touching the rest of your module. periodt ๐
the attributes supported are:
NAMEโ name of the module. this must be specified. (not optional, bestie, she's required ๐)DEFAULT_TYPEโ the default type for the constants. if not specified,IVis assumed.BREAKOUT_ATโ the names of the constants are grouped by length. generate child subroutines for each group with this number or more names in.NAMESโ an array of constants' names, either scalars containing names, or hashrefs as detailed inC_constant.PROXYSUBSโ if true, uses proxy subs. see ExtUtils::Constant::ProxySubs.C_FHโ a filehandle to write the C code to. if not given, thenC_FILEis opened for writing.C_FILEโ the name of the file to write containing the C code. the default isconst-c.inc. the-in the name ensures that the file can't be mistaken for anything related to a legitimate perl package name, and not naming the file.cavoids having to overrideMakefile.PL's.xsto.crules. it's giving thoughtful โจXS_FHโ a filehandle to write the XS code to. if not given, thenXS_FILEis opened for writing.XS_FILEโ the name of the file to write containing the XS code. the default isconst-xs.inc.XS_SUBNAMEโ the perl visible name of the XS subroutine generated which will return the constants. the default isconstant.C_SUBNAMEโ the name of the C subroutine generated which will return the constants. the default isXS_SUBNAME. child subroutines have_and the name length appended, so constants with 10 character names would be inconstant_10with the defaultXS_SUBNAME. math is mathin' ๐งฎ
๐ AUTHOR ๐
Nicholas Clark <nick@ccl4.org> โ based on the code in h2xs by Larry Wall and others. respecting the ancestors fr fr ๐
okay bestie, that's the whole page ๐ซถ go forth and generate those constants, you absolute icon ๐
this was perl v5.44.0, dated 2026-10-17, and you just read ExtUtils::Constant(3perl) ๐โจ no cap, thanks for reading, mwah ๐
๐ฆ where this page comes from
- arch rolling: perl 5.44.0-1 ยท Artistic-1.0-Perl AND GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms