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, possibly long 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 of PV. 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, IV is 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 in C_constant.
  • PROXYSUBS โ€” if true, uses proxy subs. see ExtUtils::Constant::ProxySubs.
  • C_FH โ€” a filehandle to write the C code to. if not given, then C_FILE is opened for writing.
  • C_FILE โ€” the name of the file to write containing the C code. the default is const-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 .c avoids having to override Makefile.PL's .xs to .c rules. it's giving thoughtful โœจ
  • XS_FH โ€” a filehandle to write the XS code to. if not given, then XS_FILE is opened for writing.
  • XS_FILE โ€” the name of the file to write containing the XS code. the default is const-xs.inc.
  • XS_SUBNAME โ€” the perl visible name of the XS subroutine generated which will return the constants. the default is constant.
  • C_SUBNAME โ€” the name of the C subroutine generated which will return the constants. the default is XS_SUBNAME. child subroutines have _ and the name length appended, so constants with 10 character names would be in constant_10 with the default XS_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