ExtUti...::Base(3perl)
π ExtUtils::Constant::Base β the perl manpage, but make it make sense π
okay bestie, listen. this is the page for ExtUtils::Constant::Base(3perl) out of the Perl Programmers Reference Guide, and it's genuinely a base-class banger. let's get into it π«Ά
β¨ NAME β¨
ExtUtils::Constant::Base β it's the base class for ExtUtils::Constant objects. that's it, that's the whole job description π
β¨ SYNOPSIS β¨
require ExtUtils::Constant::Base;
@ISA = 'ExtUtils::Constant::Base';
yep. two lines. you require it and you slot it into @ISA like it's the group chat and you're adding one more icon π
β¨ DESCRIPTION β¨
so ExtUtils::Constant::Base hands you a base implementation of methods that generate C code, and the whole point of that C code is to give you fast constant value lookup by named string. no cap. π¦
right now it's mostly used by ExtUtils::Constant::XS, which is the one that generates the lookup code for the constant() subroutine β you know, the one that lives inside like a billion XS modules. it's everywhere and you never even noticed her. main character energy fr.
β¨ USAGE β¨
heads up bestie: ExtUtils::Constant::Base exports no subroutines. zero. none. instead, here are the methods you get to play with πΈ
headerβ returns a scalar containing the definitions you need, typically for a C header file.memEQ_clause args_hashrefβ returns a suitable Cifstatement that checks whether name is equal to the C variablename. ifchecked_atis defined, it gets used to avoidmemEQfor short names, or to generate a comment highlighting the position of the character in theswitchstatement. and if checked_at is a reference to a scalar, then instead it gives the characters pre-checked at the beginning (plus the number of chars by which the C variable name has been advanced β those need to be chopped off the front of name).dump_names arg_hashref, ITEM...β an internal function that generates the embedded perl code which will regenerate the constant subroutines.default_type,typesandITEMs mean exactly the same as they do forC_constant.indentis treated as the number of spaces to indent by. ifdeclare_typesis true, a$typesis always declared in the generated perl code; if it's defined and false it's never declared; and if it's undefined,$typesis only declared if the values intypesas passed in can't be inferred fromdefault_typesand theITEMs. slay.assign arg_hashref, VALUE...β returns a suitable assignment clause. iftypeis aggregate (likePVN, which expects both a pointer and a length) then you should pass multipleVALUEs for the components.preandpost, if defined, give snippets of C code to come before and after the assignment.presits at the start of a block, so yes, you can define variables in it πreturn_clause arg_hashref, ITEMβ returns a suitable#ifdefclause.ITEMis a hashref (as passed toC_constantandmatch_clause).indentis the number of spaces to indent, and it defaults to6.switch_clause arg_hashref, NAMELEN, ITEMHASH, ITEM...β an internal method that generates a suitableswitchclause, called byC_constant.ITEMs are in the hash ref format given in the description ofC_constant, and they must all have names of the same length, given byNAMELEN.ITEMHASHis a reference to a hash keyed by name, with the values being the hashrefs in theITEMlist. (nothing gets modified, and it's totally fine to have keys in theITEMHASHthat aren't in the list ofITEMs β the hash is passed in just to save regenerating it fresh on every call. efficient queen π)params WHATβ an "internal" method, subject to change, currently called so an overriding class can cache info that then gets passed into all the*param*calls. (yes, having to read the source to make sense of this is considered a known bug π)WHATis a hashref of the types the constant function will return. inExtUtils::Constant::XSthis method returns a hashref keyedIVNVPVSV, showing which combination of pointers is needed in the C argument list generated byC_constant_other_params_definitionandC_constant_other_params.dogfood arg_hashref, ITEM...β an internal function that generates the embedded perl code to regenerate the constant subroutines. parameters are the same as forC_constant. currently the base class does nothing and returns an empty string. iconic behaviour honestly. πnormalise_items args, default_type, seen_types, seen_items, ITEM...β converts the items into a normalised form. for 8 bit and Unicode values it converts the item into an array of 1 or 2 items, both 8 bit and UTF-8 encoded.C_constant arg_hashref, ITEM...β the big one π. returns a list of C subroutine definitions that return the value and type of constants when the name is passed in by the XS wrapper.ITEM...is a list of constant names, and each one can either be a string (taken as a C macro name) or a reference to a hash with the following keys:nameβ the name of the constant, as seen by the perl code.typeβ the type of the constant (IV,NV, etc).valueβ a C expression for the value of the constant, or a list of C expressions if the type is aggregate. defaults to the name if not given.macroβ the C pre-processor macro to use in the#ifdef. defaults to the name, and is mainly used whenvalueis anenum. if a reference to an array is passed, the first element is used in place of the#ifdefline and the second element in place of the#endif. this lets you do pre-processor constructions like
#if defined (foo)
#if !defined (bar)
...
#endif
#endif
to work out whether a constant is to be defined. a macro of 1 signals that the constant is always defined, so the #if/#endif test gets omitted entirely π
defaultβ the default value to use (instead ofcroaking withyour vendor has not defined...) to return if the macro isn't defined. specify a reference to an array with the type followed by the value(s).preβ C code to use before the assignment of the value of the constant. this lets you use temporary variables to extract a value from part of astructand return that as the value. this C code is placed at the start of a block, so you can declare variables in it.postβ C code to place between the assignment of value (to a temporary) and the return from the function. this lets you clean up anything frompre. rarely needed.def_predef_postβ the equivalents ofpreandpost, but for the default value.utf8β generated internally. it's zero or undefined if name is 7 bit ASCII,noif the name is 8 bit (and so should only match ifSvUTF8()is false), andyesif the name is utf8 encoded. the internals automatically clone any name with characters 128-255 but none 256+ (i.e. one that could be either bytes or utf8) into a second entry which is utf8 encoded. so considerate of her π§weightβ optional sorting weight for names, used to determine the order of linear testing when multiple names fall in the same case of a switch clause. higher comes earlier, and undefined defaults to zero.
okay and inside the argument hashref: package is the name of the package, and it's only used in comments inside the generated C code. subname defaults to constant if undefined.
default_type is the type returned by ITEMs that don't specify their own type, and it defaults to the value of default_type(). types should be given either as a comma separated list of types that the C subroutine subname will generate, or as a reference to a hash. default_type will be added to the list if it's not already there, and so will any types given in the list of ITEMs. the resultant list should be the same list of types that XS_constant is given. [otherwise XS_constant and C_constant may differ in the number of parameters to the constant function. indent is currently unused and ignored β in future it may be used to pass in information used to change the C indentation style used.] the best way to keep things consistent is to pass in a hash reference and just let this function update it. lowkey genius.
breakout governs when child functions of subname get generated. if there are breakout or more ITEMs with the same length of name, then the code to switch between them gets placed into a function named subname_len β for example constant_5 for names 5 characters long. the default breakout is 3. and a single ITEM is always inlined.
β¨ BUGS β¨
not everything is documented yet π
probably others.
no cap, "probably others" is doing a lot of heavy lifting in that sentence π
β¨ AUTHOR β¨
Nicholas Clark <nick@ccl4.org>, based on the code in h2xs by Larry Wall and others. legends only π«Ά
and that's the whole page, bestie!! this shipped as part of perl v5.44.0, dated 2026-10-17, and it's literally the ExtUti...::Base(3perl) page from your friendly neighbourhood Perl Programmers Reference Guide. go forth and generate some C code β¨π
π¦ 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