PERLAPI(1)
💖 PERLAPI 💖
the perl public API, autogenerated docs, but make it a group chat 🎀
✨ NAME ✨
perlapi — autogenerated documentation for the perl public API. periodt.
🌸 DESCRIPTION 🌸
okay so bestie, this file has most of the documentation of the perl public API, and it's all generated by embed.pl. basically it's a whole listing of functions, macros, flags, and variables that extension writers are allowed to use. besides perlintern and config.h, some items are listed here even though they're actually documented in another pod. iykyk. 💅
at the end there's a list of functions that have yet to be documented. patches welcome! but fr fr, the interfaces of these are subject to change without notice, so don't get too attached. 😭
some of the functions documented here are consolidated, so one single entry serves for multiple functions that all do basically the same thing but have some slight differences. like, one form might process magic, and another doesn't. the name of each variation is listed at the top of the single entry. slay, organized.
every single API function name starts with the prefix "Perl_" so it doesn't collide with anything in your code. but unless "-Accflags=-DPERL_NO_SHORT_NAMES" has been specified when compiling your code (see "Hiding Perl_" in perlembed), you also get synonymous macros that don't have this prefix, and those also hide from you the need (or not) to have a thread context parameter passed to the function. generally, code is easier to write and read when you use the short form, so in practice that compilation flag is just... not used. also bestie, not all functions have the short form; both are listed here when available. 🫶
anything that's not listed here or in the other mentioned pods is not part of the public API, and extension writers should not use it at all. for these reasons, blindly using functions listed in proto.h is to be avoided when writing extensions. do not do it. no cap.
in Perl, unlike in C, a string of characters may generally contain embedded "NUL" characters. sometimes in the documentation a Perl string gets referred to as a "buffer" to distinguish it from a C string, but sometimes they're both just called strings. it's giving ambiguous, but it's fine. 😭
note that all Perl API global variables must be referenced with the "PL_" prefix. again, those not listed here are not to be used by extension writers, and may be changed or removed without notice; same with macros. some macros are provided for compatibility with the older, unadorned names, but this support may be disabled in a future release. you've been warned bestie.
Perl was originally written to handle US-ASCII only (that is, characters whose ordinal numbers are in the range 0 - 127). and documentation and comments may still use the term ASCII, when sometimes in fact the entire range from 0 - 255 is meant. the more you know ✨
the non-ASCII characters below 256 can have various meanings depending on various things. (see, most notably, perllocale.) but usually the whole range can be referred to as ISO-8859-1. often, the term "Latin-1" (or "Latin1") is used as an equivalent for ISO-8859-1. but some people treat "Latin1" as referring just to the characters in the range 128 through 255, or sometimes from 160 through 255. this documentation uses "Latin1" and "Latin-1" to refer to all 256 characters. 💅
note that Perl can be compiled and run under either ASCII or EBCDIC (see perlebcdic). most of the documentation (and even comments in the code) ignore the EBCDIC possibility. for almost all purposes the differences are transparent. as an example, under EBCDIC, instead of UTF-8, UTF-EBCDIC is used to encode Unicode strings, and so whenever this documentation refers to "utf8" (and variants of that name, including in function names), it also (essentially transparently) means "UTF-EBCDIC". but the ordinals of characters differ between ASCII, EBCDIC, and the UTF- encodings, and a string encoded in UTF-EBCDIC may occupy a different number of bytes than in UTF-8. brain expanding moment fr.
the organization of this document is tentative and subject to change. suggestions and patches welcome at perl5-porters@perl.org <mailto:perl5-porters@perl.org>. 🎀
the API elements are grouped by functionality into sections, as follows. within sections the elements are ordered alphabetically, ignoring case, with non-leading underscores sorted first, and leading underscores and digits sorted last. alphabetical girlies rise up.
"AV Handling""Callback Functions""Casting""Character case changing""Character classification""Compiler and Preprocessor information""Compiler directives""Compile-time scope hooks""Concurrency""COPs and Hint Hashes""Custom Operators""CV Handling""Debugging""Display functions""Embedding, Threads, and Interpreter Cloning""Errno""Exception Handling (simple) Macros""Filesystem configuration values""Floating point""General Configuration""Global Variables""GV Handling and Stashes""Hook manipulation""HV Handling""Input/Output""Integer""I/O Formats""Lexer interface""Locales""Magic""Memory Management""MRO""Multicall Functions""Numeric Functions""Optrees""Pack and Unpack""Pad Data Structures""Password and Group access""Paths to system commands""Prototype information""Reference-counted stack manipulation""REGEXP Functions""Reports and Formats""Signals""Site configuration""Sockets configuration values""Source Filters""Stack Manipulation Macros""String Handling""SV Flags""SV Handling""Tainting""Time""Typedef names""Unicode Support""Utility Functions""Versioning""Warning and Dieing""XS""Undocumented elements"
the listing below is alphabetical, case insensitive. let's GOOO 🚀
👑 AV Handling 👑
🦋 "AV" 🦋
described in perlguts.
🦋 "av_clear" 🦋
frees all the elements of an array, leaving it empty. it's the XS equivalent of "@array = ()". see also "av_undef". 💖
note that it's possible that the actions of a destructor called directly or indirectly by freeing an element of the array could cause the reference count of the array itself to be reduced (e.g. by deleting an entry in the symbol table). so it is a possibility that the AV could have been freed (or even reallocated) on return from the call unless you hold a reference to it. lowkey scary but okay.
void av_clear( AV *av)
void Perl_av_clear(pTHX_ AV *av)
🦋 "av_count" 🦋
returns the number of elements in the array "av". this is the true length of the array, including any undefined elements. it is always the same as "av_top_index(av) + 1". ✨
Size_t av_count( AV *av)
Size_t Perl_av_count(pTHX_ AV *av)
🦋 "av_create_and_push" 🦋
push an SV onto the end of the array, creating the array if necessary. it's a small internal helper function to remove a commonly duplicated idiom. we love a helper that does the most.
void Perl_av_create_and_push(pTHX_ AV ** const avp,
SV * const val)
🦋 "av_create_and_unshift_one" 🦋
unshifts an SV onto the beginning of the array, creating the array if necessary. another small internal helper function to remove a commonly duplicated idiom. 🫶
SV ** Perl_av_create_and_unshift_one(pTHX_ AV ** const avp,
SV * const val)
🦋 "av_delete" 🦋
deletes the element indexed by "key" from the array, makes the element mortal, and returns it. if "flags" equals "G_DISCARD", the element is freed and NULL is returned. NULL is also returned if "key" is out of range. 💀
Perl equivalent: "splice(@myarray, $key, 1, undef)" (with the "splice" in void context if "G_DISCARD" is present).
SV * av_delete( AV *av, SSize_t key, I32 flags)
SV * Perl_av_delete(pTHX_ AV *av, SSize_t key, I32 flags)
🦋 "av_exists" 🦋
returns true if the element indexed by "key" has been initialized. 🎀
this relies on the fact that uninitialized array elements are set to "NULL".
Perl equivalent: exists($myarray[$key]).
bool av_exists( AV *av, SSize_t key)
bool Perl_av_exists(pTHX_ AV *av, SSize_t key)
🦋 "av_extend" 🦋
pre-extend an array so that it is capable of storing values at indexes "0..key". so "av_extend(av,99)" guarantees that the array can store 100 elements, i.e. that "av_store(av, 0, sv)" through "av_store(av, 99, sv)" on a plain array will work without any further memory allocation. no more surprise reallocations bestie.
if the av argument is a tied array, then will call the "EXTEND" tied array method with an argument of "(key+1)".
void av_extend( AV *av, SSize_t key)
void Perl_av_extend(pTHX_ AV *av, SSize_t key)
🦋 "av_fetch" 🦋
returns the SV at the specified index in the array. the "key" is the index. if "lval" is true, you are guaranteed to get a real SV back (in case it wasn't real before), which you can then modify. check that the return value is non-NULL before dereferencing it to a "SV*". ⚠️
see "Understanding the Magic of Tied Hashes and Arrays" in perlguts for more information on how to use this function on tied arrays.
the rough perl equivalent is $myarray[$key].
SV ** av_fetch( AV *av, SSize_t key, I32 lval)
SV ** Perl_av_fetch(pTHX_ AV *av, SSize_t key, I32 lval)
🦋 "av_fill" 🦋
set the highest index in the array to the given number, equivalent to Perl's "$#array = $fill;". 💅
the number of elements in the array will be "fill + 1" after av_fill() returns. if the array was previously shorter, then the additional elements appended are set to NULL. if the array was longer, then the excess elements are freed. "av_fill(av, -1)" is the same as av_clear(av).
void av_fill( AV *av, SSize_t fill)
void Perl_av_fill(pTHX_ AV *av, SSize_t fill)
🦋 "av_len*" 🦋
described under "av_top_index". go look it up bestie.
🦋 "av_make" 🦋
creates a new AV and populates it with a list (**strp, length "size") of SVs. a copy is made of each SV, so their refcounts are not changed. the new AV will have a reference count of 1. 🎀
Perl equivalent: "my @new_array = ($scalar1, $scalar2, $scalar3...);"
AV * av_make( SSize_t size, SV **strp)
AV * Perl_av_make(pTHX_ SSize_t size, SV **strp)
🦋 "av_pop" 🦋
removes one SV from the end of the array, reducing its size by one and returning the SV (transferring control of one reference count) to the caller. returns &PL_sv_undef if the array is empty. bye bye element ✨
Perl equivalent: "pop(@myarray);"
SV * av_pop( AV *av)
SV * Perl_av_pop(pTHX_ AV *av)
💅 av_push / av_push_simple 💅
okay bestie, both of these do the same core thing: they push an SV onto the end of the array, and control of one reference count is transferred along with it. The array will grow automatically to accommodate the addition, so no stressing about space.
Perl equivalent: push @myarray, $val;
av_push is the general purpose form — she works in all situations, she's the main character.
av_push_simple is a cut-down version of av_push that assumes the array is super straightforward: no magic, not readonly, and is AvREAL (peep "Real AVs - and those that are not" in perlguts), and that key is not less than -1.
😭 This function MUST NOT be used in situations where any of those assumptions may not hold. Do not test her. Do not push your luck.
void av_push ( AV *av, SV *val)
void Perl_av_push (pTHX_ AV *av, SV *val)
void av_push_simple( AV *av, SV *val)
void Perl_av_push_simple(pTHX_ AV *av, SV *val)
🦋 av_shift 🦋
She removes one SV from the start of the array, shrinking it by one, and hands that SV back to the caller (transferring control of one reference count with it). If the array is empty? You get &PL_sv_undef back. No drama.
Perl equivalent: shift(@myarray);
SV * av_shift( AV *av)
SV * Perl_av_shift(pTHX_ AV *av)
🎀 av_store 🎀
Stores an SV in an array, and you say where with key (that's the index, babe).
The return value is NULL if the operation failed, or if the value didn't actually need to be stored within the array (classic tied arrays behaviour). Otherwise, you can dereference it to get the SV* that was stored there (= val)).
⚠️ Heads up: the caller is responsible for suitably incrementing the reference count ofvalbefore the call, and decrementing it if the function returnedNULL. That's on you, not on her.
Approximate Perl equivalent: splice(@myarray, $key, 1, $val).
Also go read "Understanding the Magic of Tied Hashes and Arrays" in perlguts for the full tea on how to use this function on tied arrays.
SV ** av_store( AV *av, SSize_t key, SV *val)
SV ** Perl_av_store(pTHX_ AV *av, SSize_t key, SV *val)
✨ av_top_index / av_tindex / AvFILL / av_len ✨
These four are literally the same girl. They behave identically.
If the array av is empty, they return -1. Otherwise they return the maximum value of the indices</u> of all the array elements currently defined in av.
They process 'get' magic.
The Perl equivalent for these is $#av.
💀 And now for the plot twist: despite what the nameav_lenis giving, it returns the maximum index</u> in the array. That is NOT the same assv_len, which returns what you'd actually expect. If you want the real number of elements in an array, useav_count. iykyk.
SSize_t av_top_index( AV *av)
SSize_t av_tindex ( AV *av)
SSize_t AvFILL ( AV* av)
SSize_t av_len ( AV *av)
SSize_t Perl_av_len (pTHX_ AV *av)
🌸 av_undef 🌸
Undefines the array. It's the XS equivalent of undef(@array).
Not only does she free all the elements of the array (like av_clear() does), she also frees the memory the av was using to store its list of scalars. Full cleanout. Iconic.
See av_clear for a note about the array possibly being invalid on return. 👀
void av_undef( AV *av)
void Perl_av_undef(pTHX_ AV *av)
🧚 av_unshift 🧚
Unshifts the given number of undef values onto the beginning of the array. The array will grow automatically to accommodate the addition, obviously.
Perl equivalent: unshift @myarray, ((undef) x $num);
void av_unshift( AV *av, SSize_t num)
void Perl_av_unshift(pTHX_ AV *av, SSize_t num)
🍓 AvALLOC 🍓
Described in perlguts.
AvALLOC(AV* av)
⭐ AvARRAY ⭐
Returns a pointer to the AV's internal SV* array.
This is the move when you wanna do pointer arithmetic on the array. But if all you need is to look up an array element? Then prefer av_fetch. Don't be extra for no reason.
SV** AvARRAY(AV* av)
💜 AvFILL* 💜
Described under "av_top_index". Go look up there bestie, it's all covered.
👑 AvREFCNT_inc / AvREFCNT_inc_simple / AvREFCNT_inc_simple_NN 👑
All three of these increment the reference count of the given SV, which must be an AV. They're lowkey the best when you're assigning the result into a typed pointer, because they save you from having to cast the result to the appropriate type. Convenience queens.
AV * AvREFCNT_inc (AV *av)
AV * AvREFCNT_inc_simple (AV *av)
AV * AvREFCNT_inc_simple_NN(AV *av)
🌈 get_av 🌈
Returns the AV of the specified Perl global or package array with the given name. (So yes, it will NOT work on lexical variables. Sorry babe.)
flags are passed to gv_fetchpv. If GV_ADD is set and the Perl variable doesn't exist yet, then it will be created. If flags is zero (ignoring SVf_UTF8) and the variable doesn't exist, then NULL is returned.
Perl equivalent: @{"$name"}.
🙈 NOTE: the perl_get_av() form is deprecated. Don't.
AV * get_av( const char *name, I32 flags)
AV * Perl_get_av(pTHX_ const char *name, I32 flags)
🎀 newAV / newAV_mortal / newAV_alloc_x / newAV_alloc_xz 🎀
All of these create a new AV, setting the reference count to 1. And if you already know the initial elements of the array, see av_make.
Now for the lore, because you need it: an array consists of three things:
- A data structure containing information about the array as a whole, like its size and reference count.
- A C language array of pointers to the individual elements. These get treated as pointers to SVs, so all must be castable to
SV*. - The individual elements themselves. These could be, for instance, SVs and/or AVs and/or HVs, etc.
An empty array only needs the first one, and all these functions create that. Where they differ is what else they do. Let's get into it:
newAV form
This does nothing beyond creating the whole-array data structure. Perl equivalent is approximately my @array;
She's your girl when the minimum size of the array could be zero — like, maybe there are code paths that'll entirely skip using it.
But if the array does get used, the pointers data structure will need to be allocated at that time. That'll end up being done by av_extend, either explicitly:
av_extend(av, len);
or implicitly when the first element is stored:
(void)av_store(av, 0, sv);
Unused array elements are typically initialized by av_extend.
newAV_mortal form
This one also creates the whole-array data structure, but she also mortalises it. (Which is to say: a reference to the AV is added to the temps stack.)
newAV_alloc_x form
This effectively does a newAV followed by also allocating (uninitialized) space for the pointers array. You use this when you already know ahead of time the likely minimum size of the array. It's more efficient than doing a plain newAV followed by an av_extend. Slay.
Of course the array can be extended later should it become necessary.
size must be at least 1.
newAV_alloc_xz form
This is newAV_alloc_x, but she initializes each pointer in it to NULL. That gives you added safety, guarding against them being read before being set. Safety queen. 🫶
size must be at least 1.
The following examples all result in an array that can fit four elements (indexes 0 .. 3):
AV *av = newAV();
av_extend(av, 3);
AV *av = newAV_alloc_x(4);
AV *av = newAV_alloc_xz(4);
In contrast, the following examples allocate an array that is only guaranteed to fit one element without extending:
AV *av = newAV_alloc_x(1);
AV *av = newAV_alloc_xz(1);
AV * newAV ()
AV * Perl_newAV (pTHX)
AV * newAV_mortal ()
AV * newAV_alloc_x (SSize_t size)
AV * newAV_alloc_xz(SSize_t size)
💖 newAVav 💖
Creates a new AV and populates it with values copied from an existing AV. The new AV will have a reference count of 1, and will contain newly created SVs copied from the original SV. The original source stays unchanged. No theft, only vibes.
Perl equivalent: my @new_array = @existing_array;
AV * newAVav( AV *oav)
AV * Perl_newAVav(pTHX_ AV *oav)
🌸 newAVhv 🌸
Creates a new AV and populates it with keys and values copied from an existing HV. The new AV will have a reference count of 1, and will contain newly created SVs copied from the original HV. The original source remains unchanged.
Perl equivalent: my @new_array = %existing_hash;
AV * newAVhv( HV *ohv)
AV * Perl_newAVhv(pTHX_ HV *ohv)
💀 Nullav 💀
🚨 DEPRECATED! It is planned to remove Nullav from a future release of Perl. Do not use it for new code; remove it from existing code. Periodt.
Null AV pointer.
(deprecated - use (AV *)NULL instead)
🦋 Callback Functions 🦋
💅 call_argv 💅
Performs a callback to the specified named and package-scoped Perl subroutine, using argv (a NULL-terminated array of strings) as the arguments. See perlcall.
Approximate Perl equivalent: &{"$sub_name"}(@$argv).
🙈 NOTE: the perl_call_argv() form is deprecated.
SSize_t call_argv( const char *sub_name, I32 flags,
char **argv)
SSize_t Perl_call_argv(pTHX_ const char *sub_name, I32 flags,
char **argv)
🌈 call_method 🌈
Performs a callback to the specified Perl method. The blessed object must be on the stack. See perlcall.
🙈 NOTE: the perl_call_method() form is deprecated.
SSize_t call_method( const char *methname, I32 flags)
SSize_t Perl_call_method(pTHX_ const char *methname, I32 flags)
✨ call_pv ✨
Performs a callback to the specified Perl sub. See perlcall.
🙈 NOTE: the perl_call_pv() form is deprecated.
SSize_t call_pv( const char *sub_name, I32 flags)
SSize_t Perl_call_pv(pTHX_ const char *sub_name, I32 flags)
🎀 call_sv 🎀
Performs a callback to the Perl sub specified by the SV.
If neither the G_METHOD nor G_METHOD_NAMED flag is supplied, the SV may be any of a CV, a GV, a reference to a CV, a reference to a GV, or SvPV(sv) will be used as the name of the sub to call. So many options, so much flexibility, she ate.
If the G_METHOD flag is supplied, the SV may be a reference to a CV or SvPV(sv) will be used as the name of the method to call. 💖
If the G_METHOD_NAMED flag is supplied, SvPV(sv) will be used as the name of the method to call. ✨
Some other values are treated specially for internal use and should not be depended on. 🙈
See perlcall.
NOTE: the perl_call_sv() form is deprecated. 💀
SSize_t call_sv( SV *sv, I32 flags)
SSize_t Perl_call_sv(pTHX_ SV *sv, I32 flags)
DESTRUCTORFUNC_NOCONTEXT_t, DESTRUCTORFUNC_t
Described in perlguts. 🫶
ENTER
Opening bracket on a callback. See LEAVE and perlcall. 🌸
ENTER;
ENTER_with_name
Same as ENTER, but when debugging is enabled it also associates the given literal string with the new scope. 🎀
ENTER_with_name("name");
eval_pv
Tells Perl to eval the given string in scalar context and return an SV* result. 👑
NOTE: the perl_eval_pv() form is deprecated. 🚫
SV * eval_pv( const char *p, I32 croak_on_error)
SV * Perl_eval_pv(pTHX_ const char *p, I32 croak_on_error)
eval_sv
Tells Perl to eval the string in the SV. It supports the same flags as call_sv, with the obvious exception of G_EVAL. See perlcall. ✨
The G_RETHROW flag can be used if you only need eval_sv() to execute code specified by a string, but not catch any errors. 🔥
By default the code is compiled and executed with the default hints, such as strict and features. Set G_USEHINTS in flags to use the current hints from PL_curcop. 💅
NOTE: the perl_eval_sv() form is deprecated. 💀
SSize_t eval_sv( SV *sv, I32 flags)
SSize_t Perl_eval_sv(pTHX_ SV *sv, I32 flags)
FREETMPS
Closing bracket for temporaries on a callback. See SAVETMPS and perlcall. 🦋
FREETMPS;
G_DISCARD, G_EVAL, G_KEEPERR, G_LIST, G_NOARGS, G_SCALAR, G_VOID
Described in perlcall. 🌈
GIMME
DEPRECATED! It is planned to remove GIMME from a future release of Perl. Do not use it for new code; remove it from existing code. 😭
A backward-compatible version of GIMME_V which can only return G_SCALAR or G_LIST; in a void context, it returns G_SCALAR. Deprecated. Use GIMME_V instead. 👑
U32 GIMME
GIMME_V
The XSUB-writer's equivalent to Perl's wantarray. Returns G_VOID, G_SCALAR or G_LIST for void, scalar or list context, respectively. See perlcall for a usage example. 🌟
U32 GIMME_V
is_lvalue_sub
Returns non-zero if the sub calling this function is being called in an lvalue context. Returns 0 otherwise. 💖
I32 is_lvalue_sub()
I32 Perl_is_lvalue_sub(pTHX)
LEAVE
Closing bracket on a callback. See ENTER and perlcall. 🎀
LEAVE;
LEAVE_with_name
Same as LEAVE, but when debugging is enabled it first checks that the scope has the given name. name must be a literal string. ✨
LEAVE_with_name("name");
mortal_destructor_sv
This function arranges for either a Perl code reference, or a C function reference to be called at the end of the current statement. 🫶
The coderef argument determines the type of function that will be called. If it is SvROK() it is assumed to be a reference to a CV and will arrange for the coderef to be called. If it is not SvROK() then it is assumed to be a SvIV() which is SvIOK() whose value is a pointer to a C function of type DESTRUCTORFUNC_t created using PTR2INT(). Either way the args parameter will be provided to the callback as a parameter, although the rules for doing so differ between the Perl and C mode. Normally this function is only used directly for the Perl case and the wrapper mortal_destructor_x() is used for the C function case. 💅
When operating in Perl callback mode the args parameter may be NULL in which case the code reference is called with no arguments, otherwise if it is an AV (SvTYPE(args) == SVt_PVAV) then the contents of the AV will be used as the arguments to the code reference, and if it is any other type then the args SV will be provided as a single argument to the code reference. 🔥
When operating in a C callback mode the args parameter will be passed directly to the C function as a void * pointer. No additional processing of the argument will be performed, and it is the callers responsibility to free the args parameter if necessary. 🙈
Be aware that there is a significant difference in timing between the end of the current statement and the end of the current pseudo block. If you are looking for a mechanism to trigger a function at the end of the current pseudo block you should look at ""SAVEDESTRUCTOR_X"" in perlapi instead of this function. 💀
void mortal_destructor_sv( SV *coderef, SV *args)
void Perl_mortal_destructor_sv(pTHX_ SV *coderef, SV *args)
MORTALDESTRUCTOR_SV, MORTALSVFUNC_X
Described in perlguts. 🌸
MORTALDESTRUCTOR_SV(SV *coderef, SV *args)
MORTALSVFUNC_X (SVFUNC_t f, SV *sv)
PL_errgv
Described in perlcall. 🦋
save_aelem, save_aelem_flags
These each arrange for the value of the array element av[idx] to be restored at the end of the enclosing pseudo-block. 💖
In save_aelem, the SV at C**sptr> will be replaced by a new undef scalar. That scalar will inherit any magic from the original **sptr, and any 'set' magic will be processed. ✨
In save_aelem_flags, SAVEf_KEEPOLDELEM being set in flags causes the function to forgo all that: the scalar at **sptr is untouched. If SAVEf_KEEPOLDELEM is not set, the SV at C**sptr> will be replaced by a new undef scalar. That scalar will inherit any magic from the original **sptr. Any 'set' magic will be processed if and only if SAVEf_SETMAGIC is set in in flags. 💅
void save_aelem ( AV *av, SSize_t idx, SV **sptr)
void save_aelem_flags( AV *av, SSize_t idx, SV **sptr,
const U32 flags)
void Perl_save_aelem_flags(pTHX_ AV *av, SSize_t idx, SV **sptr,
const U32 flags)
save_aptr, save_ary, save_hash
Described in perlguts. 🎀
void save_aptr( AV **aptr)
void Perl_save_aptr(pTHX_ AV **aptr)
AV * save_ary ( GV *gv)
AV * Perl_save_ary (pTHX_ GV *gv)
HV * save_hash( GV *gv)
HV * Perl_save_hash(pTHX_ GV *gv)
save_helem, save_helem_flags
These each arrange for the value of the hash element (in Perlish terms) $hv{key}] to be restored at the end of the enclosing pseudo-block. 🔥
In save_helem, the SV at C**sptr> will be replaced by a new undef scalar. That scalar will inherit any magic from the original **sptr, and any 'set' magic will be processed. 💖
In save_helem_flags, SAVEf_KEEPOLDELEM being set in flags causes the function to forgo all that: the scalar at **sptr is untouched. If SAVEf_KEEPOLDELEM is not set, the SV at C**sptr> will be replaced by a new undef scalar. That scalar will inherit any magic from the original **sptr. Any 'set' magic will be processed if and only if SAVEf_SETMAGIC is set in in flags. ✨
void save_helem ( HV *hv, SV *key, SV **sptr)
void save_helem_flags( HV *hv, SV *key, SV **sptr,
const U32 flags)
void Perl_save_helem_flags(pTHX_ HV *hv, SV *key, SV **sptr,
const U32 flags)
save_hptr, save_item, save_scalar, save_svref, SAVEBOOL, SAVEDELETE, SAVEDESTRUCTOR, SAVEDESTRUCTOR_X, SAVEFREEOP, SAVEFREEPV, SAVEFREERCPV, SAVEFREESV, SAVEGENERICSV, SAVEINT, SAVEIV, SAVEI8, SAVEI16, SAVEI32, SAVEMORTALIZESV, SAVEPPTR, SAVERCPV, SAVESPTR, SAVESTACK_POS, SAVESTRLEN
Described in perlguts. 🌈
void save_hptr ( HV **hptr)
void Perl_save_hptr (pTHX_ HV **hptr)
void save_item ( SV *item)
void Perl_save_item (pTHX_ SV *item)
SV * save_scalar ( GV *gv)
SV * Perl_save_scalar (pTHX_ GV *gv)
SV * save_svref ( SV **sptr)
SV * Perl_save_svref (pTHX_ SV **sptr)
SAVEBOOL ( bool i)
SAVEDELETE ( HV * hv, char * key,
I32 length)
SAVEDESTRUCTOR ( DESTRUCTORFUNC_NOCONTEXT_t f,
void *p)
SAVEDESTRUCTOR_X( DESTRUCTORFUNC_t f, void *p)
SAVEFREEOP ( OP *op)
SAVEFREEPV ( char *pv)
SAVEFREERCPV ( char *pv)
SAVEFREESV ( SV* sv)
SAVEGENERICSV ( char **psv)
SAVEINT ( int i)
SAVEIV ( IV i)
SAVEI8 ( I8 i)
SAVEI16 ( I16 i)
SAVEI32 ( I32 i)
SAVEMORTALIZESV ( SV* sv)
SAVEPPTR ( char * p)
SAVERCPV ( char *pv)
SAVESPTR ( SV * s)
SAVESTACK_POS ()
SAVESTRLEN ( STRLEN i)
SAVETMPS
Opening bracket for temporaries on a callback. See FREETMPS and perlcall. 💜
SAVETMPS;
💅 Casting ✨
cBOOL
Cast-to-bool, bestie. Back in the day when Perl could still get compiled on pre-C99 compilers, a (bool) cast didn't always do the right thing, so they made this macro (and made it kinda extra-complicated to dodge bugs in old compilers). Fast forward many years, C99 is what we use, so it's not actually needed anymore — but it's kept around for backwards compatibility. Legacy icon behavior, honestly. 👑
bool cBOOL(bool expr)
I_V
Cast an NV to IV while avoiding undefined C behavior. no cap, that undefined stuff is not the vibe. 💀
IV I_V(NV what)
I_32
Cast an NV to I32 while avoiding undefined C behavior.
I32 I_32(NV what)
INT2PTR, PTRV, PTR2IV, PTR2nat, PTR2NV, PTR2ul, PTR2UV
These ones are described over in perlguts, so go peek there if you want the full lore. 🫶
type INT2PTR(type, int value)
IV PTR2IV (void * ptr)
IV PTR2nat(void *)
NV PTR2NV (void * ptr)
unsigned long PTR2ul (void *)
UV PTR2UV (void * ptr)
U_V
Cast an NV to UV while avoiding undefined C behavior.
UV U_V(NV what)
U_32
Cast an NV to U32 while avoiding undefined C behavior.
U32 U_32(NV what)
🌸 Character case changing 🌸
Okay so this is where it gets interesting, bestie. Perl uses "full" Unicode case mappings, which means converting a single character to another case can lowkey give you back a whole sequence of characters. Like, the uppercase of "ß" (LATIN SMALL LETTER SHARP S) is the two-character sequence "SS". Iconic of it, honestly, but also a whole complication.
The lowercase of every character in the range 0..255 is a single character, so that's why toLOWER_L1 exists. But toUPPER_L1 can't exist — it literally couldn't return a valid result for all legal inputs. Instead, toUPPER_uvchr has an API that does allow every possible legal result to be returned. 💅 Likewise, no other function that's crippled by not being able to give correct results for the full range of possible inputs has been implemented here. Periodt.
toFOLD, toFOLD_A, toFOLD_utf8, toFOLD_utf8_safe, toFOLD_uvchr
These all return the foldcase of a character. "foldcase" is an internal case for /i pattern matching. If the foldcase of character A and the foldcase of character B are the same, they match caselessly; otherwise they don't. That's the whole tea. ☕
The differences in the forms are what domain they operate on, and whether the input is specified as a code point (those forms with a "cp" parameter) or as a UTF-8 string (the others). In the latter case, the code point to use is the first one in the buffer of UTF-8 encoded code points, delineated by the arguments "p .. e - 1".
toFOLD and toFOLD_A are synonyms of each other. They return the foldcase of any ASCII-range code point. In this range, the foldcase is identical to the lowercase. All other inputs are returned unchanged. Since these are macros, the input type may be any integral one, and the output will occupy the same number of bits as the input. ✨
There is no toFOLD_L1 nor toFOLD_LATIN1, because the foldcase of some code points in the 0..255 range is above that range or consists of multiple characters. Instead use toFOLD_uvchr. 🙈
toFOLD_uvchr returns the foldcase of any Unicode code point. The return value is identical to that of toFOLD_A for input code points in the ASCII range. The foldcase of the vast majority of Unicode code points is the same as the code point itself. For these, and for code points above the legal Unicode maximum, this returns the input code point unchanged. It additionally stores the UTF-8 of the result into the buffer beginning at s, and its length in bytes into *lenp. The caller must have made s large enough to contain at least UTF8_MAXBYTES_CASE+1 bytes to avoid possible overflow. ⚠️
NOTE: the foldcase of a code point may be more than one code point. The return value of this function is only the first of these. The entire foldcase is returned in s. To determine if the result is more than a single code point, you can do something like this:
uc = toFOLD_uvchr(cp, s, &len);
if (len > UTF8SKIP(s)) { is multiple code points }
else { is a single code point }
toFOLD_utf8 and toFOLD_utf8_safe are synonyms of each other. The only difference between these and toFOLD_uvchr is that the source for these is encoded in UTF-8, instead of being a code point. It is passed as a buffer starting at p, with e pointing to one byte beyond its end. The p buffer may certainly contain more than one code point; but only the first one (up through e - 1) is examined. If the UTF-8 for the input character is malformed in some way, the program may croak, or the function may return the REPLACEMENT CHARACTER, at the discretion of the implementation, and subject to change in future releases. 🫠
UV toFOLD (UV cp)
UV toFOLD_A (UV cp)
UV toFOLD_utf8 (U8* p, U8* e, U8* s, STRLEN* lenp)
UV toFOLD_utf8_safe(U8* p, U8* e, U8* s, STRLEN* lenp)
UV toFOLD_uvchr (UV cp, U8* s, STRLEN* lenp)
toLOWER, toLOWER_A, toLOWER_LATIN1, toLOWER_LC, toLOWER_L1, toLOWER_utf8, toLOWER_utf8_safe, toLOWER_uvchr
These all return the lowercase of a character. The differences are what domain they operate on, and whether the input is specified as a code point (those forms with a "cp" parameter) or as a UTF-8 string (the others). In the latter case, the code point to use is the first one in the buffer of UTF-8 encoded code points, delineated by the arguments "p .. e - 1".
toLOWER and toLOWER_A are synonyms of each other. They return the lowercase of any uppercase ASCII-range code point. All other inputs are returned unchanged. Since these are macros, the input type may be any integral one, and the output will occupy the same number of bits as the input. 💖
toLOWER_L1 and toLOWER_LATIN1 are synonyms of each other. They behave identically as toLOWER for ASCII-range input. But additionally will return the lowercase of any uppercase code point in the entire 0..255 range, assuming a Latin-1 encoding (or the EBCDIC equivalent on such platforms). 🌈
toLOWER_LC returns the lowercase of the input code point according to the rules of the current POSIX locale. Input code points outside the range 0..255 are returned unchanged.
toLOWER_uvchr returns the lowercase of any Unicode code point. The return value is identical to that of toLOWER_L1 for input code points in the 0..255 range. The lowercase of the vast majority of Unicode code points is the same as the code point itself. For these, and for code points above the legal Unicode maximum, this returns the input code point unchanged. It additionally stores the UTF-8 of the result into the buffer beginning at s, and its length in bytes into *lenp. The caller must have made s large enough to contain at least UTF8_MAXBYTES_CASE+1 bytes to avoid possible overflow. ⚠️
NOTE: the lowercase of a code point may be more than one code point. The return value of this function is only the first of these. The entire lowercase is returned in s. To determine if the result is more than a single code point, you can do something like this:
uc = toLOWER_uvchr(cp, s, &len);
if (len > UTF8SKIP(s)) { is multiple code points }
else { is a single code point }
toLOWER_utf8 and toLOWER_utf8_safe are synonyms of each other. The only difference between these and toLOWER_uvchr is that the source for these is encoded in UTF-8, instead of being a code point. It is passed as a buffer starting at p, with e pointing to one byte beyond its end. The p buffer may certainly contain more than one code point; but only the first one (up through e - 1) is examined. If the UTF-8 for the input character is malformed in some way, the program may croak, or the function may return the REPLACEMENT CHARACTER, at the discretion of the implementation, and subject to change in future releases. 🫠
UV toLOWER (UV cp)
UV toLOWER_A (UV cp)
UV toLOWER_LATIN1 (UV cp)
UV toLOWER_LC (UV cp)
UV toLOWER_L1 (UV cp)
UV toLOWER_utf8 (U8* p, U8* e, U8* s, STRLEN* lenp)
UV toLOWER_utf8_safe(U8* p, U8* e, U8* s, STRLEN* lenp)
UV toLOWER_uvchr (UV cp, U8* s, STRLEN* lenp)
toTITLE, toTITLE_A, toTITLE_utf8, toTITLE_utf8_safe, toTITLE_uvchr
These all return the titlecase of a character. The differences are what domain they operate on, and whether the input is specified as a code point (those forms with a "cp" parameter) or as a UTF-8 string (the others). In the latter case, the code point to use is the first one in the buffer of UTF-8 encoded code points, delineated by the arguments "p .. e - 1".
toTITLE and toTITLE_A are synonyms of each other. They return the titlecase of any lowercase ASCII-range code point. In this range, the titlecase is identical to the uppercase. All other inputs are returned unchanged. Since these are macros, the input type may be any integral one, and the output will occupy the same number of bits as the input. 🎀
There is no toTITLE_L1 nor toTITLE_LATIN1, because the titlecase of some code points in the 0..255 range is above that range or consists of multiple characters. Instead use toTITLE_uvchr. 🙈
toTITLE_uvchr returns the titlecase of any Unicode code point. The return value is identical to that of toTITLE_A for input code points in the ASCII range. The titlecase of the vast majority of Unicode code points is the same as the code point itself. For these, and for code points above the legal Unicode maximum, this returns the input code point unchanged. It additionally stores the UTF-8 of the result into the buffer beginning at s, and its length in bytes into *lenp. The caller must have made s large enough to contain at least UTF8_MAXBYTES_CASE+1 bytes to avoid possible overflow. ⚠️
NOTE: the titlecase of a code point may be more than one code point. The return value of this function is only the first of these. The entire titlecase is returned in s. To determine if the result is more than a single code point, you can do something like this:
uc = toTITLE_uvchr(cp, s, &len);
if (len > UTF8SKIP(s)) { is multiple code points }
else { is a single code point }
toTITLE_utf8 and toTITLE_utf8_safe are literally the same function wearing two name tags, bestie. The <em>only</em> glow-up they have over toTITLE_uvchr is that your source is sitting there encoded in UTF-8 instead of being handed over as a plain code point. So you pass it as a buffer starting at p, and e points one byte past the end of it. And like — the p buffer can absolutely hold more than one code point, she's not picky 💅 — but only the <em>first</em> one (that's everything up through e - 1) gets examined. Now, if the UTF-8 for your input character is malformed in some way? Yeah so, the program may croak, <em>or</em> the function may return the REPLACEMENT CHARACTER — and that's at the discretion of the implementation, and subject to change in future releases. So don't build your whole personality on which one happens 😭
UV toTITLE (UV cp)
UV toTITLE_A (UV cp)
UV toTITLE_utf8 (U8* p, U8* e, U8* s, STRLEN* lenp)
UV toTITLE_utf8_safe(U8* p, U8* e, U8* s, STRLEN* lenp)
UV toTITLE_uvchr (UV cp, U8* s, STRLEN* lenp)
✨ toUPPER ✨
toUPPER toUPPER_A toUPPER_utf8 toUPPER_utf8_safe toUPPER_uvchr
Okay so all of these hand you back the uppercase of a character. What's different between them is what domain they operate on, and whether your input arrives as a code point (the ones with a cp parameter, iykyk) or as a UTF-8 string (everyone else). When it's the UTF-8 string situation, the code point they actually use is the first one in that buffer of UTF-8 encoded code points, and that buffer is delineated by the arguments p .. e - 1.
toUPPER and toUPPER_A are synonyms of each other — same girl, different wig. They return the uppercase of <em>any lowercase ASCII-range code point</em>. Every other input just gets handed right back to you unchanged. And because these are macros, your input type can be literally any integral one, and the output will occupy the same number of bits as the input did. cute, right? 🎀
Now here's a thing: there is no toUPPER_L1 and no toUPPER_LATIN1. Why? Because the uppercase of some code points in the 0..255 range is either <em>above</em> that range, or it's made of multiple characters. So no shortcut for you — use toUPPER_uvchr instead.
toUPPER_uvchr returns the uppercase of any Unicode code point, periodt. For input code points sitting in the ASCII range, the return value is identical to what toUPPER_A gives you. And for the vast majority of Unicode code points, the uppercase is just... the same code point as the input. For those, and also for code points above the legal Unicode maximum, it returns your input code point unchanged. On top of that, it stores the UTF-8 of the result into the buffer beginning at s, and its length in bytes into *lenp. Heads up though: the caller must have made s large enough to hold at least UTF8_MAXBYTES_CASE+1 bytes, or you're risking overflow 🫣
NOTE: the uppercase of a code point may be more than one code point. The return value of this function is only the <em>first</em> of these. The entire uppercase gets returned in s. So if you wanna know whether the result is more than a single code point, you can do something like this:
uc = toUPPER_uvchr(cp, s, &len);
if (len > UTF8SKIP(s)) { is multiple code points }
else { is a single code point }
toUPPER_utf8 and toUPPER_utf8_safe are synonyms of each other. The only difference between these and toUPPER_uvchr is that the source for these is encoded in UTF-8, instead of being a code point. It is passed as a buffer starting at p, with e pointing to one byte beyond its end. The p buffer may certainly contain more than one code point; but only the first one (up through e - 1) is examined. If the UTF-8 for the input character is malformed in some way, the program may croak, or the function may return the REPLACEMENT CHARACTER, at the discretion of the implementation, and subject to change in future releases.
UV toUPPER (UV cp)
UV toUPPER_A (UV cp)
UV toUPPER_utf8 (U8* p, U8* e, U8* s, STRLEN* lenp)
UV toUPPER_utf8_safe(U8* p, U8* e, U8* s, STRLEN* lenp)
UV toUPPER_uvchr (UV cp, U8* s, STRLEN* lenp)
🌈 Character classification 🌈
So this whole section? It's about functions — really macros, let's be honest — that sort characters into types. Like punctuation versus alphabetic, that kind of vibe check. Most of these are analogous to regular expression character classes (peep "POSIX Character Classes" in perlrecharclass if you wanna go deeper 🦋).
There are several <em>variants</em> for each class. (Not every macro has every variant, bestie — each item below lists the ones that are valid for it.) None of them are affected by use bytes, and only the ones with LC in the name are affected by the current locale. The way that matters is <em>everything</em> here.
The base function, e.g., isALPHA(), takes any signed or unsigned value, treating it as a code point, and returns a boolean as to whether or not the character represented by it is (or on non-ASCII platforms, corresponds to) an ASCII character in the named class, based on platform, Unicode, and Perl rules. If your input is a number that doesn't fit in an octet, FALSE is what you get.
- Variant
isFOO_A(e.g.,isALPHA_A()) is identical to the base function with no suffix_A. This variant exists purely to emphasize <em>by its name</em> that only ASCII-range characters can return TRUE. main character naming energy. - Variant
isFOO_L1imposes the Latin-1 (or EBCDIC equivalent) character set onto the platform. That is, the code points that are ASCII are unaffected, since ASCII is a subset of Latin-1. But the non-ASCII code points are treated as if they are Latin-1 characters. For example,isWORDCHAR_L1()will return true when called with the code point0xDF, which is a word character in both ASCII and EBCDIC (though it represents different characters in each). If the input is a number that doesn't fit in an octet, FALSE is returned. (Perl's documentation uses a colloquial definition of Latin-1, to include all code points below 256.) - Variant
isFOO_uvchris exactly like theisFOO_L1variant, for inputs below 256, but if the code point is larger than 255, Unicode rules are used to determine if it is in the character class. For example,isWORDCHAR_uvchr(0x100)returns TRUE, since0x100is LATIN CAPITAL LETTER A WITH MACRON in Unicode, and is a word character. - Variants
isFOO_utf8andisFOO_utf8_safeare likeisFOO_uvchr, but are used for UTF-8 encoded strings. The two forms are different names for the same thing. Each call to one of these classifies the first character of the string starting atp. The second parameter,e, points to anywhere in the string beyond the first character, up to one byte past the end of the entire string. Although both variants are identical, the suffix_safein one name emphasizes that it will not attempt to read beyonde - 1, provided that the constraints < eis true (this is asserted for in-DDEBUGGINGbuilds). If the UTF-8 for the input character is malformed in some way, the program may croak, or the function may return FALSE, at the discretion of the implementation, and subject to change in future releases. - Variant
isFOO_LCis like theisFOO_AandisFOO_L1variants, but the result is based on the current locale, which is whatLCin the name stands for. If Perl can determine that the current locale is a UTF-8 locale, it uses the published Unicode rules; otherwise, it uses the C library function that gives the named classification. For example,isDIGIT_LC()when not in a UTF-8 locale returns the result of callingisdigit(). FALSE is always returned if the input won't fit into an octet. On some platforms where the C library function is known to be defective, Perl changes its result to follow the POSIX standard's rules. - Variant
isFOO_LC_uvchracts exactly likeisFOO_LCfor inputs less than 256, but for larger ones it returns the Unicode classification of the code point. - Variants
isFOO_LC_utf8andisFOO_LC_utf8_safeare likeisFOO_LC_uvchr, but are used for UTF-8 encoded strings. The two forms are different names for the same thing. Each call to one of these classifies the first character of the string starting atp. The second parameter,e, points to anywhere in the string beyond the first character, up to one byte past the end of the entire string. Although both variants are identical, the suffix_safein one name emphasizes that it will not attempt to read beyonde - 1, provided that the constraints < eis true (this is asserted for in-DDEBUGGINGbuilds). If the UTF-8 for the input character is malformed in some way, the program may croak, or the function may return FALSE, at the discretion of the implementation, and subject to change in future releases.
🍓 isALNUM 🍓
isALNUM isALNUM_A isALNUM_LC isALNUM_LC_uvchr
Each one of these is a synonym for its respectively named "isWORDCHAR" variant. Slay.
They're provided for backward compatibility, even though a word character includes <em>more</em> than the standard C language meaning of alphanumeric. So if you actually want the C language definition, go use the corresponding "isALPHANUMERIC" variant instead.
bool isALNUM (UV ch)
bool isALNUM_A (UV ch)
bool isALNUM_LC (UV ch)
bool isALNUM_LC_uvchr(UV ch)
🧚 isALNUMC 🧚
isALNUMC isALNUMC_A isALNUMC_LC isALNUMC_LC_uvchr isALNUMC_L1
These are discouraged, backward compatibility macros for "isALPHANUMERIC". That is, each returns a boolean indicating whether the specified character is one of [A-Za-z0-9], analogous to m/[[:alnum:]]/.
And the C suffix in the names? It was meant to indicate that they correspond to the C language isalnum(3). That's it, that's the lore 💀
bool isALNUMC (UV ch)
bool isALNUMC_A (UV ch)
bool isALNUMC_LC (UV ch)
bool isALNUMC_LC_uvchr(UV ch)
bool isALNUMC_L1 (UV ch)
💜 isALPHA 💜
isALPHA isALPHA_A isALPHA_LC isALPHA_LC_utf8_safe isALPHA_LC_uvchr isALPHA_L1 isALPHA_utf8 isALPHA_utf8_safe isALPHA_uvchr
Returns a boolean indicating whether the specified input is one of [A-Za-z], analogous to m/[[:alpha:]]/. And for the whole breakdown of what each variant means, go look at the top of this section, bestie 🫶
bool isALPHA (UV ch)
bool isALPHA_A (UV ch)
bool isALPHA_LC (UV ch)
bool isALPHA_LC_utf8_safe(U8 * s, U8 *end)
bool isALPHA_LC_uvchr (UV ch)
bool isALPHA_L1 (UV ch)
bool isALPHA_utf8 (U8 * s, U8 * end)
bool isALPHA_utf8_safe (U8 * s, U8 * end)
bool isALPHA_uvchr (UV ch)
🌸 isALPHANUMERIC 🌸
isALPHANUMERIC isALPHANUMERIC_A isALPHANUMERIC_LC isALPHANUMERIC_LC_utf8_safe isALPHANUMERIC_LC_uvchr isALPHANUMERIC_L1 isALPHANUMERIC_utf8 isALPHANUMERIC_utf8_safe isALPHANUMERIC_uvchr
Returns a boolean indicating whether the specified character is one of [A-Za-z0-9], analogous to m/[[:alnum:]]/. And again — see the top of this section for an explanation of the variants ✨
bool isALPHANUMERIC (UV ch)
bool isALPHANUMERIC_A (UV ch)
bool isALPHANUMERIC_LC (UV ch)
bool isALPHANUMERIC_LC_utf8_safe(U8 * s, U8 *end)
bool isALPHANUMERIC_LC_uvchr (UV ch)
bool isALPHANUMERIC_L1 (UV ch)
bool isALPHANUMERIC_utf8 (U8 * s, U8 * end)
bool isALPHANUMERIC_utf8_safe (U8 * s, U8 * end)
bool isALPHANUMERIC_uvchr (UV ch)
✨ The is-isASCII girlies ✨
Okay so meet the whole squad: isASCII, isASCII_A, isASCII_LC, isASCII_LC_utf8_safe, isASCII_LC_uvchr, isASCII_L1, isASCII_utf8, isASCII_utf8_safe, isASCII_uvchr. Nine icons, one job.
This one hands you back a boolean telling you whether the character you passed in is one of the 128 characters in the ASCII character set. It's giving m/[[:ascii:]]/, fr fr. 💅
Careful bestie: on non-ASCII platforms, it returns TRUE iff this character corresponds to an ASCII character.
And the variants isASCII_A() and isASCII_L1()? They're literally identical to isASCII(). If you need the whole lore on what the variants even are, scroll back to the top of this section, it's all there. 🫶
But okay, plot twist — some platforms just don't have the C library routine isascii(). No cap. In those cases, the variants whose names contain "LC" are the same as the corresponding ones without. So if you were expecting a difference, there isn't one there. 💀
Also, and this part is lowkey iconic: because all ASCII characters are UTF-8 invariant — meaning they have the exact same representation (always a single byte) whether encoded in UTF-8 or not — isASCII will give the correct results when called with any byte in any string, encoded in UTF-8 or not. 🌈 And same energy for isASCII_utf8 and isASCII_utf8_safe: they'll work properly on any string, encoded or not in UTF-8. So flexible, we love to see it. ✨
bool isASCII (UV ch)
bool isASCII_A (UV ch)
bool isASCII_LC (UV ch)
bool isASCII_LC_utf8_safe(U8 * s, U8 *end)
bool isASCII_LC_uvchr (UV ch)
bool isASCII_L1 (UV ch)
bool isASCII_utf8 (U8 * s, U8 * end)
bool isASCII_utf8_safe (U8 * s, U8 * end)
bool isASCII_uvchr (UV ch)
🎀 isBLANK and its little family 🎀
The lineup: isBLANK, isBLANK_A, isBLANK_LC, isBLANK_LC_utf8_safe, isBLANK_LC_uvchr, isBLANK_L1, isBLANK_utf8, isBLANK_utf8_safe, isBLANK_uvchr. 🦋
Returns a boolean telling you whether the character you handed over is a character considered to be a blank — m/[[:blank:]]/ energy, exactly. The variants get explained at the top of this section, go look if you're new here. 💖
And the heads up: some platforms do not have the C library routine isblank(). 😭 In those cases, the variants whose names contain "LC" are the same as the corresponding ones without. Same deal as isASCII, no drama.
bool isBLANK (UV ch)
bool isBLANK_A (UV ch)
bool isBLANK_LC (UV ch)
bool isBLANK_LC_utf8_safe(U8 * s, U8 *end)
bool isBLANK_LC_uvchr (UV ch)
bool isBLANK_L1 (UV ch)
bool isBLANK_utf8 (U8 * s, U8 * end)
bool isBLANK_utf8_safe (U8 * s, U8 * end)
bool isBLANK_uvchr (UV ch)
💀 isCNTRL — the control freaks 💀
Say hi to isCNTRL, isCNTRL_A, isCNTRL_LC, isCNTRL_LC_utf8_safe, isCNTRL_LC_uvchr, isCNTRL_L1, isCNTRL_utf8, isCNTRL_utf8_safe, isCNTRL_uvchr. ✨
Returns a boolean saying whether the character you gave it is a control character, analogous to m/[[:cntrl:]]/. Variants? Top of the section, bestie, it's all explained there. 📚
EBCDIC girlies listen up: on EBCDIC platforms, you almost always want to use the isCNTRL_L1 variant. Not optional-ish. Almost always. 👑
bool isCNTRL (UV ch)
bool isCNTRL_A (UV ch)
bool isCNTRL_LC (UV ch)
bool isCNTRL_LC_utf8_safe(U8 * s, U8 *end)
bool isCNTRL_LC_uvchr (UV ch)
bool isCNTRL_L1 (UV ch)
bool isCNTRL_utf8 (U8 * s, U8 * end)
bool isCNTRL_utf8_safe (U8 * s, U8 * end)
bool isCNTRL_uvchr (UV ch)
🔢 isDIGIT — number check, but make it fashion 🔢
The cast: isDIGIT, isDIGIT_A, isDIGIT_LC, isDIGIT_LC_utf8_safe, isDIGIT_LC_uvchr, isDIGIT_L1, isDIGIT_utf8, isDIGIT_utf8_safe, isDIGIT_uvchr. 🍓
Returns a boolean indicating whether the specified character is a digit, analogous to m/[[:digit:]]/. And the tea: the variants isDIGIT_A and isDIGIT_L1 are identical to isDIGIT. Twins. Same face. Same vibes. For the full variant explanation, top of this section, as always. 💖
bool isDIGIT (UV ch)
bool isDIGIT_A (UV ch)
bool isDIGIT_LC (UV ch)
bool isDIGIT_LC_utf8_safe(U8 * s, U8 *end)
bool isDIGIT_LC_uvchr (UV ch)
bool isDIGIT_L1 (UV ch)
bool isDIGIT_utf8 (U8 * s, U8 * end)
bool isDIGIT_utf8_safe (U8 * s, U8 * end)
bool isDIGIT_uvchr (UV ch)
🖼️ isGRAPH — graphic content, but make it legal 🖼️
Presenting: isGRAPH, isGRAPH_A, isGRAPH_LC, isGRAPH_LC_utf8_safe, isGRAPH_LC_uvchr, isGRAPH_L1, isGRAPH_utf8, isGRAPH_utf8_safe, isGRAPH_uvchr. ✨
Returns a boolean telling you whether the specified character is a graphic character, analogous to m/[[:graph:]]/. Variants explanation? You already know where it lives — top of this section. 🫶
bool isGRAPH (UV ch)
bool isGRAPH_A (UV ch)
bool isGRAPH_LC (UV ch)
bool isGRAPH_LC_utf8_safe(U8 * s, U8 *end)
bool isGRAPH_LC_uvchr (UV ch)
bool isGRAPH_L1 (UV ch)
bool isGRAPH_utf8 (U8 * s, U8 * end)
bool isGRAPH_utf8_safe (U8 * s, U8 * end)
bool isGRAPH_uvchr (UV ch)
🪪 isIDCONT — the second-or-later character of an identifier 🪪
Lineup: isIDCONT, isIDCONT_A, isIDCONT_LC, isIDCONT_LC_utf8_safe, isIDCONT_LC_uvchr, isIDCONT_L1, isIDCONT_utf8, isIDCONT_utf8_safe, isIDCONT_uvchr. 💜
Returns a boolean indicating whether the specified character can be the second or succeeding character of an identifier. This is very close to, but not quite the same as the official Unicode property XID_Continue. The difference? This returns true only if the input character also matches isWORDCHAR. That's the whole plot twist. 😭 Variants: top of this section, go read it, it's right there. 🎀
bool isIDCONT (UV ch)
bool isIDCONT_A (UV ch)
bool isIDCONT_LC (UV ch)
bool isIDCONT_LC_utf8_safe(U8 * s, U8 *end)
bool isIDCONT_LC_uvchr (UV ch)
bool isIDCONT_L1 (UV ch)
bool isIDCONT_utf8 (U8 * s, U8 * end)
bool isIDCONT_utf8_safe (U8 * s, U8 * end)
bool isIDCONT_uvchr (UV ch)
👑 isIDFIRST — main character energy, first position only 👑
The crew: isIDFIRST, isIDFIRST_A, isIDFIRST_LC, isIDFIRST_LC_utf8_safe, isIDFIRST_LC_uvchr, isIDFIRST_L1, isIDFIRST_utf8, isIDFIRST_utf8_safe, isIDFIRST_uvchr. 🌸
Returns a boolean indicating whether the specified character can be the first character of an identifier. Again — very close to, but not quite the same as the official Unicode property XID_Start. And again, the difference is that this returns true only if the input character also matches isWORDCHAR. Same caveat, different position. ✨ Variants at the top of the section, you know the drill by now. 💅
bool isIDFIRST (UV ch)
bool isIDFIRST_A (UV ch)
bool isIDFIRST_LC (UV ch)
bool isIDFIRST_LC_utf8_safe(U8 * s, U8 *end)
bool isIDFIRST_LC_uvchr (UV ch)
bool isIDFIRST_L1 (UV ch)
bool isIDFIRST_utf8 (U8 * s, U8 * end)
bool isIDFIRST_utf8_safe (U8 * s, U8 * end)
bool isIDFIRST_uvchr (UV ch)
💗 isLOWER — lowercase detection, serve 💗
Meet isLOWER, isLOWER_A, isLOWER_LC, isLOWER_LC_utf8_safe, isLOWER_LC_uvchr, isLOWER_L1, isLOWER_utf8, isLOWER_utf8_safe, isLOWER_uvchr. 🦋
Returns a boolean indicating whether the specified character is a lowercase character, analogous to m/[[:lower:]]/. See the top of this section for an explanation of the variants (no period at the end of that sentence in the source, and honestly? valid. she said what she said. 😌)
bool isLOWER (UV ch)
bool isLOWER_A (UV ch)
bool isLOWER_LC (UV ch)
bool isLOWER_LC_utf8_safe(U8 * s, U8 *end)
bool isLOWER_LC_uvchr (UV ch)
bool isLOWER_L1 (UV ch)
bool isLOWER_utf8 (U8 * s, U8 * end)
bool isLOWER_utf8_safe (U8 * s, U8 * end)
bool isLOWER_uvchr (UV ch)
🐙 isOCTAL — the tiny three-member friend group 🐙
Okay this one is a small squad, we love a low-maintenance icon: isOCTAL, isOCTAL_A, isOCTAL_L1. That's it. That's the whole group chat. 🎀
Returns a boolean indicating whether the specified character is an octal digit, [0-7]. And the only two variants are isOCTAL_A and isOCTAL_L1 — each is identical to isOCTAL. No notes, no differences, no drama. ✨
bool isOCTAL (UV ch)
bool isOCTAL_A (UV ch)
bool isOCTAL_L1(UV ch)
🖨️ isPRINT — printable and proud 🖨️
Roll call: isPRINT, isPRINT_A, isPRINT_LC, isPRINT_LC_utf8_safe, isPRINT_LC_uvchr, isPRINT_L1, isPRINT_utf8, isPRINT_utf8_safe, isPRINT_uvchr. 🌈
Returns a boolean indicating whether the specified character is a printable character, analogous to m/[[:print:]]/. Variants explanation, top of the section, bestie. You're basically a pro at this now. 💅
bool isPRINT (UV ch)
bool isPRINT_A (UV ch)
bool isPRINT_LC (UV ch)
bool isPRINT_LC_utf8_safe(U8 * s, U8 *end)
bool isPRINT_LC_uvchr (UV ch)
bool isPRINT_L1 (UV ch)
bool isPRINT_utf8 (U8 * s, U8 * end)
bool isPRINT_utf8_safe (U8 * s, U8 * end)
bool isPRINT_uvchr (UV ch)
🌟 isPSXSPC — short for Posix Space, and she's got LORE 🌟
The full roster: isPSXSPC, isPSXSPC_A, isPSXSPC_LC, isPSXSPC_LC_utf8_safe, isPSXSPC_LC_uvchr, isPSXSPC_L1, isPSXSPC_utf8, isPSXSPC_utf8_safe, isPSXSPC_uvchr. 👑
Okay, history lesson because this is genuinely interesting: starting in 5.18, this is identical in all its forms to the corresponding isSPACE() macros. ✨
The locale forms of this macro are identical to their corresponding isSPACE() forms in all Perl releases. Periodt. 💅
But in releases prior to 5.18, the non-locale forms differ from their isSPACE() forms in exactly one way: the isSPACE() forms don't match a Vertical Tab, and the isPSXSPC() forms do. Otherwise they are identical. The more you know. 🦋
So this macro is analogous to what m/[[:space:]]/ matches in a regular expression. And yes — see the top of this section for an explanation of the variants. 📚
bool isPSXSPC (UV ch)
bool isPSXSPC_A (UV ch)
bool isPSXSPC_LC (UV ch)
bool isPSXSPC_LC_utf8_safe(U8 * s, U8 *end)
bool isPSXSPC_LC_uvchr (UV ch)
bool isPSXSPC_L1 (UV ch)
bool isPSXSPC_utf8 (U8 * s, U8 * end)
bool isPSXSPC_utf8_safe (U8 * s, U8 * end)
bool isPSXSPC_uvchr (UV ch)
isPUNCT,isPUNCT_A,isPUNCT_LC,isPUNCT_LC_utf8_safe,isPUNCT_LC_uvchr,isPUNCT_L1,isPUNCT_utf8,isPUNCT_utf8_safe,isPUNCT_uvchr
Returns a boolean indicating whether the specified character is a punctuation character, analogous to m/[[:punct:]]/. ngl bestie, what counts as "punctuation" is NOT the simple little thing you'd hope it is — see "POSIX Character Classes" in perlrecharclass for the whole deal. And for what all the variants mean, look up at the top of this section. 💅
bool isPUNCT (UV ch)
bool isPUNCT_A (UV ch)
bool isPUNCT_LC (UV ch)
bool isPUNCT_LC_utf8_safe(U8 * s, U8 *end)
bool isPUNCT_LC_uvchr (UV ch)
bool isPUNCT_L1 (UV ch)
bool isPUNCT_utf8 (U8 * s, U8 * end)
bool isPUNCT_utf8_safe (U8 * s, U8 * end)
bool isPUNCT_uvchr (UV ch)
isSPACE,isSPACE_A,isSPACE_LC,isSPACE_LC_utf8_safe,isSPACE_LC_uvchr,isSPACE_L1,isSPACE_utf8,isSPACE_utf8_safe,isSPACE_uvchr
Returns a boolean indicating whether the specified character is a whitespace character. This is analogous to what m/\s/ matches in a regular expression. Starting in Perl 5.18 this also matches what m/[[:space:]]/ does. Prior to 5.18, only the locale forms of this macro (the ones with LC in their names) matched precisely what m/[[:space:]]/ does. In those releases, the only difference, in the non-locale variants, was that isSPACE() did not match a vertical tab. (Peep isPSXSPC for a macro that matches a vertical tab in all releases.) See the top of this section for the variants rundown. 🫶
bool isSPACE (UV ch)
bool isSPACE_A (UV ch)
bool isSPACE_LC (UV ch)
bool isSPACE_LC_utf8_safe(U8 * s, U8 *end)
bool isSPACE_LC_uvchr (UV ch)
bool isSPACE_L1 (UV ch)
bool isSPACE_utf8 (U8 * s, U8 * end)
bool isSPACE_utf8_safe (U8 * s, U8 * end)
bool isSPACE_uvchr (UV ch)
isUPPER,isUPPER_A,isUPPER_LC,isUPPER_LC_utf8_safe,isUPPER_LC_uvchr,isUPPER_L1,isUPPER_utf8,isUPPER_utf8_safe,isUPPER_uvchr
Returns a boolean indicating whether the specified character is an uppercase character, analogous to m/[[:upper:]]/. See the top of this section for the variants explanation. Uppercase? iconic. no notes. 👑
bool isUPPER (UV ch)
bool isUPPER_A (UV ch)
bool isUPPER_LC (UV ch)
bool isUPPER_LC_utf8_safe(U8 * s, U8 *end)
bool isUPPER_LC_uvchr (UV ch)
bool isUPPER_L1 (UV ch)
bool isUPPER_utf8 (U8 * s, U8 * end)
bool isUPPER_utf8_safe (U8 * s, U8 * end)
bool isUPPER_uvchr (UV ch)
isWORDCHAR,isWORDCHAR_A,isWORDCHAR_LC,isWORDCHAR_LC_utf8_safe,isWORDCHAR_LC_uvchr,isWORDCHAR_L1,isWORDCHAR_utf8,isWORDCHAR_utf8_safe,isWORDCHAR_uvchr
Returns a boolean indicating whether the specified character is a character that is a word character, analogous to what m/\w/ and m/[[:word:]]/ match in a regular expression. So what's a word character? It's an alphabetic character, a decimal digit, a connecting punctuation character (like an underscore), or a "mark" character that attaches to one of those (like some sort of accent). marks really said "I'm riding shotgun with my letter bestie" 😌
See the top of this section for the variants explanation.
isWORDCHAR_A, isWORDCHAR_L1, isWORDCHAR_uvchr, isWORDCHAR_LC, isWORDCHAR_LC_uvchr, isWORDCHAR_LC_utf8, and isWORDCHAR_LC_utf8_safe are also as described there, but additionally include the platform's native underscore. ✨
bool isWORDCHAR (UV ch)
bool isWORDCHAR_A (UV ch)
bool isWORDCHAR_LC (UV ch)
bool isWORDCHAR_LC_utf8_safe(U8 * s, U8 *end)
bool isWORDCHAR_LC_uvchr (UV ch)
bool isWORDCHAR_L1 (UV ch)
bool isWORDCHAR_utf8 (U8 * s, U8 * end)
bool isWORDCHAR_utf8_safe (U8 * s, U8 * end)
bool isWORDCHAR_uvchr (UV ch)
isXDIGIT,isXDIGIT_A,isXDIGIT_LC,isXDIGIT_LC_utf8_safe,isXDIGIT_LC_uvchr,isXDIGIT_L1,isXDIGIT_utf8,isXDIGIT_utf8_safe,isXDIGIT_uvchr
Returns a boolean indicating whether the specified character is a hexadecimal digit. In the ASCII range these are [0-9A-Fa-f]. Variants isXDIGIT_A() and isXDIGIT_L1() are identical to isXDIGIT(). See the top of this section for the variants explanation. hex digits stay undefeated fr fr 🔥
bool isXDIGIT (UV ch)
bool isXDIGIT_A (UV ch)
bool isXDIGIT_LC (UV ch)
bool isXDIGIT_LC_utf8_safe(U8 * s, U8 *end)
bool isXDIGIT_LC_uvchr (UV ch)
bool isXDIGIT_L1 (UV ch)
bool isXDIGIT_utf8 (U8 * s, U8 * end)
bool isXDIGIT_utf8_safe (U8 * s, U8 * end)
bool isXDIGIT_uvchr (UV ch)
🛠️ Compiler and Preprocessor information 🛠️
okay this next chunk is the "what is my compiler even capable of" lore — these symbols get set so you know what you can actually use. 🧚
CPPLAST
This symbol is intended to be used along with CPPRUN in the same manner symbol CPPMINUS is used with CPPSTDIN. It contains either - or ``. 💫
CPPMINUS
This symbol contains the second part of the string which will invoke the C preprocessor on the standard input and produce to standard output. This symbol will have the value - if CPPSTDIN needs a minus to specify standard input, otherwise the value is ``. 🎀
CPPRUN
This symbol contains the string which will invoke a C preprocessor on the standard input and produce to standard output. It needs to end with CPPLAST, after all other preprocessor flags have been specified. The main difference with CPPSTDIN is that this program will never be a pointer to a shell wrapper, i.e. it will be empty if no preprocessor is available directly to the user. Note that it may well be different from the preprocessor used to compile the C program. sneaky little detail, don't sleep on it 👀
CPPSTDIN
This symbol contains the first part of the string which will invoke the C preprocessor on the standard input and produce to standard output. Typical value of cc -E or /lib/cpp, but it can also call a wrapper. See CPPRUN. 🌸
HAS_BUILTIN_ADD_OVERFLOW
This symbol, if defined, indicates that the compiler supports __builtin_add_overflow for adding integers with overflow checks. ✨
HAS_BUILTIN_CHOOSE_EXPR
Can we handle GCC builtin for compile-time ternary-like expressions 💅
HAS_BUILTIN_EXPECT
Can we handle GCC builtin for telling that certain values are more likely 💅
HAS_BUILTIN_MUL_OVERFLOW
This symbol, if defined, indicates that the compiler supports __builtin_mul_overflow for multiplying integers with overflow checks. ✨
HAS_BUILTIN_SUB_OVERFLOW
This symbol, if defined, indicates that the compiler supports __builtin_sub_overflow for subtracting integers with overflow checks. ✨
HAS_C99_VARIADIC_MACROS
If defined, the compiler supports C99 variadic macros. 🦋
HAS_STATIC_INLINE
This symbol, if defined, indicates that the C compiler supports C99-style static inline. That is, the function can't be called from another translation unit. it's giving "mine, stay in your lane" 🙈
HASATTRIBUTE_ALWAYS_INLINE
Can we handle GCC attribute for functions that should always be inlined. 🔥
HASATTRIBUTE_DEPRECATED
Can we handle GCC attribute for marking deprecated APIs 💀
HASATTRIBUTE_FORMAT
Can we handle GCC attribute for checking printf-style formats 🌈
HASATTRIBUTE_NONNULL
Can we handle GCC attribute for nonnull function parms. ⭐
HASATTRIBUTE_NORETURN
Can we handle GCC attribute for functions that do not return 👻
HASATTRIBUTE_PURE
Can we handle GCC attribute for pure functions 💖
HASATTRIBUTE_UNUSED
Can we handle GCC attribute for unused variables and arguments 🎀
HASATTRIBUTE_VISIBILITY
Can we handle GCC attribute for functions that should have a different visibility. 👑
HASATTRIBUTE_WARN_UNUSED_RESULT
Can we handle GCC attribute for warning on unused results 👀
MEM_ALIGNBYTES
This symbol contains the number of bytes required to align a double, or a long double when applicable. Usual values are 2, 4 and 8. The default is eight, for safety. For cross-compiling or multiarch support, Configure will set a minimum of 8. safety first bestie 🫶
PERL_STATIC_INLINE
This symbol gives the best-guess incantation to use for static inline functions. If HAS_STATIC_INLINE is defined, this will give C99-style inline. If HAS_STATIC_INLINE is not defined, this will give a plain 'static'. It will always be defined to something that gives static linkage. Possibilities include
static inline (c99)
static __inline__ (gcc -ansi)
static __inline (MSVC)
static _inline (older MSVC)
static (c89 compilers)
PERL_THREAD_LOCAL
This symbol, if defined, gives a linkage specification for thread-local storage. For example, for a C11 compiler this will be _Thread_local. Beware, some compilers are sensitive to the C language standard they are told to parse. For example, suncc defaults to C11, so our probe will report that _Thread_local can be used. However, if the -std=c99 is later added to the compiler flags, then _Thread_local will become a syntax error. Hence it is important for these flags to be consistent between probing and use. 😭
U32_ALIGNMENT_REQUIRED
This symbol, if defined, indicates that you must access character data through U32-aligned pointers. align it or regret it, no cap 💅
✨ Compiler directives ✨
Okay bestie, buckle UP — these are the macros that boss your C preprocessor around. They're little spells you sprinkle into Perl's C code to make compilers behave. 🪄
__ASSERT_— this is a helper macro that exists purely to dodge preprocessor drama 💅. Outside ofDEBUGGINGit gets replaced by absolutely nothing. But underDEBUGGING, it expands into an assert of its argument, followed by a comma (yes, the comma operator is doing the work here). Why the comma though? Because if they'd just used a plain oldassert(), you'd end up with a comma sitting there with nothing in front of it whenever you're not inDEBUGGINGmode. Iconic problem, iconic solution.
__ASSERT_(bool expr)
ASSUME— okay soASSUMEis givingassert()energy but it's got a glow-up for release builds. It's a hint to the compiler about a statement of fact inside a function-call-free expression, and that lets the compiler cook up better machine code. In a debug build,ASSUME(x)is literally just a synonym forassert(x). AndASSUME(0)? That means the control path is unreachable, bestie. Inside aforloop you can useASSUMEto hint that the loop's gonna run at least X times. Fun fact:ASSUMEis based off MSVC's__assumeintrinsic function, so go check its docs if you want the full tea. 🫖
ASSUME(bool expr)
dNOOP— declares nothing. It's a placeholder, usually there to stand in for something that used to declare something. Works on compilers that insist declarations come before any code, so it's lowkey a peacekeeper. 🕊️
dNOOP;
END_EXTERN_C— if you're not compiling with C++, this expands to nothing. Otherwise, it closes out a section of code that aSTART_EXTERN_Calready opened. It's the closing parenthesis of the extern-C saga. 🎀EXTERN_C— not compiling with C++? Then it's nothing. Compiling with C++? Then it's used in a function declaration to say this function should have external C linkage. And ngl, this is required for basically all functions with external linkage compiled into perl, so don't skip it. A lot of the time you can just wrap everything inSTART_EXTERN_C...END_EXTERN_Cblocks and call it a day. 💅
Example usage:
EXTERN_C int flock(int fd, int op);
LIKELY— hands back the input totally unchanged, but sneakily it gives the compiler a branch prediction hint that this condition is probably true. Manifesting atruefr fr. ✨
LIKELY(bool expr)
NOOP— does nothing! A placeholder for something that used to actually do something. The vibe is "we left the seat warm". 🪑
NOOP;
PERL_UNUSED_ARG— this one shuts up compiler warnings about a function parameter going unused. When does that happen? Say a parameter's needed under some configuration conditions but not others, so the C preprocessor's conditional compilation means it only gets used sometimes. Situational, bestie.
PERL_UNUSED_ARG(void x);
PERL_UNUSED_CONTEXT— same energy as above but for the thread context parameter. It suppresses warnings that the context param isn't used, which can happen when C preprocessor conditional compilation only uses it some of the time. 🧚
PERL_UNUSED_CONTEXT;
PERL_UNUSED_DECL— tells the compiler that the parameter in the function prototype right before it isn't necessarily expected to be used in the function. Heads up though: not that many compilers actually get this, so only reach for it whenPERL_UNUSED_ARGcan't conveniently do the job. 🙈
Example usage:
Signal_t
Perl_perly_sighandler(int sig, Siginfo_t *sip PERL_UNUSED_DECL,
void *uap PERL_UNUSED_DECL, bool safe)
PERL_UNUSED_RESULT— this macro says "yeah I'm discarding the return value of the function call inside me", like so:
PERL_UNUSED_RESULT(foo(a, b))
The main reason it exists: the combo of gcc -Wunused-result (part of -Wall) and __attribute__((warn_unused_result)) cannot be silenced just by casting to void. And that gets messy when system header files use the attribute. 😭
Use PERL_UNUSED_RESULT sparingly, okay? Usually that warning is there for a good reason — you might lose success/failure info, leak resources, or mess with resource changes.
That said, sometimes you genuinely just wanna ignore the return value. Like on codepaths that are about to hit abort anyway, or in "best effort" attempts, or in situations where there's just no good way to handle failures. Valid.
Also, sometimes PERL_UNUSED_RESULT isn't the most natural move — you could instead capture the return value and use PERL_UNUSED_VAR on it. 🌸
PERL_UNUSED_RESULT(void x)
PERL_UNUSED_VAR— suppresses compiler warnings that variable x isn't used. Happens when C preprocessor conditional compilation means it only gets used sometimes, yk how it is. 💜
PERL_UNUSED_VAR(void x);
START_EXTERN_C— not compiling with C++? Nothing. Otherwise it opens a section of code where every function effectively getsEXTERN_Capplied to it, meaning external C linkage. The section gets closed off by anEND_EXTERN_C. 🎀STATIC— described in perlguts. That's it, that's the whole entry. Go look it up bestie. 📖STMT_STARTandSTMT_END— these two let a series of statements inside a macro act like a single statement, like:
if (x) STMT_START { ... } STMT_END else ...
Warning though: you can't return a value out of this construct, and you can't use it as an operand to the comma operator. Those two limits really cramp its utility. 😬
But! You can get a value back by constructing the API so a pointer gets passed and the macro dereferences it to set the return. If the value could be any of various types depending on context, you can sometimes handle that by adding the return type as an extra accompanying parameter:
#define foo(param, type) STMT_START {
type * param; *param = do_calc; ...
} STMT_END
Ngl that could get awkward, so consider using a C language static inline function instead. 🙏
And if you do go with this construct, it's easy to forget it's a macro and not a function — which means you can fall into traps that don't show up until someone someday writes code with names that clash with yours, or calls it with a parameter that's an expression with side effects, and the consequences are something you never even thought about. Yikes. See "Writing safer macros" in perlhacktips for how to dodge all that. 🦋
UNLIKELY— returns the input unchanged, but gives the compiler a branch prediction hint that this condition is probably false. Reverse manifestation, still slay. 💅
UNLIKELY(bool expr)
🧚 Compile-time scope hooks 🧚
These ones fire when the Perl lexical scope changes at compile time. Very main character energy, very experimental.
BhkDISABLE— NOTE:BhkDISABLEis experimental and may change or be removed without notice.
Temporarily disables an entry in this BHK structure by clearing the appropriate flag. which is a preprocessor token indicating which entry to disable. 🚫
void BhkDISABLE(BHK *hk, token which)
BhkENABLE— NOTE:BhkENABLEis experimental and may change or be removed without notice.
Re-enables an entry in this BHK structure by setting the appropriate flag. which is a preprocessor token indicating which entry to enable. This will assert (under -DDEBUGGING) if the entry doesn't contain a valid pointer. ✅
void BhkENABLE(BHK *hk, token which)
BhkENTRY_set— NOTE:BhkENTRY_setis experimental and may change or be removed without notice.
Sets an entry in the BHK structure, and sets the flags to indicate it's valid. which is a preprocessing token indicating which entry to set. The type of ptr depends on the entry. 🎯
void BhkENTRY_set(BHK *hk, token which, void *ptr)
blockhook_register— NOTE:blockhook_registeris experimental and may change or be removed without notice.
Registers a set of hooks to be called when the Perl lexical scope changes at compile time. See "Compile-time scope hooks" in perlguts. 🪝✨
void Perl_blockhook_register(pTHX_ BHK *hk)
🌈 Concurrency 🌈
Threads, bestie. The whole vibe of "many things happening at once" energy. Let's get into it 💅
aTHX,aTHX_— Described in perlguts. That's it, that's the entry. Go read perlguts, fr fr 🫶CPERLscope— DEPRECATED! 😭 It is planned to removeCPERLscopefrom a future release of Perl. Do not use it for new code; remove it from existing code. It's giving "ghosted by the maintainers." Now a no-op.
void CPERLscope(void x)
dTHR,dTHX— Described in perlguts. Same deal as above, go look it up 👑dTHXa— On threaded perls, setpTHXtoa; on unthreaded perls, do nothing. Conditional queen ✨dTHXoa— Now a synonym fordTHXa. They're twins, no cap.dVAR— This is now a synonym for dNOOP: declare nothing. Literally manifesting absolutely nothing and that's the whole job 💀GETENV_PRESERVES_OTHER_THREAD— This symbol, if defined, indicates that the getenv system call doesn't zap the static buffer of getenv() in a different thread. The typical getenv() implementation will return a pointer to the proper position in**environ. But some may instead copy them to a static buffer in getenv(). If there is a per-thread instance of that buffer, or the return points to**environ, then a many-reader/1-writer mutex will work; otherwise an exclusive locking mutex is required to prevent races. Races are NOT it 🏃♀️💨HAS_PTHREAD_ATFORK— This symbol, if defined, indicates that thepthread_atforkroutine is available to setup fork handlers. Fork handlers, the iconic little setup crew 🍴HAS_PTHREAD_ATTR_SETSCOPE— This symbol, if defined, indicates that thepthread_attr_setscopesystem call is available to set the contention scope attribute of a thread attribute object. Setting the vibe of a thread attribute object, periodt ✨HAS_PTHREAD_YIELD— This symbol, if defined, indicates that thepthread_yieldroutine is available to yield the execution of the current thread.sched_yieldis preferable topthread_yield. Noted and taken, bestie.HAS_SCHED_YIELD— This symbol, if defined, indicates that thesched_yieldroutine is available to yield the execution of the current thread.sched_yieldis preferable topthread_yield. Yeah, the page said it twice, it really means it 🙈I_MACH_CTHREADS— This symbol, if defined, indicates to the C program that it should include mach/cthreads.h.
#ifdef I_MACH_CTHREADS
#include <mach_cthreads.h>
#endif
I_PTHREAD— This symbol, if defined, indicates to the C program that it should include pthread.h.
#ifdef I_PTHREAD
#include <pthread.h>
#endif
MULTIPLICITY— This symbol, if defined, indicates that Perl should be built to use multiplicity. Built different, literally 🧚OLD_PTHREAD_CREATE_JOINABLE— This symbol, if defined, indicates how to create pthread in joinable (aka undetached) state. NOTE: not defined if pthread.h already has definedPTHREAD_CREATE_JOINABLE(the new version of the constant). If defined, known values arePTHREAD_CREATE_UNDETACHEDand__UNDETACHED. Old-school constants, retro aesthetic 🎀OLD_PTHREADS_API— This symbol, if defined, indicates that Perl should be built to use the old draftPOSIXthreadsAPI. Throwback era, we respect the history 💜PERL_IMPLICIT_CONTEXT,PERL_NO_GET_CONTEXT,pTHX,pTHX_— Described in perlguts. Bestie the perlguts is doing a LOT of heavy lifting in this section, honestly 😭SCHED_YIELD— This symbol defines the way to yield the execution of the current thread. Known ways aresched_yield,pthread_yield, andpthread_yieldwithNULL. Three options, one yield, main character energy 👑
🎀 COPs and Hint Hashes 🎀
Okay so COPs are where the compiler keeps the receipts, and hint hashes are the little ✨metadata✨ attached to them. Let's go, this section is long but we're built for this 💅
cop_fetch_label— NOTE:cop_fetch_labelis experimental and may change or be removed without notice. ⚠️ So like, use it, but don't get attached.
Returns the label attached to a cop, and stores its length in bytes into *len. Upon return, *flags will be set to either SVf_UTF8 or 0.
Alternatively, use the macro CopLABEL_len_flags; or if you don't need to know if the label is UTF-8 or not, the macro CopLABEL_len; or if you additionally don't need to know the length, CopLABEL. Options on options, we love a menu 🍓
const char * cop_fetch_label( COP * const cop,
STRLEN *len, U32 *flags)
const char * Perl_cop_fetch_label(pTHX_ COP * const cop,
STRLEN *len, U32 *flags)
cop_hints_exists_pv,cop_hints_exists_pvn,cop_hints_exists_pvs,cop_hints_exists_sv— These look up the hint entry in the copcopwith the key specified bykey(andkeylenin thepvnform), returning true if a value exists, and false otherwise.
The forms differ in how the key is specified. In all forms, the key is pointed to by key. In the plain pv form, the key is a C language NUL-terminated string. In the pvs form, the key is a C language string literal. In the pvn form, an additional parameter, keylen, specifies the length of the string, which hence, may contain embedded-NUL characters. In the sv form, *key is an SV, and the key is the PV extracted from that. using SvPV_const.
hash is a precomputed hash of the key string, or zero if it has not been precomputed. This parameter is omitted from the pvs form, as it is computed automatically at compile time. The compiler is doing your homework for you, slay 🧚
The only flag currently used from the flags parameter is COPHH_KEY_UTF8. It is illegal to set this in the sv form. In the pv* forms, it specifies whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if cleared). The sv form uses the underlying SV to determine the UTF-8ness of the octets.
bool cop_hints_exists_pv (const COP *cop, const char *key,
U32 hash, U32 flags)
bool cop_hints_exists_pvn(const COP *cop, const char *key,
STRLEN keylen, U32 hash, U32 flags)
bool cop_hints_exists_pvs(const COP *cop, "key", U32 flags)
bool cop_hints_exists_sv (const COP *cop, SV *key, U32 hash,
U32 flags)
cop_hints_fetch_pv,cop_hints_fetch_pvn,cop_hints_fetch_pvs,cop_hints_fetch_sv— These look up the hint entry in the copcopwith the key specified bykey(andkeylenin thepvnform), returning that value as a mortal scalar copy, or&PL_sv_placeholderif there is no value associated with the key. Mortal scalar copy — it's got a whole lifespan, dramatic 💀
The forms differ in how the key is specified. In the plain pv form, the key is a C language NUL-terminated string. In the pvs form, the key is a C language string literal. In the pvn form, an additional parameter, keylen, specifies the length of the string, which hence, may contain embedded-NUL characters. In the sv form, *key is an SV, and the key is the PV extracted from that. using SvPV_const.
hash is a precomputed hash of the key string, or zero if it has not been precomputed. This parameter is omitted from the pvs form, as it is computed automatically at compile time.
The only flag currently used from the flags parameter is COPHH_KEY_UTF8. It is illegal to set this in the sv form. In the pv* forms, it specifies whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if cleared). The sv form uses the underlying SV to determine the UTF-8ness of the octets.
SV * cop_hints_fetch_pv (const COP *cop, const char *key,
U32 hash, U32 flags)
SV * cop_hints_fetch_pvn(const COP *cop, const char *key,
STRLEN keylen, U32 hash, U32 flags)
SV * cop_hints_fetch_pvs(const COP *cop, "key", U32 flags)
SV * cop_hints_fetch_sv (const COP *cop, SV *key, U32 hash,
U32 flags)
cop_hints_2hv— Generates and returns a standard Perl hash representing the full set of hint entries in the copcop.flagsis currently unused and must be zero. Must be zero bestie, no negotiating 🫶
HV * cop_hints_2hv(const COP *cop, U32 flags)
cop_store_label— NOTE:cop_store_labelis experimental and may change or be removed without notice. ⚠️
Save a label into a cop_hints_hash. You need to set flags to SVf_UTF8 for a UTF-8 label. Any other flag is ignored. Ignored, not judged, just ignored ✨
void cop_store_label( COP * const cop,
const char *label, STRLEN len,
U32 flags)
void Perl_cop_store_label(pTHX_ COP * const cop,
const char *label, STRLEN len,
U32 flags)
CopFILE— Returns the name of the file associated with theCOPc
const char * CopFILE(const COP * c)
CopFILE_copy— Efficiently copies the cop file name from one COP to another. Wraps the required logic to do a refcounted copy under threads or not. Efficiently! We love a shortcut that actually works 💅
void CopFILE_copy(COP * dst, COP * src)
CopFILE_free— Frees the file data in a cop. Under the hood this is a refcounting operation. Under the hood, where the real machinery lives 🔧
void CopFILE_free(COP * c)
CopFILE_LEN— Returns the length of the file associated with theCOPc
const char * CopFILE_LEN(const COP * c)
CopFILE_set,CopFILE_setn— These each makepvthe name of the file associated with theCOPc. In the plainCopFILE_setform,pvis a C language NUL-terminated string. InCopFILE_setn,lenis the length ofpv, which hence may contain embedded NUL characters. Embedded NULs, the chaotic fave 😭
void CopFILE_set (COP * c, const char * pv)
void CopFILE_setn(COP * c, const char * pv, STRLEN len)
CopFILEAV— Returns the AV associated with theCOPc, creating it if necessary. It'll manifest one for you if it doesn't exist, generous icon 🌸
AV * CopFILEAV(const COP * c)
CopFILEAVn— Returns the AV associated with theCOPc, returning NULL if it doesn't already exist. No creating here, if it's not there it's not there 💀
AV * CopFILEAVn(const COP * c)
CopFILEGV— Returns the GV associated with theCOPc
GV * CopFILEGV(const COP * c)
CopFILEGV_set— Available only on unthreaded perls. Makespvthe name of the file associated with theCOPc. Exclusive to unthreaded perls, very limited edition 🎀
void CopFILEGV_set(COP *c, GV *gv)
CopFILESV— Returns the SV associated with theCOPc
SV * CopFILESV(const COP * c)
cophh_copy— NOTE:cophh_copyis experimental and may change or be removed without notice. ⚠️
Make and return a complete copy of the cop hints hash cophh. A complete copy, no shortcuts, the whole thing duplicated and thriving 🦋
COPHH * cophh_copy(COPHH *cophh)
cophh_delete_pv,cophh_delete_pvn,cophh_delete_pvs,cophh_delete_sv— NOTE: all these forms are experimental and may change or be removed without notice. ⚠️ Yeah, all four of them. Experimental. Unstable. Living on the edge, bestie 🫶cophh_delete_pv,cophh_delete_pvn,cophh_delete_pvs,cophh_delete_sv— okay so these ones yeet a key and its lil value buddy right out of the cop hints hashcophh, then hand you back the glow-up'd hash. IMPORTANT: the pointer you get back is usually not the same pointer you passed in, bestie. The input hash gets consumed — literally eaten — so you must NOT touch that old pointer afterwards, not even once, not even a little. If you need both versions hanging around, callcophh_copyfirst. 🫶
The four flavors just differ in how you spell the key. In every form the key is pointed at by key. In the plain pv form, it's a C language NUL-terminated string. In the pvs form, it's a C language string literal (slay, compile-time vibes). In the pvn form you also pass keylen, the length of the string, which means it's allowed to have embedded NUL characters in it (iconic, unhinged, love that for it). In the sv form, *key is an SV, and the key is the PV extracted from it, using SvPV_const.
hash is a precomputed hash of the key string, or zero if you haven't precomputed it. The pvs form just... doesn't take this parameter at all, because it computes it automatically at compile time. 💅
The only flag currently used out of the flags parameter is COPHH_KEY_UTF8. Setting it in the sv form is illegal, no cap. In the pv* forms it says whether the key octets are read as UTF-8 (if set) or as Latin-1 (if cleared). The sv form doesn't need it — it peeks at the underlying SV to figure out the UTF-8ness of the octets itself. 👑
COPHH * cophh_delete_pv (COPHH *cophh, const char *key,
U32 hash, U32 flags)
COPHH * cophh_delete_pvn(COPHH *cophh, const char *key,
STRLEN keylen, U32 hash, U32 flags)
COPHH * cophh_delete_pvs(COPHH *cophh, "key", U32 flags)
COPHH * cophh_delete_sv (COPHH *cophh, SV *key, U32 hash,
U32 flags)
cophh_exists_pvn— NOTE:cophh_exists_pvnis experimental and may change or be removed without notice. ⚠️
These go looking for the hint entry in the cop cop with the key you named in key (plus keylen in the pvn form), and they return true if a value exists there and false if it doesn't. Simple as. 🌸
The forms differ in how the key is specified. In the plain pv form, the key is a C language NUL-terminated string. In the pvs form, the key is a C language string literal. In the pvn form, an additional parameter, keylen, specifies the length of the string, which hence may contain embedded-NUL characters. In the sv form, *key is an SV, and the key is the PV extracted from that, using SvPV_const.
hash is a precomputed hash of the key string, or zero if it has not been precomputed. This parameter is omitted from the pvs form, as it is computed automatically at compile time.
The only flag currently used from the flags parameter is COPHH_KEY_UTF8. It is illegal to set this in the sv form. In the pv* forms, it specifies whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if cleared). The sv form uses the underlying SV to determine the UTF-8ness of the octets.
bool cophh_exists_pvn(const COPHH *cophh, const char *key,
STRLEN keylen, U32 hash, U32 flags)
cophh_fetch_pv,cophh_fetch_pvn,cophh_fetch_pvs,cophh_fetch_sv— NOTE: all these forms are experimental and may change or be removed without notice. ⚠️
These go look up the entry in the cop hints hash cophh with the key specified by key (and keylen in the pvn form), and they hand back that value as a mortal scalar copy, or &PL_sv_placeholder if there's no value associated with that key. Bestie came back empty-handed and brought a placeholder instead, we've all been there. 😭
The forms differ in how the key is specified. In the plain pv form, the key is a C language NUL-terminated string. In the pvs form, the key is a C language string literal. In the pvn form, an additional parameter, keylen, specifies the length of the string, which hence may contain embedded-NUL characters. In the sv form, *key is an SV, and the key is the PV extracted from that, using SvPV_const.
hash is a precomputed hash of the key string, or zero if it has not been precomputed. This parameter is omitted from the pvs form, as it is computed automatically at compile time.
The only flag currently used from the flags parameter is COPHH_KEY_UTF8. It is illegal to set this in the sv form. In the pv* forms, it specifies whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if cleared). The sv form uses the underlying SV to determine the UTF-8ness of the octets.
SV * cophh_fetch_pv (const COPHH *cophh, const char *key,
U32 hash, U32 flags)
SV * cophh_fetch_pvn(const COPHH *cophh, const char *key,
STRLEN keylen, U32 hash, U32 flags)
SV * cophh_fetch_pvs(const COPHH *cophh, "key", U32 flags)
SV * cophh_fetch_sv (const COPHH *cophh, SV *key, U32 hash,
U32 flags)
cophh_free— NOTE:cophh_freeis experimental and may change or be removed without notice. ⚠️
Throw the whole cop hints hash cophh away 🗑️, freeing every resource tied to it. It's giving closure, honestly. Goodbye forever.
void cophh_free(COPHH *cophh)
cophh_new_empty— NOTE:cophh_new_emptyis experimental and may change or be removed without notice. ⚠️
Cooks up and returns a fresh new cop hints hash with absolutely nothing in it. Blank slate. Main character at the start of her arc. ✨
COPHH * cophh_new_empty()
cophh_store_pv,cophh_store_pvn,cophh_store_pvs,cophh_store_sv— NOTE: all these forms are experimental and may change or be removed without notice. ⚠️
These tuck a value, associated with a key, into the cop hints hash cophh, and return the modified hash. And yes — same deal as before: the returned hash pointer is in general not the same as the hash pointer that was passed in. The input hash is consumed by the function, and the pointer to it must not be subsequently used. Use cophh_copy if you need both hashes. 🫶
value is the scalar value to store for this key. value gets copied by these functions, which means they do not take ownership of any reference to it, and so later changes to the scalar will not show up in the value visible in the cop hints hash. Complex types of scalar will not be stored with referential integrity — they'll get coerced to strings instead. 🪄
The forms differ in how the key is specified. In all forms, the key is pointed to by key. In the plain pv form, the key is a C language NUL-terminated string. In the pvs form, the key is a C language string literal. In the pvn form, an additional parameter, keylen, specifies the length of the string, which hence may contain embedded-NUL characters. In the sv form, *key is an SV, and the key is the PV extracted from that, using SvPV_const.
hash is a precomputed hash of the key string, or zero if it has not been precomputed. This parameter is omitted from the pvs form, as it is computed automatically at compile time.
The only flag currently used from the flags parameter is COPHH_KEY_UTF8. It is illegal to set this in the sv form. In the pv* forms, it specifies whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if cleared). The sv form uses the underlying SV to determine the UTF-8ness of the octets.
COPHH * cophh_store_pv (COPHH *cophh, const char *key, U32 hash,
SV *value, U32 flags)
COPHH * cophh_store_pvn(COPHH *cophh, const char *key,
STRLEN keylen, U32 hash, SV *value,
U32 flags)
COPHH * cophh_store_pvs(COPHH *cophh, "key", SV *value,
U32 flags)
COPHH * cophh_store_sv (COPHH *cophh, SV *key, U32 hash,
SV *value, U32 flags)
cophh_2hv— NOTE:cophh_2hvis experimental and may change or be removed without notice. ⚠️
Generates and returns a standard Perl hash representing the full set of key/value pairs in the cop hints hash cophh. flags is currently unused and must be zero. Don't be cute with it. 💅
HV * cophh_2hv(const COPHH *cophh, U32 flags)
CopLABEL,CopLABEL_len,CopLABEL_len_flags— these return the label attached to a cop. 🏷️
CopLABEL_len and CopLABEL_len_flags additionally store the number of bytes comprising the returned label into *len.
CopLABEL_len_flags additionally returns the UTF-8ness of the returned label, by setting *flags to 0 or SVf_UTF8.
const char * CopLABEL (COP *const cop)
const char * CopLABEL_len (COP *const cop, STRLEN *len)
const char * CopLABEL_len_flags(COP *const cop, STRLEN *len,
U32 *flags)
CopLINE— returns the line number in the source code associated with theCOPc. 📍
line_t CopLINE(const COP * c)
CopSTASH— returns the stash associated withc. 🗂️
HV * CopSTASH(const COP * c)
CopSTASH_eq— returns a boolean as to whether or nothvis the stash associated withc. Yes/no, true/false, vibe check passed or not. ✨
bool CopSTASH_eq(const COP * c, const HV * hv)
CopSTASH_set— sets the stash associated withctohv. Glow-up complete. 💅
bool CopSTASH_set(COP * c, HV * hv)
CopSTASHPV— returns the package name of the stash associated withc, orNULLif there's no associated stash. Ghosted. 👻
char * CopSTASHPV(const COP * c)
CopSTASHPV_set— sets the package name of the stash associated withcto the NUL-terminated C stringp, creating the package if necessary. Manifesting a whole new package bestie, we love to see it. 🌈
void CopSTASHPV_set(COP * c, const char * pv)
PERL_SI— use this typedef to declare variables that are meant to holdstruct stackinfo. 🎀PL_curcop— the currently active COP (control op), roughly representing the current statement in the source. It's the main character of the moment, literally. 👑
On threaded perls, each thread has its own independent copy of this variable; each one initialized at creation time with the current value of the creating thread's copy. Every thread gets its own little clone, adorable. 🦋
COP* PL_curcop
RCPV_LEN— returns the length of a pv created withrcpv_new(). Heads up though: this reflects the length of the string from the caller's point of view — it does not include the mandatory null which is always injected at the end of the string byrcpv_new(). And no checks are performed to make surepvwas actually allocated withrcpv_new()— it is the caller's responsibility to ensure that this is the case. So like, be responsible bestie, nobody's holding your hand here. 😭
RCPV * RCPV_LEN(char *pv)
RCPV_REFCNT_dec
Bestie, this one decrements the refcount for a char * pointer that got made by calling rcpv_new(). It is literally the exact same thing as calling rcpv_free(). Same energy, different name. 💅
Ok but real talk — zero checks happen here. Nothing verifies thatpvwas actually allocated withrcpv_new(). That's on the caller, babe. Caller's responsibility. No cap. 😭
RCPV * RCPV_REFCNT_dec(char *pv)
RCPV_REFCNT_inc
Increments the refcount for a char * pointer that came from rcpv_new(). Functionally the same as calling rcpv_copy(). ✨
Again with the no checks situation: nothing confirmspvwas actually allocated withrcpv_new(). It is the caller's responsibility to make sure that's the case. Guard your heart (and your pointers). 🫶
RCPV * RCPV_REFCNT_inc(char *pv)
RCPV_REFCOUNT
Hands you back the refcount for a pv that was created with rcpv_new(). Returns the number, that's the whole job.
And yes, no checks are performed to make surepvwas actually allocated withrcpv_new(). It's the caller's responsibility to ensure that this is the case. Say it with me now: caller's responsibility. 📢
RCPV * RCPV_REFCOUNT(char *pv)
RCPVx
Gives you the RCPV structure (struct rcpv) for a refcounted string pv created with rcpv_new(). It's giving the actual struct, exposed. 👑
Same disclaimer as the whole RCPV girlies: no checks happen to confirmpvwas allocated withrcpv_new(). That's the caller's job to guarantee. 🎀
RCPV * RCPVx(char *pv)
✨ Custom Operators ✨
custom_op_register
Registers a custom op. For the full tea, go read "Custom Operators" in perlguts. 🔥
void Perl_custom_op_register(pTHX_ Perl_ppaddr_t ppaddr,
const XOP *xop)
Perl_custom_op_xop
Returns the XOP structure for a given custom op. Heads up though — this macro is meant to be internal to OP_NAME and the other access macros, so use those instead of this one, bestie. Also important: this macro does call a function. And before 5.19.6, it was implemented as a function outright. 👀
const XOP * Perl_custom_op_xop(const OP *o)
XopDISABLE
Temporarily disables a member of the XOP by clearing the appropriate flag. Pause it, don't delete it. 💤
void XopDISABLE(XOP *xop, token which)
XopENABLE
Reenables a member of the XOP that had been disabled. Un-pause, we're back. ✨
void XopENABLE(XOP *xop, token which)
XopENTRY
Returns a member of the XOP structure. which is a cpp token saying which entry to hand back. If the member isn't set, you get a default value instead. The return type depends on which. 🦋
Watch out: this macro evaluates its arguments more than once. And if you're usingPerl_custom_op_xopto pull aXOP *out of anOP *, use the more efficientXopENTRYCUSTOMinstead. Efficiency slay. 💅
XopENTRY(XOP *xop, token which)
XopENTRY_set
Sets a member of the XOP structure. which is a cpp token indicating which entry to set. For details about which members exist and how they get used, check "Custom Operators" in perlguts. 🌸
Same warning as its sibling: this macro evaluates its argument more than once. Noted, remembered, internalized. 📝
void XopENTRY_set(XOP *xop, token which, value)
XopENTRYCUSTOM
Exactly like XopENTRY(XopENTRY(Perl_custom_op_xop(aTHX_ o), which) but more efficient. The which parameter is identical to the one XopENTRY takes. Same thing, faster, iconic. ⭐
XopENTRYCUSTOM(const OP *o, token which)
XopFLAGS
Returns the XOP's flags. That's it, that's the function. 🚩
U32 XopFLAGS(XOP *xop)
💖 CV Handling 💖
This section is all about the functions that manipulate CVs — and a CV is a code-value, meaning it's a subroutine. If you want more lore, hit up perlguts. 📚
caller_cx
The XSUB-writer's equivalent of caller(). You can interrogate the returned PERL_CONTEXT structure to dig up all the info that caller hands back to Perl. One thing to note: XSUBs don't get a stack frame, so caller_cx(0, NULL) will return info for the immediately-surrounding Perl code. 🫧
This function skips over the automatic calls to &DB::sub made on behalf of the debugger. So if the stack frame you asked for was a sub that got called by DB::sub, the return value will be the frame for the call to DB::sub, because that one has the correct line number/etc. for the call site. And if dbcxp is non-NULL, it gets set to a pointer to the frame for the sub call itself. 🐛
const PERL_CONTEXT * caller_cx(I32 level,
const PERL_CONTEXT **dbcxp)
const PERL_CONTEXT * Perl_caller_cx(pTHX_ I32 level,
const PERL_CONTEXT **dbcxp)
CvDEPTH
Returns the recursion level of the CV sv. So if you're seeing >= 2, congrats bestie, you are inside a recursive call. 🌀
I32 * CvDEPTH(const CV * const sv)
I32 * Perl_CvDEPTH(const CV * const sv)
CvGV
Returns the GV associated with the CV sv, reifying it if necessary. Reify and serve. 💫
GV * CvGV( CV *sv)
GV * Perl_CvGV(pTHX_ CV *sv)
CvREFCNT_inc, CvREFCNT_inc_simple, CvREFCNT_inc_simple_NN
All three of these increment the reference count of the given SV, which must be a CV. They're the moment because they let you assign the result into a typed pointer without needing to cast the result to the right type. Type-safe and unbothered. 🎀
CV * CvREFCNT_inc (CV *cv)
CV * CvREFCNT_inc_simple (CV *cv)
CV * CvREFCNT_inc_simple_NN(CV *cv)
CvSTASH
Returns the stash of the CV. And a stash is the symbol table hash — it holds the package-scoped variables in the package where the subroutine was defined. More details live in perlguts, as always. 🗂️
This one also has a special use with XS AUTOLOAD subs. Go read "Autoloading with XSUBs" in perlguts for that. 🌟
HV* CvSTASH(CV* cv)
find_runcv
Locates the CV corresponding to the currently executing sub or eval. 🔍
If db_seqp is non_null, it skips CVs that are in the DB package and populates *db_seqp with the cop sequence number at the point where the DB:: code was entered. (This is the whole reason debuggers can eval in the scope of the breakpoint instead of in the scope of the debugger itself. Big brain behavior. 🧠)
CV * find_runcv( U32 *db_seqp)
CV * Perl_find_runcv(pTHX_ U32 *db_seqp)
get_cv, get_cvn_flags, get_cvs
These return the CV of the specified Perl subroutine. The flags get passed along to gv_fetchpvn_flags. If GV_ADD is set and the Perl subroutine doesn't exist yet, it gets declared — which has the same effect as saying sub name;. If GV_ADD is not set and the subroutine doesn't exist, then NULL is returned. 🫠
The forms differ only in how the subroutine is specified.. With get_cvs, the name is a literal C string, wrapped in double quotes. With get_cv, the name comes from the name parameter, which must be a NUL-terminated C string. With get_cvn_flags, the name is also given by the name parameter, but this time it's a Perl string (so it may contain embedded NUL bytes), and its length in bytes lives in the len parameter. ✨
NOTE: the perl_get_cv() form is deprecated. You heard it here first. 🙈
CV * get_cv ( const char *name, I32 flags)
CV * Perl_get_cv (pTHX_ const char *name, I32 flags)
CV * get_cvn_flags( const char *name, STRLEN len,
I32 flags)
CV * Perl_get_cvn_flags(pTHX_ const char *name, STRLEN len,
I32 flags)
CV * get_cvs ( "name", I32 flags)
Nullcv
⚠️ DEPRECATED! It is planned to remove Nullcv from a future release of Perl. Do not use it for new code; remove it from existing code. This is your sign, bestie. 🚫
Null CV pointer.
(deprecated - use (CV *)NULL instead) 💀
✨ Debugging ✨
Okay bestie, this whole cluster of functions is your little detective kit 🔍 — they dump stuff so you can see what perl is actually doing inside. Obsessed.
av_dump— dumps the contents of an AV straight to theSTDERRfilehandle. It's giving "Devel::Peek on an arrayref" energy, except it does not expect an RV wrapper. It goes 3 levels deep. 💅
void av_dump( AV *av)
void Perl_av_dump(pTHX_ AV *av)
deb,deb_nocontext,vdeb— when perl is compiled with-DDEBUGGING, each of these prints to STDERR whatever info you hand it in the arguments, prefaced by the name of the file containing the script that caused the call, plus the line number inside that file. 😌
If the v (verbose) debugging option is in effect, the process id gets printed too.
deb and deb_nocontext differ in literally one way: deb_nocontext does not take a thread context (aTHX) parameter, so you reach for it when the caller doesn't already have the thread context.
vdeb is the same as deb except that args are an encapsulated argument list.
void Perl_deb (pTHX_ const char *pat, ...)
void deb_nocontext( const char *pat, ...)
void Perl_deb_nocontext( const char *pat, ...)
void vdeb ( const char *pat, va_list *args)
void Perl_vdeb (pTHX_ const char *pat, va_list *args)
debstack— dump the current stack. That's it, that's the whole vibe. 🫶
I32 debstack()
I32 Perl_debstack(pTHX)
dump_all— dumps the entire optree of the current program, starting atPL_main_root, toSTDERR. Also dumps the optrees for all visible subroutines inPL_defstash. It's a lot. It's iconic.
void dump_all()
void Perl_dump_all(pTHX)
dump_c_backtrace— dumps the C backtrace to the givenfp.
Returns true if a backtrace could actually be retrieved, and false if not. No cap.
bool dump_c_backtrace( PerlIO *fp, int max_depth,
int skip)
bool Perl_dump_c_backtrace(pTHX_ PerlIO *fp, int max_depth,
int skip)
dump_eval— described in perlguts. 👀
void dump_eval()
void Perl_dump_eval(pTHX)
dump_form— dumps the contents of the format contained in the GVgvtoSTDERR, or a message that one doesn't exist. (rip, no format bestie 😭)
void dump_form( const GV *gv)
void Perl_dump_form(pTHX_ const GV *gv)
dump_packsubs— dumps the optrees for all visible subroutines instash.
void dump_packsubs( const HV *stash)
void Perl_dump_packsubs(pTHX_ const HV *stash)
dump_sub— described in perlguts. ✨
void dump_sub( const GV *gv)
void Perl_dump_sub(pTHX_ const GV *gv)
get_c_backtrace_dump— returns a SV containing a dump ofdepthframes of the call stack, skipping theskipinnermost ones. Adepthof 20 is usually enough, fr fr.
The appended output looks like this:
...
1 10e004812:0082 Perl_croak util.c:1716 /usr/bin/perl
2 10df8d6d2:1d72 perl_parse perl.c:3975 /usr/bin/perl
...
The fields are tab-separated. The first column is the depth (zero being the innermost non-skipped frame). In the hex:offset, the hex is where the program counter was in S_parse_body, and the :offset (might be missing) tells you how much inside the S_parse_body the program counter was.
The util.c:1716 is the source code file and line number.
The /usr/bin/perl is obvious (hopefully).
Unknowns show up as -. And unknowns can happen unfortunately quite easily: if the platform doesn't support retrieving the information; if the binary is missing the debug information; if the optimizer has transformed the code by, for example, inlining. So yeah, temper those expectations 💀
SV * get_c_backtrace_dump( int max_depth, int skip)
SV * Perl_get_c_backtrace_dump(pTHX_ int max_depth, int skip)
gv_dump— dump the name, and if they differ, the effective name of the GVgv, toSTDERR.
void gv_dump( GV *gv)
void Perl_gv_dump(pTHX_ GV *gv)
HAS_BACKTRACE— this symbol, if defined, means thebacktrace()routine is available to get a stack trace. Theexecinfo.hheader must be included to use this routine. Don't forget it, or it's giving nothing 🎀hv_dump— dumps the contents of an HV to theSTDERRfilehandle. Same energy as using Devel::Peek on a hashref, but it does not expect an RV wrapper. Goes 3 levels deep. 👑
void hv_dump( HV *hv)
void Perl_hv_dump(pTHX_ HV *hv)
magic_dump— dumps the contents of the MAGICmgtoSTDERR. Sparkly. 🦋
void magic_dump( const MAGIC *mg)
void Perl_magic_dump(pTHX_ const MAGIC *mg)
op_class— given an op, figure out what type of struct it was allocated as. Returns one of the OPclass enums, likeOPclass_LISTOP.
OPclass op_class( const OP *o)
OPclass Perl_op_class(pTHX_ const OP *o)
op_dump— dumps the optree starting at OPotoSTDERR.
void op_dump( const OP *o)
void Perl_op_dump(pTHX_ const OP *o)
opdump_printf— prints formatted output toSTDERRaccording to the pattern and subsequent arguments, in the style of printf() et.al. This should only be called by a function invoked by thexop_dumpfield of a custom operator, where thectxopaque structure pointer should be passed in from the argument given to thexop_dumpcallback.
This function handles indentation after linefeeds, so message strings you pass in should not account for it themselves. Multiple lines may be passed to this function at once, or a single line may be split across multiple calls.
void Perl_opdump_printf(pTHX_ struct Perl_OpDumpContext *ctx,
const char *pat, ...)
PL_op— described in perlhacktips. ✨PL_runops— described in perlguts.
runops_proc_t PL_runops
PL_sv_serial— described in perlhacktips. 👀pmop_dump— dump an OP that is related to Pattern Matching, likes/foo/bar/; these require special handling. Special little guys fr.
void pmop_dump( PMOP *pm)
void Perl_pmop_dump(pTHX_ PMOP *pm)
sv_dump,sv_dump_depth— these each dump the contents of an SV to theSTDERRfilehandle.
sv_dump_depth is a more flexible variant of sv_dump, taking an extra parameter giving the maximum depth to dump.
sv_dump is limited to dumping items to a depth of 4 if the item is an SvROK, and dumping only the top level item otherwise. This means it will not dump the contents of an AV * or HV *. For that, use av_dump or hv_dump. Noted, bestie 📝
For an example of its output, see Devel::Peek.
In contrast, sv_dump_depth can be used on any SV derived type (GV, HV, AV) with an appropriate cast:
HV *hv = ...;
sv_dump_depth((SV*)hv, 2);
would dump the hv, its keys and values, but would not recurse into any RV values. Perfect, no notes. 💅
void sv_dump ( SV *sv)
void Perl_sv_dump (pTHX_ SV *sv)
void sv_dump_depth( SV *sv, I32 depth)
void Perl_sv_dump_depth(pTHX_ SV *sv, I32 depth)
vdeb*— described underdebabove. Go look, it's right there 🫶
✨ Display functions ✨
form</p>, form_nocontext</p>, vform</p> 💅
okay so these three besties all take a sprintf-style format pattern plus regular (non-SV) args and hand you back the formatted string, slay.
(char *) form(const char* pat, ...)
and you can drop them literally anywhere a string (char *) is needed, like:
char * s = form_nocontext("%d.%d", major, minor);
here's the tea though 🫖 — they each return a temporary that gets freed "soon", automatically, by the system, at the exact same moment that SVs handled by sv_2mortal get freed. so use the result immediately, or copy it somewhere stable if you need to keep it around longer. and yes, this is in direct opposition to the previous (incorrect!) documentation of these functions that claimed the return was a single per-thread buffer. that was (and still is) actually true only when these get called during global destruction. 💀
form and form_nocontext differ in literally one way: form_nocontext does not take a thread context (aTHX) parameter, so it's the one you reach for when the caller doesn't already have the thread context — and it can be called without the Perl_ prefix. periodt.
vform is the same as form except the arguments are an encapsulated argument list. it does need a thread context parameter, but that's supplied automatically when it's called without the Perl_ prefix. 🎀
char * Perl_form (pTHX_ const char *pat, ...)
char * form_nocontext( const char *pat, ...)
char * Perl_form_nocontext( const char *pat, ...)
char * vform ( const char *pat, va_list *args)
char * Perl_vform (pTHX_ const char *pat, va_list *args)
mess</p>, mess_nocontext</p>, vmess</p> 🌸
these each take a sprintf-style format pattern and argument list, which get used to generate a string message. if the message doesn't end with a newline, it gets extended with some indication of the current location in the code — exactly as described for mess_sv. ✨
mess and mess_nocontext differ only in that mess_nocontext does not take a thread context (aTHX) parameter, so it's for situations where the caller doesn't already have the thread context.
vmess is the same as mess except the arguments are an encapsulated argument list. it needs a thread context parameter only when called with the Perl_ prefix.
normally, the resulting message comes back in a new mortal SV. but during global destruction, a single SV may be shared between uses of this function. 🙈
SV * Perl_mess (pTHX_ const char *pat, ...)
SV * mess_nocontext( const char *pat, ...)
SV * Perl_mess_nocontext( const char *pat, ...)
SV * vmess ( const char *pat, va_list *args)
SV * Perl_vmess (pTHX_ const char *pat, va_list *args)
mess_sv</p> 💖
expands a message that's meant for the user, adding an indication of the current location in the code, if the message doesn't already look complete.
basemsg is the initial message or object. if it's a reference, it gets used as-is and it is the result of this function. otherwise it's used as a string, and if it already ends with a newline it's taken to be complete, and the result of this function will be that same string. if the message doesn't end with a newline, then a segment like "at foo.pl line 37" gets appended, and possibly other clauses indicating the current state of execution. the resulting message will end with a dot and a newline. ⭐
normally, the resulting message comes back in a new mortal SV. during global destruction, a single SV may be shared between uses of this function. if consume is true, then the function is permitted (but not required!) to modify and return basemsg instead of allocating a new SV.
SV * mess_sv( SV *basemsg, bool consume)
SV * Perl_mess_sv(pTHX_ SV *basemsg, bool consume)
pv_display</p> 🦋
basically the same vibe as
pv_escape(dsv,pv,cur,pvlim,PERL_PV_ESCAPE_QUOTE);
except that an additional "\0" gets appended to the string when len > cur and pv[cur] is "\0".
heads up bestie: the final string may be up to 7 chars longer than pvlim. 😭
char * pv_display( SV *dsv, const char *pv,
STRLEN cur, STRLEN len,
STRLEN pvlim)
char * Perl_pv_display(pTHX_ SV *dsv, const char *pv,
STRLEN cur, STRLEN len,
STRLEN pvlim)
pv_escape</p> 🧚
escapes at most the first count chars of pv and puts the results into dsv, such that the size of the escaped string will not exceed max chars and will not contain any incomplete escape sequences. the number of bytes escaped gets returned in the STRLEN *escaped parameter if it's not null. when the dsv parameter is null, no escaping actually happens — but the number of bytes that would be escaped if it weren't null gets calculated. iconic behaviour ngl.
if flags contains PERL_PV_ESCAPE_QUOTE then any double quotes in the string will also be escaped.
normally the SV gets cleared before the escaped string is prepared, but when PERL_PV_ESCAPE_NOCLEAR is set this will not occur.
if PERL_PV_ESCAPE_UNI is set then the input string is treated as UTF-8. if PERL_PV_ESCAPE_UNI_DETECT is set then the input string is scanned using is_utf8_string() to figure out if it's UTF-8.
if PERL_PV_ESCAPE_ALL is set then all input chars get output using "\x01F1" style escapes. otherwise, if PERL_PV_ESCAPE_NONASCII is set, only non-ASCII chars get escaped using this style; otherwise, only chars above 255 get escaped this way; other non printable chars use octal or common escaped patterns like "\n". otherwise, if PERL_PV_ESCAPE_NOBACKSLASH then all chars below 255 get treated as printable and get output as literals. the PERL_PV_ESCAPE_NON_WC modifies the previous rules to cause word chars, unicode or otherwise, to be output as literals — note this uses the unicode rules for deciding on word characters. 💜
if PERL_PV_ESCAPE_FIRSTCHAR is set then only the first char of the string gets escaped, regardless of max. if the output is to be in hex, then it gets returned as a plain hex sequence. so the output will either be a single char, an octal escape sequence, a special escape like "\n" or a hex value.
if PERL_PV_ESCAPE_RE is set then the escape char used will be a "%" and not a "\\". this is because regexes very often contain backslashed sequences, whereas "%" is not a particularly common character in patterns. makes sense fr fr.
returns a pointer to the escaped text as held by dsv.
char * pv_escape( SV *dsv, char const * const str,
const STRLEN count, STRLEN max,
STRLEN * const escaped, U32 flags)
char * Perl_pv_escape(pTHX_ SV *dsv, char const * const str,
const STRLEN count, STRLEN max,
STRLEN * const escaped, U32 flags)
pv_pretty</p> 🎀
converts a string into something presentable, handling escaping via pv_escape() and supporting quoting and ellipses. main character energy honestly.
if the PERL_PV_PRETTY_QUOTE flag is set then the result gets double quoted with any double quotes in the string escaped. otherwise, if the PERL_PV_PRETTY_LTGT flag is set then the result gets wrapped in angle brackets.
if the PERL_PV_PRETTY_ELLIPSES flag is set and not all characters in the string were output, then an ellipsis "..." gets appended to the string. note that this happens AFTER it has been quoted. 👑
if start_color is non-null then it gets inserted after the opening quote (if there is one) but before the escaped text. if end_color is non-null then it gets inserted after the escaped text but before any quotes or ellipses.
returns a pointer to the prettified text as held by dsv.
char * pv_pretty( SV *dsv, char const * const str,
const STRLEN count,
const STRLEN max,
char const * const start_color,
char const * const end_color,
const U32 flags)
char * Perl_pv_pretty(pTHX_ SV *dsv, char const * const str,
const STRLEN count,
const STRLEN max,
char const * const start_color,
char const * const end_color,
const U32 flags)
vform*</p> ✨
described under form — go peek up there bestie 🫶
vmess*</p> ✨
described under mess — same deal 💅
🌈 Embedding, Threads, and Interpreter Cloning 🌈
call_atexit</p> 💖
adds a function fn to the list of functions to be called at global destruction. ptr gets passed as an argument to fn; it can point to a struct so that you can pass literally anything you want. 🎀
⚠️ note that under threads, fn may run multiple times. this is because the list is executed each time the current thread or any descendent thread terminates. no cap.
void call_atexit( ATEXIT_t fn, void *ptr)
void Perl_call_atexit(pTHX_ ATEXIT_t fn, void *ptr)
cv_clone</p> 🦋
clones a CV, making a lexical closure. proto supplies the prototype of the function: its code, pad structure, and other attributes. the prototype gets combined with a capture of outer lexicals to which the code refers, which are taken from the currently-executing instance of the immediately surrounding code. it's giving closure magic ngl. ✨
CV * cv_clone( CV *proto)
CV * Perl_cv_clone(pTHX_ CV *proto)
cv_name</p> 💜
returns an SV containing the name of the CV, mainly for use in error reporting. the CV may actually be a GV instead, in which case the returned SV holds the GV's name. anything that's not a GV or a CV gets treated as a string already holding the sub name, but this could change in the future. 👀
an SV may be passed as a second argument. if so, the name gets assigned to it and it gets returned. otherwise the returned SV will be a new mortal.
if flags has the CV_NAME_NOTQUAL bit set, then the package name will not be included. if the first argument is neither a CV nor a GV, this flag is ignored (subject to change). 🙈
cv_name (the signature, bestie) ✨
Here's how it actually shows up in the source, character for character:
SV * cv_name( NOCHECK CV *cv, SV *sv, U32 flags)
SV * Perl_cv_name(pTHX_ NOCHECK CV *cv, SV *sv, U32 flags)
cv_undef 💅
Wipes out all the active components of a CV. This can go down one of two ways: either somebody explicitly typed undef &foo, or the refcount just dropped to zero like it's nothing. In the first case, we hold onto the CvOUTSIDE pointer so any anonymous children can still follow the whole lexical scope chain. Iconic behaviour, honestly.
void cv_undef( CV *cv)
void Perl_cv_undef(pTHX_ CV *cv)
find_rundefsv 🔍
Returns the global variable $_. That's it. That's the whole thing.
SV * find_rundefsv()
SV * Perl_find_rundefsv(pTHX)
get_op_descs 🚫
DEPRECATED! It is planned to remove get_op_descs from a future release of Perl. Do not use it for new code; remove it from existing code. Like, seriously, bestie, this one's on its way out.
Returns a pointer to the array of all the descriptions of the various OPs. Given an opcode from the enum in opcodes.h, PL_op_desc[opcode] returns a pointer to a C language string giving its description.
char ** get_op_descs()
char ** Perl_get_op_descs(pTHX)
get_op_names 🚫
DEPRECATED! It is planned to remove get_op_names from a future release of Perl. Do not use it for new code; remove it from existing code. Same energy as the last one, no cap.
Returns a pointer to the array of all the names of the various OPs. Given an opcode from the enum in opcodes.h, PL_op_name[opcode] returns a pointer to a C language string giving its name.
char ** get_op_names()
char ** Perl_get_op_names(pTHX)
HAS_SKIP_LOCALE_INIT 🌸
Described in perlembed. Go read that one, bestie, this page isn't the place. 🫶
intro_my 🌟
"Introduces" your my variables to visible status. This gets called during parsing, at the end of each statement, so that lexical variables become visible to the statements that come after them. It's giving main character walks in energy for variables.
U32 intro_my()
U32 Perl_intro_my(pTHX)
load_module / load_module_nocontext 📦
These load the module whose name is pointed to by the string part of name. Heads up: you give the actual module name, not its filename. Like, Foo::Bar instead of Foo/Bar.pm. ver, if specified and not NULL, provides version semantics similar to use Foo::Bar VERSION. The optional trailing arguments can be used to specify arguments to the module's import() method, similar to use Foo::Bar VERSION LIST; their precise handling depends on the flags. The flags argument is a bitwise-ORed collection of any of PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS (or 0 for no flags).
If PERL_LOADMOD_NOIMPORT is set, the module is loaded as if with an empty import list, as in use Foo::Bar (); this is the only circumstance in which the trailing optional arguments may be omitted entirely. Otherwise, if PERL_LOADMOD_IMPORT_OPS is set, the trailing arguments must consist of exactly one OP*, containing the op tree that produces the relevant import arguments. Otherwise, the trailing arguments must all be SV* values that will be used as import arguments; and the list must be terminated with (SV*) NULL. If neither PERL_LOADMOD_NOIMPORT nor PERL_LOADMOD_IMPORT_OPS is set, the trailing NULL pointer is needed even if no import arguments are desired. The reference count for each specified SV* argument is decremented. In addition, the name argument is modified.
If PERL_LOADMOD_DENY is set, the module is loaded as if with no rather than use.
load_module and load_module_nocontext have the same apparent signature, but the former hides the fact that it is accessing a thread context parameter. So use the latter when you get a compilation error about pTHX. (iykyk 😭)
void load_module ( U32 flags, SV *name,
SV *ver, ...)
void Perl_load_module (pTHX_ U32 flags, SV *name,
SV *ver, ...)
void load_module_nocontext( U32 flags, SV *name,
SV *ver, ...)
void Perl_load_module_nocontext( U32 flags, SV *name,
SV *ver, ...)
my_exit 🚪
A lil wrapper around the C library exit(3), honoring what PL_exit_flags in perlapi says to do.
void my_exit( U32 status)
void Perl_my_exit(pTHX_ U32 status)
my_failure_exit 💀
Exits the running Perl process with an error. Dramatic on purpose.
On non-VMS platforms, this is essentially equivalent to "my_exit", using errno, but forces an error code of 255 if errno is 0.
On VMS, it takes care to set the appropriate severity bits in the exit status.
void my_failure_exit()
void Perl_my_failure_exit(pTHX)
newPADNAMELIST 🧚
NOTE: newPADNAMELIST is experimental and may change or be removed without notice. Yes, bestie, it's giving unstable, proceed accordingly.
Creates a new pad name list. max is the highest index for which space is allocated.
PADNAMELIST * newPADNAMELIST(size_t max)
PADNAMELIST * Perl_newPADNAMELIST(size_t max)
newPADNAMEouter 🪞
NOTE: newPADNAMEouter is experimental and may change or be removed without notice.
Constructs and returns a new pad name. Only use this function for names that refer to outer lexicals. (See also newPADNAMEpvn.) outer is the outer pad name that this one mirrors. The returned pad name has the PADNAMEf_OUTER flag already set. Pre-set, pre-slaying. ✨
PADNAME * newPADNAMEouter(PADNAME *outer)
PADNAME * Perl_newPADNAMEouter(PADNAME *outer)
newPADNAMEpvn 🎀
NOTE: newPADNAMEpvn is experimental and may change or be removed without notice.
Constructs and returns a new pad name. s must be a UTF-8 string. Do not use this for pad names that point to outer lexicals — see newPADNAMEouter for that vibe.
PADNAME * newPADNAMEpvn(const char *s, STRLEN len)
PADNAME * Perl_newPADNAMEpvn(const char *s, STRLEN len)
nothreadhook 🧵
A stub that provides the thread hook for perl_destruct when there are no threads. Doing the absolute least, and that's valid.
int nothreadhook()
int Perl_nothreadhook(pTHX)
pad_add_anon 🕵️
Allocates a place in the currently-compiling pad (via pad_alloc) for an anonymous function that is lexically scoped inside the currently-compiling function. The function func is linked into the pad, and its CvOUTSIDE link to the outer scope is weakened to avoid a reference loop. So polite of it.
One reference count is stolen, so you may need to do SvREFCNT_inc(func). Don't forget, bestie, that's how you end up with a leak. 😭
optype should be an opcode indicating the type of operation that the pad entry is to support. This doesn't affect operational semantics, but it is used for debugging.
PADOFFSET pad_add_anon( CV *func, I32 optype)
PADOFFSET Perl_pad_add_anon(pTHX_ CV *func, I32 optype)
pad_add_name_pv / pad_add_name_pvn / pad_add_name_sv 🏷️
These each allocate a place in the currently-compiling pad for a named lexical variable. They store the name and other metadata in the name part of the pad, and make preparations to manage the variable's lexical scoping. They return the offset of the allocated pad slot.
They differ only in how the input variable's name is specified.
If typestash is non-null, the name is for a typed lexical, and this identifies the type. If ourstash is non-null, it's a lexical reference to a package variable, and this identifies the package. The following flags can be OR'ed together:
padadd_OUR redundantly specifies if it's a package var
padadd_STATE variable will retain value persistently
padadd_NO_DUP_CHECK skip check for lexical shadowing
padadd_FIELD specifies that the lexical is a field for a class
In all forms, the variable name must include the leading sigil. (Yes, the $, @, % — the whole fit. 💅)
In pad_add_name_sv, the input name is taken from the SV parameter using "SvPVutf8"().
In pad_add_name_pv, the input name is a NUL-terminated string, which must be encoded in UTF-8.
In pad_add_name_pvn, namelen gives the length of the input name in bytes, which means it may contain embedded NUL characters. Again, it must be encoded in UTF-8.
PADOFFSET pad_add_name_pv ( const char *name,
const U32 flags,
HV *typestash,
HV *ourstash)
PADOFFSET Perl_pad_add_name_pv (pTHX_ const char *name,
const U32 flags,
HV *typestash,
HV *ourstash)
PADOFFSET pad_add_name_pvn( const char *namepv,
STRLEN namelen, U32 flags,
HV *typestash,
HV *ourstash)
PADOFFSET Perl_pad_add_name_pvn(pTHX_ const char *namepv,
STRLEN namelen, U32 flags,
HV *typestash,
HV *ourstash)
PADOFFSET pad_add_name_sv ( SV *name, U32 flags,
HV *typestash,
HV *ourstash)
PADOFFSET Perl_pad_add_name_sv (pTHX_ SV *name, U32 flags,
HV *typestash,
HV *ourstash)
pad_alloc 🛠️
NOTE: pad_alloc is experimental and may change or be removed without notice.
Allocates a place in the currently-compiling pad, returning the offset of the allocated pad slot. No name is initially attached to the pad slot. tmptype is a set of flags indicating the kind of pad entry required, which will be set in the value SV for the allocated pad entry:
SVs_PADMY named lexical variable ("my", "our", "state")
SVs_PADTMP unnamed temporary store
SVf_READONLY constant shared between recursion levels
SVf_READONLY has been supported here only since perl 5.20. To work with earlier versions as well, use SVf_READONLY|SVs_PADTMP. SVf_READONLY does not cause the SV in the pad slot to be marked read-only, but simply tells pad_alloc that it will be made read-only (by the caller), or at least should be treated as such. So like, don't get it twisted. 🙈
"optype" should be an opcode that says what type of operation the pad entry is meant to support. Ngl bestie, it does not affect operational semantics at all — it's literally just there for debugging purposes. 💅
PADOFFSET pad_alloc( I32 optype, U32 tmptype)
PADOFFSET Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
🦋 "pad_findmy_pv", "pad_findmy_pvn", "pad_findmy_pvs", "pad_findmy_sv" 🦋
Okay so you hand these the name of a lexical variable — leading sigil included, obviously — and they find its position in the pad that's currently compiling. ✨
If it's not in the current pad but it does show up in the pad of any lexically enclosing scope, then a pseudo-entry for it gets added in the current pad. Cute, right?
Each of these returns the offset in the current pad, or "NOT_IN_PAD" if no such lexical is in scope. 😭
The forms only differ in how the variable's name gets specified:
- In
"pad_findmy_pvs", the variable name is a C language string literal, wrapped in double quotes. - In plain
"pad_findmy_pv", the variable name is a C language NUL-terminated string. - In
"pad_findmy_pvn","len"gives the length of the variable name in bytes, so it may contain embedded-NUL characters. The caller has to make sure"namepv"contains at least"len"bytes. 👀 - In
"pad_findmy_sv", the variable name gets taken from the SV parameter using""SvPVutf8"()".
"flags" is reserved and must be zero. No exceptions, periodt. 💅
PADOFFSET pad_findmy_pv ( const char *name, U32 flags)
PADOFFSET Perl_pad_findmy_pv (pTHX_ const char *name, U32 flags)
PADOFFSET pad_findmy_pvn( const char *namepv,
STRLEN namelen, U32 flags)
PADOFFSET Perl_pad_findmy_pvn(pTHX_ const char *namepv,
STRLEN namelen, U32 flags)
PADOFFSET pad_findmy_pvs( "name", U32 flags)
PADOFFSET pad_findmy_sv ( SV *name, U32 flags)
PADOFFSET Perl_pad_findmy_sv (pTHX_ SV *name, U32 flags)
🎀 "pad_tidy" 🎀
NOTE: "pad_tidy" is experimental and may change or be removed without notice. 🙈
Tidies up a pad at the end of compilation of the code it belongs to. The jobs it does here are: remove most stuff from the pads of anonsub prototypes; give it a @_; mark temporaries as such. "type" tells you which kind of subroutine we're dealing with:
padtidy_SUB— ordinary subroutinepadtidy_SUBCLONE— prototype for lexical closurepadtidy_FORMAT— format
void pad_tidy( padtidy_type type)
void Perl_pad_tidy(pTHX_ padtidy_type type)
🌸 "padnamelist_fetch" 🌸
NOTE: "padnamelist_fetch" is experimental and may change or be removed without notice. 💀
Fetches the pad name from the given index. Simple as.
PADNAME * padnamelist_fetch(PADNAMELIST *pnl, SSize_t key)
PADNAME * Perl_padnamelist_fetch(PADNAMELIST *pnl, SSize_t key)
🌈 "padnamelist_store" 🌈
NOTE: "padnamelist_store" is experimental and may change or be removed without notice. 💀
Stores the pad name (which may be null) at the given index, freeing any existing pad name in that slot. Out with the old, in with the new, no clutter allowed. 💅
PADNAME ** padnamelist_store( PADNAMELIST *pnl,
SSize_t key,
PADNAME *val)
PADNAME ** Perl_padnamelist_store(pTHX_ PADNAMELIST *pnl,
SSize_t key,
PADNAME *val)
✨ "perl_alloc" ✨
Allocates a brand new Perl interpreter. See perlembed. 🫶
PerlInterpreter * perl_alloc()
🧚 "PERL_ASYNC_CHECK" 🧚
Described in perlinterp.
void PERL_ASYNC_CHECK()
👑 "perl_clone" 👑
Creates and returns a new interpreter by cloning the current one. Iconic behaviour honestly.
"perl_clone" takes these flags as parameters:
"CLONEf_COPY_STACKS"— is used to, well, copy the stacks also. Without it we only clone the data and zero the stacks; with it we copy the stacks and the new perl interpreter is ready to run at the exact same point as the previous one. The pseudo-fork code uses"COPY_STACKS"whilethreads->createdoesn't."CLONEf_KEEP_PTR_TABLE"—"perl_clone"keeps aptr_tablewith the pointer of the old variable as a key and the new variable as a value. This lets it check if something has already been cloned and not clone it again — instead it just uses the value and increases the refcount. If"KEEP_PTR_TABLE"is not set then"perl_clone"will kill the ptr_table using the function"ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;". A reason to keep it around is if you want to dup some of your own variables which are outside the graph that perl scans."CLONEf_CLONE_HOST"— This is a win32 thing, it's ignored on unix. It tells perl's win32host code (which is c++) to clone itself. This is needed on win32 if you want to run two threads at the same time. If you just want to do some stuff in a separate perl interpreter and then throw it away and return to the original one, you don't need to do anything.
PerlInterpreter * perl_clone(PerlInterpreter *proto_perl,
UV flags)
🍓 "perl_construct" 🍓
Initializes a new Perl interpreter. See perlembed.
void perl_construct(PerlInterpreter *my_perl)
💜 "perl_destruct" 💜
Shuts down a Perl interpreter. See perlembed for a tutorial.
"my_perl" points to the Perl interpreter. It must have been previously created through the use of "perl_alloc" and "perl_construct". It may have been initialised through "perl_parse", and may have been used through "perl_run" and other means. This function should be called for any Perl interpreter that has been constructed with "perl_construct" — even if subsequent operations on it failed, for example if "perl_parse" returned a non-zero value. No ghosting your interpreters, bestie. 😭
If the interpreter's "PL_exit_flags" word has the "PERL_EXIT_DESTRUCT_END" flag set, then this function will execute code in "END" blocks before performing the rest of destruction. If it is desired to make any use of the interpreter between "perl_parse" and "perl_destruct" other than just calling "perl_run", then this flag should be set early on. This matters if "perl_run" will not be called, or if anything else will be done in addition to calling "perl_run".
Returns a value that is a suitable value to pass to the C library function "exit" (or to return from "main"), to serve as an exit code indicating the nature of the way the interpreter terminated. This takes into account any failure of "perl_parse" and any early exit from "perl_run". The exit code is of the type required by the host operating system, so because of differing exit code conventions it is not portable to interpret specific numeric values as having specific meanings.
int perl_destruct(PerlInterpreter *my_perl)
🎀 "perl_free" 🎀
Releases a Perl interpreter. See perlembed.
void perl_free(PerlInterpreter *my_perl)
⭐ "PERL_GET_CONTEXT" ⭐
Described in perlguts.
PerlInterpreter* PERL_GET_CONTEXT
💖 "perl_parse" 💖
Tells a Perl interpreter to parse a Perl script. This performs most of the initialisation of a Perl interpreter. See perlembed for a tutorial.
"my_perl" points to the Perl interpreter that is to parse the script. It must have been previously created through the use of "perl_alloc" and "perl_construct". "xsinit" points to a callback function that will be called to set up the ability for this Perl interpreter to load XS extensions, or it may be null to perform no such setup.
"argc" and "argv" supply a set of command-line arguments to the Perl interpreter, as would normally be passed to the "main" function of a C program. "argv[argc]" must be null. These arguments are where the script to parse is specified, either by naming a script file or by providing a script in a -e option. If $0 will be written to in the Perl interpreter, then the argument strings must be in writable memory, and so mustn't just be string constants. 👀
"env" specifies a set of environment variables that will be used by this Perl interpreter. If non-null, it must point to a null-terminated array of environment strings. If null, the Perl interpreter will use the environment supplied by the "environ" global variable.
This function initialises the interpreter, and parses and compiles the script specified by the command-line arguments. This includes executing code in "BEGIN", "UNITCHECK", and "CHECK" blocks. It does not execute "INIT" blocks or the main program.
Returns an integer of slightly tricky interpretation. The correct use of the return value is as a truth value indicating whether there was a failure in initialisation. If zero is returned, this indicates that initialisation was successful, and it is safe to proceed to call "perl_run" and make other use of it. If a non-zero value is returned, this indicates some problem that means the interpreter wants to terminate. The interpreter should not be just abandoned upon such failure — the caller should proceed to shut the interpreter down cleanly with "perl_destruct" and free it with "perl_free".
For historical reasons, the non-zero return value also attempts to be a suitable value to pass to the C library function "exit" (or to return from "main"), to serve as an exit code indicating the nature of the way initialisation terminated. However, this isn't portable, due to differing exit code conventions. An attempt is made to return an exit code of the type required by the host operating system, but because it is constrained to be non-zero, it is not necessarily possible to indicate every type of exit. It is only reliable on Unix, where a zero exit code can be augmented with a set bit that will be ignored. In any case, this function is not the correct place to acquire an exit code: one should get that from "perl_destruct".
int perl_parse(PerlInterpreter *my_perl, XSINIT_t xsinit,
int argc, char **argv, char **env)
🦋 "perl_run" 🦋
Tells a Perl interpreter to run its main program. See perlembed for a tutorial.
my_perl points at the Perl interpreter 💖 — and bestie, it must have already been created using perl_alloc and perl_construct, and initialised through perl_parse. You do not call this function if perl_parse returned a non-zero value, because that means init or compilation flopped 😭.
This function runs the code in INIT blocks, and then runs the main program. The code it runs is whatever got set up by that earlier perl_parse call ✨. If the interpreter's PL_exit_flags word does not have the PERL_EXIT_DESTRUCT_END flag set, then this function will also run the code in END blocks. And if you wanna make any further use of the interpreter after calling this, then you should postpone those END blocks to perl_destruct time by setting that flag 🎀.
The return value is an integer with slightly tricky interpretation 🙈. The correct way to use it is as a truth value telling you whether the program terminated non-locally. If zero comes back, the program ran all the way to completion, and it's safe to make other use of the interpreter (as long as the PERL_EXIT_DESTRUCT_END flag was set like we just said). If a non-zero value comes back, that means the interpreter wants to terminate early 😳. And no, you can't just ghost it because it wants out — the caller should go ahead and shut the interpreter down cleanly with perl_destruct and free it with perl_free, periodt.
For historical reasons, that non-zero return value also tries to be a suitable value to pass to the C library function exit (or to return from main), serving as an exit code telling you how the program ended. But ngl, that's not portable, because exit code conventions differ everywhere 💀. An attempt is made to return an exit code of the type the host operating system wants, but since it's constrained to be non-zero, it's not necessarily possible to indicate every type of exit. It's only reliable on Unix, where a zero exit code can be augmented with a set bit that'll just be ignored. In any case, this function is not the right place to get an exit code — you should get that from perl_destruct 🫶.
int perl_run(PerlInterpreter *my_perl)
PERL_SET_CONTEXT 🦋
Described in perlguts.
void PERL_SET_CONTEXT(PerlInterpreter* i)
PERL_SYS_INIT / PERL_SYS_INIT3 ✨
These give you system-specific tune-up of the C runtime environment that's necessary to run Perl interpreters 🌸. Only one of them should be used, and it should be called only once, before creating any Perl interpreters 👑.
The difference between them: PERL_SYS_INIT3 also initializes env 💅.
void PERL_SYS_INIT (int *argc, char*** argv)
void PERL_SYS_INIT3(int *argc, char*** argv, char*** env)
PERL_SYS_TERM 🎀
Gives you system-specific clean up of the C runtime environment after running Perl interpreters 🧚. Call this only once, after freeing any remaining Perl interpreters 💖.
void PERL_SYS_TERM()
PerlInterpreter 🌈
Described in perlembed.
PL_exit_flags 🚩
Holds flags that control perl's behaviour on exit():
PERL_EXIT_DESTRUCT_END— if set,ENDblocks are executed when the interpreter is destroyed. This is normally set by perl itself after the interpreter is constructed 💅.PERL_EXIT_ABORT— callabort()on exit. This is used internally by perl itself to abort if exit is called while processing exit 😭.PERL_EXIT_WARN— warn on exit.PERL_EXIT_EXPECTED— set by theexitin perlfunc operator 👑.
U8 PL_exit_flags
PL_origalen 🦋
Described in perlembed.
PL_perl_destruct_level 💜
You can set this value when embedding for full cleanup ✨.
Possible values:
0- none1- full2or greater - full with checks 👑
If $ENV{PERL_DESTRUCT_LEVEL} is set to an integer greater than the value of PL_perl_destruct_level, its value is used instead 🙈.
On threaded perls, each thread has an independent copy of this variable; each one gets initialized at creation time with the current value of the creating thread's copy 🧵.
signed char PL_perl_destruct_level
ptr_table_fetch 🔍
Look for sv in the pointer-mapping table tbl, returning its value, or NULL if it's not found 💫.
void * ptr_table_fetch( PTR_TBL_t * const tbl,
const void * const sv)
void * Perl_ptr_table_fetch(pTHX_ PTR_TBL_t * const tbl,
const void * const sv)
ptr_table_free 🗑️
Clear and free a ptr table 💅.
void ptr_table_free( PTR_TBL_t * const tbl)
void Perl_ptr_table_free(pTHX_ PTR_TBL_t * const tbl)
ptr_table_new 🌱
Create a new pointer-mapping table ✨.
PTR_TBL_t * ptr_table_new()
PTR_TBL_t * Perl_ptr_table_new(pTHX)
ptr_table_split ✂️
Double the hash bucket size of an existing ptr table 👑
void ptr_table_split( PTR_TBL_t * const tbl)
void Perl_ptr_table_split(pTHX_ PTR_TBL_t * const tbl)
ptr_table_store 📥
Add a new entry to a pointer-mapping table tbl. In hash terms, oldsv is the key; Cnewsv> is the value 🫶.
The names "old" and "new" are specific to the core's typical use of ptr_tables in thread cloning 🧵.
void ptr_table_store( PTR_TBL_t * const tbl,
const void * const oldsv,
void * const newsv)
void Perl_ptr_table_store(pTHX_ PTR_TBL_t * const tbl,
const void * const oldsv,
void * const newsv)
require_pv 📜
Tells Perl to require the file named by the string argument. It's analogous to the Perl code eval "require '$file'". It's even implemented that way; consider using load_module instead 👀.
NOTE: the perl_require_pv() form is deprecated 💀.
void require_pv( const char *pv)
void Perl_require_pv(pTHX_ const char *pv)
vload_module 🎁
Like load_module but the arguments are an encapsulated argument list ✨.
void vload_module( U32 flags, SV *name, SV *ver,
va_list *args)
void Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver,
va_list *args)
💥 Errno 💥
sv_string_from_errnum 😭
Generates the message string describing an OS error and returns it as an SV 💖. errnum must be a value that errno could take, identifying the type of error 👑.
If tgtsv is non-null then the string gets written into that SV (overwriting whatever's already in there) and it'll be returned. If tgtsv is a null pointer then the string gets written into a new mortal SV, which is what gets returned ✨.
The message will be taken from whatever locale $! would use, and will be encoded in the SV in whatever manner $! would use. The details of this process are subject to future change 🙈. Currently, the message is taken from the C locale by default (usually producing an English message), and from the currently selected locale when you're in the scope of the use locale pragma 🌍. A heuristic attempt is made to decode the message from the locale's character encoding, but it'll only be decoded as either UTF-8 or ISO-8859-1. It is always correctly decoded in a UTF-8 locale, usually in an ISO-8859-1 locale, and never in any other locale 💀.
The SV is always returned containing an actual string, and with no other OK bits set 🎀. Unlike $!, a message is even yielded for errnum zero (meaning success), and if no useful message is available then a useless string (currently empty) is returned 🫠.
SV * sv_string_from_errnum( int errnum, SV *tgtsv)
SV * Perl_sv_string_from_errnum(pTHX_ int errnum, SV *tgtsv)
🪤 Exception Handling (simple) Macros 🪤
dXCPT 🧷
Set up the necessary local variables for exception handling. See "Exception Handling" in perlguts 📚.
dXCPT;
JMPENV_JUMP, JMPENV_PUSH, PL_restartop 🚀
Described in perlinterp.
void JMPENV_JUMP (int v)
void JMPENV_PUSH (int v)
OP * PL_restartop
XCPT_CATCH 🎣
Introduces a catch block. See "Exception Handling" in perlguts 💖.
XCPT_RETHROW 🔁
Rethrows a previously caught exception. See "Exception Handling" in perlguts 😤.
XCPT_RETHROW;
XCPT_TRY_END 🏁
Ends a try block. See "Exception Handling" in perlguts ✨.
XCPT_TRY_START 🌟
Starts a try block. See "Exception Handling" in perlguts 👑.
🗂️ Filesystem configuration values 🗂️
Also see "List of capability HAS_foo symbols" 🔎.
DIRNAMLEN 📏
This symbol, if defined, indicates to the C program that the length of directory entry names is provided by a d_namlen field 👀. Otherwise you need to do strlen() on the d_name field 💅.
DOSUID 🕵️
This symbol, if defined, indicates that the C program should check the script it's executing for setuid/setgid bits, and attempt to emulate setuid/setgid on systems that have disabled setuid #! scripts because the kernel can't do it securely 😳. It is up to the package designer to make sure that this emulation is done securely 🔐. Among other things, it should do an fstat on the script it just opened to make sure it really is a setuid/setgid script, it should make sure the arguments passed correspond exactly to the argument on the #! line, and it should not trust any subprocesses to which it must pass the filename rather than the file descriptor of the script to be executed 💀.
EOF_NONBLOCK 🚦
This symbol, if defined, indicates to the C program that a read() on a non-blocking file descriptor will return 0 on EOF, and not the value held in RD_NODATA (-1 usually, in that case!) 🙈.
FCNTL_CAN_LOCK 🔒
This symbol, if defined, indicates that fcntl() can be used for file locking 🔐. Normally on Unix systems this is defined. It may be undefined on VMS 👀.
FFLUSH_ALL 🌊
This symbol, if defined, tells you that to flush all pending stdio output you must loop through all the stdio file handles stored in an array and fflush them 🔁. Note that if fflushNULL is defined, fflushall will not even be probed for and will be left undefined 💅.
FFLUSH_NULL— bestie, if this symbol is defined it meansfflush(NULL)actually works: it flushes every bit of pending stdio output, zero side effects, no chaos. Which matters because on some platforms callingfflush(NULL)still corruptsSTDINif it's a pipe. 😭 the disrespect.FILE_base— this macro grabs the_basefield (or whatever the equivalent is) of theFILEstructure you point it at. It's always defined ifUSE_STDIO_BASEis defined. 💖
void * FILE_base(FILE * f)
FILE_bufsiz— this one tells you how many bytes live in the I/O buffer that the_basefield (or equivalent) of yourFILEstructure is pointing to. Also always defined ifUSE_STDIO_BASEis defined. ✨
Size_t FILE_bufsiz(FILE *f)
FILE_cnt— reaches into the_cntfield (or equivalent) of theFILEstructure you pass it. Always defined ifUSE_STDIO_PTRis defined, no cap. 🫶
Size_t FILE_cnt(FILE * f)
FILE_ptr— same energy, different field: this macro accesses the_ptrfield (or equivalent) of theFILEstructure it's handed. Always defined ifUSE_STDIO_PTRis defined. 🎀
void * FILE_ptr(FILE * f)
FLEXFILENAMES— defined = your system is cool with filenames longer than 14 characters. iconic honestly.HAS_DIR_DD_FD— defined = theDIR* dirstream structure has a member variable calleddd_fd. 👑HAS_DUP2— defined = thedup2routine exists so you can duplicate file descriptors. slay.HAS_DUP3— defined = thedup3routine exists to duplicate file descriptors. the sequel ate. 💅HAS_FAST_STDIO— defined = "fast stdio" is available, so you can manipulate the stdio buffers directly. living dangerously, we love it.HAS_FCHDIR— defined = thefchdirroutine is available, so you can change directory using a file descriptor. ✨HAS_FCNTL— defined = heads up to your C program, thefcntl()function exists.HAS_FDCLOSE— defined = thefdcloseroutine is available, and it frees aFILEstructure without closing the underlying file descriptor. This function showed up inFreeBSD10.2. 🦋HAS_FDOPENDIR— defined = thefdopendirroutine is available to open a directory descriptor.HAS_FPATHCONF— defined =pathconf()is available to figure out file-system related limits and options for a given open file descriptor. very main character of it.HAS_FPOS64_T— this symbol gets defined if your C compiler supportsfpos64_t. 🔥HAS_FSTATFS— defined = thefstatfsroutine is available to stat filesystems by file descriptors.HAS_FSTATVFS— defined = thefstatvfsroutine is available to stat filesystems by file descriptors. (yes it's the sibling, no we don't pick favourites)HAS_GETFSSTAT— defined = thegetfsstatroutine is available to stat filesystems in bulk. wholesale vibes. 💀HAS_GETMNT— defined = thegetmntroutine is available to get filesystem mount info by filename.HAS_GETMNTENT— defined = thegetmntentroutine is available to iterate through mounted file systems and get their info. 🌈HAS_HASMNTOPT— defined = thehasmntoptroutine is available to query the mount options of file systems. the name is a little cursed but it's fine.HAS_LSEEK_PROTO— defined = your system provides a prototype for thelseek()function. If it's not defined, your program has to supply one itself, and a good guess is:
extern off_t lseek(int, off_t, int);
HAS_MKDIR— defined = themkdirroutine is available to create directories. Otherwise you should fork off a new process to exec/bin/mkdir. 😭 the struggle was real back then.HAS_OFF64_T— this symbol gets defined if the C compiler supportsoff64_t. ✨HAS_OPENAT— defined if theopenat()routine is available.HAS_OPEN3— this manifest constant lets your C program know that the three argument form ofopen(2)is available. periodt.HAS_POLL— defined = thepollroutine is available topollactive file descriptors. Also please checkI_POLLandI_SYS_POLLso you know which header to include too. 👀HAS_READDIR— defined = thereaddirroutine is available to read directory entries. You may have to includedirent.h. SeeI_DIRENT.HAS_READDIR64_R— defined = thereaddir64_rroutine is available, so you can readdir64 re-entrantly. ✨HAS_REWINDDIR— defined = therewinddirroutine is available. You may have to includedirent.h. SeeI_DIRENT.HAS_RMDIR— defined = thermdirroutine is available to remove directories. Otherwise you should fork off a new process to exec/bin/rmdir. 💀HAS_SEEKDIR— defined = theseekdirroutine is available. You may have to includedirent.h. SeeI_DIRENT.HAS_SELECT— defined = theselectroutine is available toselectactive file descriptors. If the timeout field gets used,sys/time.hmay need to be included. 🌸HAS_SETVBUF— defined = thesetvbufroutine is available to change the buffering on an open stdio stream. to a line-buffered mode.HAS_STDIO_STREAM_ARRAY— defined = there's an array holding the stdio streams. it's giving behind-the-scenes infrastructure. 🎀HAS_STRUCT_FS_DATA— defined = thestruct fs_dataused to dostatfs()is supported.HAS_STRUCT_STATFS— defined = thestruct statfsused to dostatfs()is supported.HAS_STRUCT_STATFS_F_FLAGS— defined = thestruct statfsdoes have thef_flagsmember, which holds the mount flags of the filesystem containing the file. This kind ofstruct statfscomes fromsys/mount.h(BSD4.3), not fromsys/statfs.h(SYSV). OlderBSDs(like Ultrix) don't havestatfs()andstruct statfsat all — they haveustat()andgetmnt()withstruct ustatandstruct fs_data. history lesson, no notes. 📜HAS_TELLDIR— defined = thetelldirroutine is available. You may have to includedirent.h. SeeI_DIRENT.HAS_USTAT— defined = theustatsystem call is available to query file system statistics bydev_t.I_FCNTL— this manifest constant tells your C program to includefcntl.h. 💅
#ifdef I_FCNTL
#include <fcntl.h>
#endif
I_SYS_DIR— defined = your C program should includesys/dir.h. 🌈
#ifdef I_SYS_DIR
#include <sys_dir.h>
#endif
I_SYS_FILE— defined = your C program should includesys/file.hto get the definition ofR_OKand friends. 🫶
#ifdef I_SYS_FILE
#include <sys_file.h>
#endif
I_SYS_NDIR— defined = your C program should includesys/ndir.h.
#ifdef I_SYS_NDIR
#include <sys_ndir.h>
#endif
I_SYS_STATFS— defined =sys/statfs.hexists. that's it, that's the whole vibe.
#ifdef I_SYS_STATFS
#include <sys_statfs.h>
#endif
LSEEKSIZE— this symbol holds the number of bytes used by theOff_t. ✨RD_NODATA— this symbol holds the return code fromread()when there's no data present on the non-blocking file descriptor. Be careful! IfEOF_NONBLOCKis not defined, then you can't tell the difference between "no data" and "EOF" by just issuing aread(). You'll have to find another way to know for sure! 😭 do not get got by this one.READDIR64_R_PROTO— this symbol encodes the prototype ofreaddir64_r. It's zero ifd_readdir64_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros fromreentr.hifd_readdir64_ris defined. 🎀ST_INO_SIGN— this symbol holds the signedness ofstruct stat'sst_ino. 1 for unsigned, -1 for signed. yes, negative one, no typo. 👑ST_INO_SIZE— this variable contains the size ofstruct stat'sst_ino, measured in bytes.STDCHAR— this symbol is defined to be the type ofcharused instdio.h. It takes the valuesunsigned charorchar. pick your fighter. 💅STDIO_CNT_LVALUE— defined if theFILE_cntmacro can be used as an lvalue. 👀STDIO_PTR_LVAL_NOCHANGE_CNT— defined if using theFILE_ptrmacro as an lvalue to increase the pointer by n leavesFile_cnt(fp)unchanged. no side quests.STDIO_PTR_LVAL_SETS_CNT— defined if using theFILE_ptrmacro as an lvalue to increase the pointer by n has the side effect of decreasing the value ofFile_cnt(fp)by n. the chaos version. 💀STDIO_PTR_LVALUE— defined if theFILE_ptrmacro can be used as an lvalue.STDIO_STREAM_ARRAY— this symbol tells you the name of the array holding the stdio streams. Usual values include_iob,__iob, and__sF. 🌈VAL_EAGAIN— this symbol holds the errno error code thatread()sets when there was no data present on the non-blocking file descriptor.VAL_O_NONBLOCK— use this duringopen()orfcntl(F_SETFL)to turn on non-blocking I/O for the file descriptor. Note that there is no way back — i.e. you cannot turn it blocking again this way. If you want to switch back and forth between blocking and non-blocking, use theioctl(FIOSNBIO)call instead, but heads up: that is not supported by all devices. 🙈VOID_CLOSEDIR— defined = theclosedir()routine does not return a value.
🌸 Floating point 🌸
Also, List of capability HAS_foo symbols lists capabilities that aren't in this section. For example HAS_ASINH, for the hyperbolic sine function. 🦋 keeping everyone accounted for.
CASTFLAGS— this symbol contains flags that say what difficulties the compiler has when casting odd floating values tounsigned long:
0 = ok
1 = couldn't cast < 0
2 = couldn't cast >= 0x80000000
4 = couldn't cast in argument expression list
CASTNEGFLOAT 💅
okay so this one gets defined when your C compiler is chill enough to cast negative numbers into unsigned longs, ints and shorts. iconic behavior, not every compiler can.
DOUBLE_HAS_INF
defined = your double got that infinity in it, bestie.
DOUBLE_HAS_NAN
defined = your double has the not-a-number. yes the sassy one.
DOUBLE_HAS_NEGATIVE_ZERO
defined = your double has the "negative_zero". she's real and she's here.
DOUBLE_HAS_SUBNORMALS
defined = your double has the subnormals (aka denormals). the little guys.
DOUBLE_STYLE_CRAY
defined = your double is that 64-bit "CRAY" mainframe format. vintage queen.
DOUBLE_STYLE_IBM
defined = your double is the 64-bit "IBM" mainframe format.
DOUBLE_STYLE_IEEE
defined = your double is the 64-bit "IEEE" 754. the standard one, she ate.
DOUBLE_STYLE_VAX
defined = your double is the 64-bit "VAX" format D or G.
DOUBLEINFBYTES
defined = a comma-separated list of hexadecimal bytes for the double precision infinity.
DOUBLEKIND
"DOUBLEKIND" will be one of these, pick your fighter:
"DOUBLE_IS_IEEE_754_32_BIT_LITTLE_ENDIAN"
"DOUBLE_IS_IEEE_754_32_BIT_BIG_ENDIAN"
"DOUBLE_IS_IEEE_754_64_BIT_LITTLE_ENDIAN"
"DOUBLE_IS_IEEE_754_64_BIT_BIG_ENDIAN"
"DOUBLE_IS_IEEE_754_128_BIT_LITTLE_ENDIAN"
"DOUBLE_IS_IEEE_754_128_BIT_BIG_ENDIAN"
"DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_LE_BE"
"DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_BE_LE"
"DOUBLE_IS_VAX_F_FLOAT" "DOUBLE_IS_VAX_D_FLOAT"
"DOUBLE_IS_VAX_G_FLOAT" "DOUBLE_IS_IBM_SINGLE_32_BIT"
"DOUBLE_IS_IBM_DOUBLE_64_BIT" "DOUBLE_IS_CRAY_SINGLE_64_BIT"
"DOUBLE_IS_UNKNOWN_FORMAT"
DOUBLEMANTBITS
defined = tells you how many mantissa bits live in double precision floating point format. lowkey important note: this is usually DBL_MANT_DIG minus one, because with the standard "IEEE" 754 formats DBL_MANT_DIG includes the implicit bit, which doesn't really exist. the audacity.
DOUBLENANBYTES
defined = a comma-separated list of hexadecimal bytes (0xHH) for the double precision not-a-number.
DOUBLESIZE
this holds the size of a double, so the C preprocessor can make decisions based on it. she's the deciding vote fr.
Gconvert ✨
this preprocessor macro is defined to convert a floating point number to a string without a trailing decimal point. it emulates the behavior of sprintf("%g"), but sometimes it's way more efficient, no cap. if gconvert() isn't available, but gcvt() drops the trailing decimal point, then gcvt() is used instead. and if all else fails? a macro using sprintf("%g") steps in. the arguments for the Gconvert macro are: value, number of digits, whether trailing zeros should be retained, and the output buffer. the usual values are:
d_Gconvert='gconvert((x),(n),(t),(b))'
d_Gconvert='gcvt((x),(n),(b))'
d_Gconvert='sprintf((b),"%.*g",(n),(x))'
the last two assume trailing zeros should not be kept.
char * Gconvert(double x, Size_t n, bool t, char * b)
HAS_ATOLF
defined = the "atolf" routine is available to convert strings into long doubles. glow up for your strings.
HAS_CLASS
defined = the "class" routine is available to classify doubles. available for example in "AIX". the returned values are defined in float.h and are:
FP_PLUS_NORM Positive normalized, nonzero
FP_MINUS_NORM Negative normalized, nonzero
FP_PLUS_DENORM Positive denormalized, nonzero
FP_MINUS_DENORM Negative denormalized, nonzero
FP_PLUS_ZERO +0.0
FP_MINUS_ZERO -0.0
FP_PLUS_INF +INF
FP_MINUS_INF -INF
FP_NANS Signaling Not a Number (NaNS)
FP_NANQ Quiet Not a Number (NaNQ)
HAS_FINITE
defined = the "finite" routine is available to check whether a double is "finite" (aka non-infinity non-NaN). vibe check for your floats.
HAS_FINITEL
defined = the "finitel" routine is available to check whether a long double is finite (non-infinity non-NaN).
HAS_FP_CLASS
defined = the "fp_class" routine is available to classify doubles. available for example in Digital "UNIX". the returned values are defined in math.h and are:
FP_SNAN Signaling NaN (Not-a-Number)
FP_QNAN Quiet NaN (Not-a-Number)
FP_POS_INF +infinity
FP_NEG_INF -infinity
FP_POS_NORM Positive normalized
FP_NEG_NORM Negative normalized
FP_POS_DENORM Positive denormalized
FP_NEG_DENORM Negative denormalized
FP_POS_ZERO +0.0 (positive zero)
FP_NEG_ZERO -0.0 (negative zero)
HAS_FP_CLASSIFY
defined = the "fp_classify" routine is available to classify doubles. the values are defined in math.h:
FP_NORMAL Normalized
FP_ZERO Zero
FP_INFINITE Infinity
FP_SUBNORMAL Denormalized
FP_NAN NaN
HAS_FP_CLASSL
defined = the "fp_classl" routine is available to classify long doubles. available for example in Digital "UNIX". for possible values see "HAS_FP_CLASS". she's the long double version, periodt.
HAS_FPCLASS
defined = the "fpclass" routine is available to classify doubles. available for example in Solaris/"SVR4". the returned values are defined in ieeefp.h and are:
FP_SNAN signaling NaN
FP_QNAN quiet NaN
FP_NINF negative infinity
FP_PINF positive infinity
FP_NDENORM negative denormalized non-zero
FP_PDENORM positive denormalized non-zero
FP_NZERO negative zero
FP_PZERO positive zero
FP_NNORM negative normalized non-zero
FP_PNORM positive normalized non-zero
HAS_FPCLASSIFY
defined = the "fpclassify" routine is available to classify doubles. available for example in HP-UX. the returned values are defined in math.h and are:
FP_NORMAL Normalized
FP_ZERO Zero
FP_INFINITE Infinity
FP_SUBNORMAL Denormalized
FP_NAN NaN
HAS_FPCLASSL
defined = the "fpclassl" routine is available to classify long doubles. available for example in "IRIX". the returned values are defined in ieeefp.h and are:
FP_SNAN signaling NaN
FP_QNAN quiet NaN
FP_NINF negative infinity
FP_PINF positive infinity
FP_NDENORM negative denormalized non-zero
FP_PDENORM positive denormalized non-zero
FP_NZERO negative zero
FP_PZERO positive zero
FP_NNORM negative normalized non-zero
FP_PNORM positive normalized non-zero
HAS_FPGETROUND
defined = the "fpgetround" routine is available to get the floating point rounding mode.
HAS_FREXPL
defined = the "frexpl" routine is available to break a long double floating-point number into a normalized fraction and an integral power of 2. she splits it, respectfully.
HAS_ILOGB
defined = the "ilogb" routine is available to get the integer exponent of a floating-point value.
HAS_ISFINITE
defined = the "isfinite" routine is available to check whether a double is finite (non-infinity non-NaN).
HAS_ISFINITEL
defined = the "isfinitel" routine is available to check whether a long double is finite. (non-infinity non-NaN). yes with the extra period, that's the original's energy not mine 💀
HAS_ISINF
defined = the "isinf" routine is available to check whether a double is an infinity.
HAS_ISINFL
defined = the "isinfl" routine is available to check whether a long double is an infinity.
HAS_ISNAN
defined = the "isnan" routine is available to check whether a double is a NaN.
HAS_ISNANL
defined = the "isnanl" routine is available to check whether a long double is a NaN.
HAS_ISNORMAL
defined = the "isnormal" routine is available to check whether a double is normal (non-zero normalized).
HAS_J0
defined = the C program gets told that the j0() function is available for Bessel functions of the first kind of the order zero, for doubles.
HAS_J0L
defined = the C program gets told that the j0l() function is available for Bessel functions of the first kind of the order zero, for long doubles.
HAS_LDBL_DIG
defined = this system's float.h or limits.h defines the symbol "LDBL_DIG", which is the number of significant digits in a long double precision number. and unlike for "DBL_DIG", there's no good guess for "LDBL_DIG" if it's undefined. so like. don't assume, bestie.
HAS_LDEXPL
defined = the "ldexpl" routine is available to shift a long double floating-point number by an integral power of 2.
HAS_LLRINT
defined = the "llrint" routine is available to return the long long value closest to a double (according to the current rounding mode).
HAS_LLRINTL
defined = the "llrintl" routine is available to return the long long value closest to a long double (according to the current rounding mode).
HAS_LLROUNDL
defined = the "llroundl" routine is available to return the nearest long long value away from zero of the long double argument value.
HAS_LONG_DOUBLE
this one gets defined if the C compiler supports long doubles. slay.
HAS_LRINT
defined = the "lrint" routine is available to return the integral value closest to a double (according to the current rounding mode).
HAS_LRINTL— if this is defined, bestie, thelrintlroutine is there for you, returning the integral value closest to a long double (going by whatever rounding mode is currently active). 💅HAS_LROUNDL— defined meanslroundlexists to hand you the nearest integral value away from zero of the long double you pass in.HAS_MODFL— if defined,modflis available to split a long double x into a fractional part f and an integer part i, such that|f| < 1.0and(f + i) = x. math slay ✨HAS_NAN— defined means thenanroutine can generate NaN for you.HAS_NEXTTOWARD— defined meansnexttowardis available to return the next machine representable long double from x in direction y.HAS_REMAINDER— if defined,remainderis available to return the floating-pointremainder. no cap.HAS_SCALBN— defined meansscalbnis available to multiply a floating-point number by an integral power of the radix.HAS_SIGNBIT— if defined,signbitis available to check whether the given number has the sign bit set. This should include correct testing of-0.0. It only gets set if thesignbit()routine is safe to use with the NV type used internally in perl. Users should callPerl_signbit(), which will be#definedto the system'ssignbit()function or macro if this symbol is defined. 🫶HAS_SQRTL— defined meanssqrtlis available to do long double square roots.HAS_STRTOD_L— if defined,strtod_lis available to convert strings to long doubles.HAS_STRTOLD— if defined,strtoldis available to convert strings to long doubles.HAS_STRTOLD_L— if defined,strtold_lis available to convert strings to long doubles. (yes, another one, they're all cousins 😭)HAS_TRUNC— defined meanstruncis available to round doubles towards zero.HAS_UNORDERED— if defined,unorderedis available to check whether two doubles are "unordered" (which is effectively asking: is either of them NaN?).I_FENV— if defined, it's telling your C program to includefenv.hso it can get the floating point environment definitions. 👑
#ifdef I_FENV
#include <fenv.h>
#endif
I_QUADMATH— defined meansquadmath.hexists and should be included.
#ifdef I_QUADMATH
#include <quadmath.h>
#endif
LONG_DOUBLE_STYLE_IEEE— if defined, the long double is any of the "IEEE" 754 style long doubles:LONG_DOUBLE_STYLE_IEEE_STD,LONG_DOUBLE_STYLE_IEEE_EXTENDED,LONG_DOUBLE_STYLE_IEEE_DOUBLEDOUBLE.LONG_DOUBLE_STYLE_IEEE_DOUBLEDOUBLE— defined means the long double is the 128-bit double-double.LONG_DOUBLE_STYLE_IEEE_EXTENDED— defined means the long double is the 80-bit "IEEE" 754. Lowkey plot twist: despite the 'extended' in the name, this is less than the 'std', because it's an extension of the double precision.LONG_DOUBLE_STYLE_IEEE_STD— defined means the long double is the 128-bit "IEEE" 754.LONG_DOUBLE_STYLE_VAX— defined means the long double is the 128-bit "VAX" format H. vintage core 🧚LONG_DOUBLEKIND— this one will be one ofLONG_DOUBLE_IS_DOUBLE,LONG_DOUBLE_IS_IEEE_754_128_BIT_LITTLE_ENDIAN,LONG_DOUBLE_IS_IEEE_754_128_BIT_BIG_ENDIAN,LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN,LONG_DOUBLE_IS_X86_80_BIT_BIG_ENDIAN,LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_LE_LE,LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_BE,LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_LE_BE,LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_LE,LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_LITTLE_ENDIAN,LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BIG_ENDIAN,LONG_DOUBLE_IS_VAX_H_FLOAT,LONG_DOUBLE_IS_UNKNOWN_FORMAT. It's only defined if the system supports long doubles.LONG_DOUBLESIZE— holds the size of a long double, so the C preprocessor can make decisions based on it. Only defined if the system supports long doubles. Heads up: this issizeof(long double), which may include unused bytes. 🙈LONGDBLINFBYTES— if defined, it's a comma-separated list of hexadecimal bytes for the long double precision infinity.LONGDBLMANTBITS— if defined, it tells you how many mantissa bits there are in the long double precision floating point format. Note that this can beLDBL_MANT_DIGminus one, sinceLDBL_MANT_DIGcan include the "IEEE" 754 implicit bit. The common x86-style 80-bit long double does not have an implicit bit.LONGDBLNANBYTES— if defined, it's a comma-separated list of hexadecimal bytes (0xHH) for the long double precision not-a-number.NV— described in perlguts. go look bestie 💖NV_OVERFLOWS_INTEGERS_AT— gives the largest integer value that NVs can hold. This value + 1.0 cannot be stored accurately. It's expressed as a constant floating point expression to reduce the chance of decimal/binary conversion issues. If it can't be determined, the value 0 is given.NV_PRESERVES_UV— if defined, a variable of typeNVTYPEcan preserve all the bits of a variable of typeUVTYPE.NV_PRESERVES_UV_BITS— contains the number of bits a variable of typeNVTYPEcan preserve of a variable of typeUVTYPE.NV_ZERO_IS_ALLBITS_ZERO— defined means a variable of typeNVTYPEstores0.0in memory as all bits zero.NVMANTBITS— if defined, it tells you how many mantissa bits (not including the implicit bit) there are in a Perl NV. This depends on which floating point type was chosen.NVSIZE— contains thesizeof(NV). Real talk though: some floating point formats have unused bytes. The most iconic example is the x86* 80-bit extended precision, which comes in byte sizes of 12 and 16 (for 32 and 64 bit platforms, respectively), but only uses 10 bytes. Perl compiled with-Duselongdoubleon x86* is exactly like this. 💀NVTYPE— defines the C type used for Perl's NV.
✨ General Configuration ✨
This section is the catch-all, bestie — it holds configuration info that doesn't fit anywhere else in the more specialized sections of this document. At the end there's a list of #defines whose name should be enough to tell you what they do, plus a list of #defines that tell you whether you need to #include files to get the corresponding functionality. 🎀
ASCIIish— a preprocessor symbol that's defined iff the system is an ASCII platform; it would not be defined onEBCDICplatforms.
#ifdef ASCIIish
BYTEORDER— holds the hexadecimal constant defined in byteorder, in a UV, i.e.0x1234or0x4321or0x12345678, etc... If the compiler supports cross-compiling or multiple-architecture binaries, use compiler-defined macros to determine the byte order.CAT2— this macro concatenates 2 tokens together.
token CAT2(token x, token y)
CHARBITS— contains the size of achar, so the C preprocessor can make decisions based on it.DB_VERSION_MAJOR_CFG— if defined, it defines the major version number of Berkeley DB found in thedb.hheader when Perl was configured.DB_VERSION_MINOR_CFG— if defined, it defines the minor version number of Berkeley DB found in thedb.hheader when Perl was configured. For DB version 1 this is always 0.DB_VERSION_PATCH_CFG— if defined, it defines the patch version number of Berkeley DB found in thedb.hheader when Perl was configured. For DB version 1 this is always 0.DEFAULT_INC_EXCLUDES_DOT— if defined, this removes the legacy default behavior of including.at the end of@INC. bye bye legacy behavior 👋DLSYM_NEEDS_UNDERSCORE— if defined, it means we need to prepend an underscore to the symbol name before callingdlsym(). This only makes sense if you havedlsym, which we will presume is the case if you're usingdl_dlopen.xs.Drand01— this macro generates uniformly distributed random numbers over the range[0., 1.[. You may have to supply anextern double drand48();in your program, since SunOS 4.1.3 doesn't provide you with anything relevant in its headers. SeeHAS_DRAND48_PROTO.
double Drand01()
EBCDIC— if defined, this system usesEBCDICencoding.HAS_CSH— if defined, the C-shell exists. 🐚HAS_GETHOSTNAME— if defined, the C program may use thegethostname()routine to derive the host name. See alsoHAS_UNAMEandPHOSTNAME.HAS_GNULIBC— if defined, it tells the C program that theGNUC library is being used. A better check, though, is to use the__GLIBC__and__GLIBC_MINOR__symbols supplied with glibc.HAS_LGAMMA— if defined, thelgammaroutine is available to do the log gamma function. See alsoHAS_TGAMMAandHAS_LGAMMA_R.HAS_LGAMMA_R— if defined, thelgamma_rroutine is available to do the log gamma function without using the globalsigngamvariable. reentrant era 💅HAS_NON_INT_BITFIELDS— if defined, the C compiler accepts, without error or warning,struct bitfieldsthat are declared with sizes other than plainint; for exampleunsigned charis accepted.HAS_PRCTL_SET_NAME— if defined, theprctlroutine is available to set the process title and it supportsPR_SET_NAME. main character energy for your process ✨HAS_PROCSELFEXE— this symbol gets defined whenPROCSELFEXE_PATHis a symlink to the absolute pathname of the program that's currently executing. 👀HAS_PSEUDOFORK— if it's defined, bestie, there's an emulation of theforkroutine available. ✨HAS_REGCOMP— if defined, theregcomp()routine is available to do some regular pattern matching (usually onPOSIX.2conforming systems). slay.HAS_SETPGID— if defined, thesetpgid(pid, gpid)routine is available so you can set the process group ID. 💅HAS_SIGSETJMP— this variable tells the C program that thesigsetjmp()routine is available, so it can save the calling process's registers and stack environment for later use bysiglongjmp(), and optionally save the process's signal mask. SeeSigjmp_buf,Sigsetjmp, andSiglongjmp. 🌸HAS_STRUCT_CMSGHDR— if defined, thestruct cmsghdris supported. periodt.HAS_STRUCT_MSGHDR— if defined, thestruct msghdris supported. periodt again.HAS_TGAMMA— if defined, thetgammaroutine is available to do the gamma function. See alsoHAS_LGAMMA. 🧮HAS_UNAME— if defined, the C program may use theuname()routine to derive the host name. See alsoHAS_GETHOSTNAMEandPHOSTNAME.HAS_UNION_SEMUN— if defined, theunion semunis defined by including sys/sem.h. If it's not defined, the user code probably needs to define it itself, like this:
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
}
I_DIRENT— if defined, it's telling the C program to include dirent.h. Using this symbol also triggers the definition of theDirentry_tdefine, which ends up being"struct dirent"or"struct direct"depending on whether dirent.h is available. 🦋
#ifdef I_DIRENT
#include <dirent.h>
#endif
I_POLL— if defined, poll.h exists and should be included (see alsoHAS_POLL).
#ifdef I_POLL
#include <poll.h>
#endif
I_SYS_RESOURCE— if defined, it's telling the C program to include sys/resource.h.
#ifdef I_SYS_RESOURCE
#include <sys_resource.h>
#endif
LIBM_LIB_VERSION— if defined, libm exports_LIB_VERSIONand math.h defines the enum you'd use to manipulate it.NEED_VA_COPY— if defined, the system stores the variable argument list datatype,va_list, in a format that cannot be copied by simple assignment, so you have to use some other means whenever a copy is required. And because those systems vary in whether they provide (or don't provide) copying mechanisms, handy.h defines a platform-independent macro,Perl_va_copy(src, dst), to do the job. 🫶OSNAME— this symbol holds the name of the operating system, as determined by Configure. Don't lean on it too hard though; the specific feature tests from Configure are generally more reliable. 😌OSVERS— this symbol holds the version of the operating system, as determined by Configure. Same energy: don't lean on it too hard, the specific feature tests from Configure are generally more reliable.PERL_USE_GCC_BRACE_GROUPS— this C pre-processor value, if defined, indicates that it's permissible to use the GCC brace groups extension. However — and I cannot stress this enough — use of this extension is DISCOURAGED. Use astatic inlinefunction instead. 🚨
The extension, of the form
({ statement ... })
turns the block consisting of statement ... into an expression with a value, unlike plain C language blocks. This can present optimization possibilities, BUT, unless you know for sure that this will never be compiled without this extension being available and not forbidden, you need to specify an alternative. So then you've got two code paths to maintain, and they can get out-of-sync. All of these issues are solved by using a static inline function instead. 💖
Perl can be configured to not use this feature by passing the parameter -Accflags=-DPERL_GCC_BRACE_GROUPS_FORBIDDEN to Configure.
#ifdef PERL_USE_GCC_BRACE_GROUPS
PHOSTNAME— if defined, this indicates the command to feed to thepopen()routine to derive the host name. See alsoHAS_GETHOSTNAMEandHAS_UNAME. Note that the command uses a fully qualified path, so it's safe even if it's used by a process with super-user privileges. 👑PROCSELFEXE_PATH— ifHAS_PROCSELFEXEis defined, this symbol is the filename of the symbolic link pointing to the absolute pathname of the executing program.PTRSIZE— this symbol holds the size of a pointer, so that the C preprocessor can make decisions based on it. It will besizeof(void *)if the compiler supports(void *); otherwise it will besizeof(char *).RANDBITS— this symbol indicates how many bits are produced by the function used to generate normalized random numbers. Values include 15, 16, 31, and 48. 🎲seedDrand01— this symbol defines the macro to be used in seeding the random number generator (seeDrand01).
void seedDrand01(Rand_seed_t x)
SELECT_MIN_BITS— this symbol holds the minimum number of bits operated by select. Like, if you doselect(n, ...), how many bits at least will be cleared in the masks if some activity is detected. Usually this is either n or 32*ceil(n/32); especially many little-endians do the latter. This is only useful if you have select(), naturally. 🙃SETUID_SCRIPTS_ARE_SECURE_NOW— if defined, it means the bug that prevents setuid scripts from being secure is not present in this kernel. 🎉ST_DEV_SIGN— this symbol holds the signedness ofstruct stat'sst_dev. 1 for unsigned, -1 for signed.ST_DEV_SIZE— this variable contains the size ofstruct stat'sst_devin bytes.STRINGIFY— this macro surrounds its token with double quotes.
string STRINGIFY(token x)
✨ List of capability HAS_foo symbols ✨
Okay so this is the list of those symbols that don't appear elsewhere in this document and that indicate whether the current platform has a certain capability. Their names all begin with HAS_. Only the symbols whose capability is directly derived from the name are listed here — all the others have their meaning expanded out somewhere else in this document. The reason this list is (relatively) compact is that we think expanding it would add little or no value and take up a lot of space (because there are so many of them). If you think certain ones should be expanded, send email to perl5-porters@perl.org (<mailto:perl5-porters@perl.org>). 💌
Each symbol here will be #defined if and only if the platform has the capability. If you need more detail, see the corresponding entry in config.h. For convenience, the list is split so that the ones that indicate there is a reentrant version of a capability are listed separately. 🫡
HAS__FWALK, HAS_ACCEPT4, HAS_ACCESS, HAS_ACCESSX, HAS_ACOSH, HAS_AINTL, HAS_ALARM, HAS_ASINH, HAS_ATANH, HAS_ATOLL, HAS_CBRT, HAS_CHOWN, HAS_CHROOT, HAS_CHSIZE, HAS_CLEARENV, HAS_COPYSIGN, HAS_COPYSIGNL, HAS_CRYPT, HAS_CTERMID, HAS_CUSERID, HAS_DIRFD, HAS_DLADDR, HAS_DLERROR, HAS_EACCESS, HAS_ENDHOSTENT, HAS_ENDNETENT, HAS_ENDPROTOENT, HAS_ENDSERVENT, HAS_ERF, HAS_ERFC, HAS_EXPM1, HAS_EXP2, HAS_FCHMOD, HAS_FCHMODAT, HAS_FCHOWN, HAS_FD_SET, HAS_FDIM, HAS_FEGETROUND, HAS_FFS, HAS_FFSL, HAS_FGETPOS, HAS_FLOCK, HAS_FMA, HAS_FMAX, HAS_FMIN, HAS_FORK, HAS_FSEEKO, HAS_FSETPOS, HAS_FSYNC, HAS_FTELLO, HAS_GAI_STRERROR, HAS_GETADDRINFO, HAS_GETCWD, HAS_GETESPWNAM, HAS_GETGROUPS, HAS_GETHOSTBYADDR, HAS_GETHOSTBYNAME, HAS_GETHOSTENT, HAS_GETLOGIN, HAS_GETNAMEINFO, HAS_GETNETBYADDR, HAS_GETNETBYNAME, HAS_GETNETENT, HAS_GETPAGESIZE, HAS_GETPGID, HAS_GETPGRP, HAS_GETPGRP2, HAS_GETPPID, HAS_GETPRIORITY, HAS_GETPROTOBYNAME, HAS_GETPROTOBYNUMBER, HAS_GETPROTOENT, HAS_GETPRPWNAM, HAS_GETSERVBYNAME, HAS_GETSERVBYPORT, HAS_GETSERVENT, HAS_GETSPNAM, HAS_HTONL, HAS_HTONS, HAS_HYPOT, HAS_ILOGBL, HAS_INET_ATON, HAS_INETNTOP, HAS_INETPTON, HAS_IP_MREQ, HAS_IP_MREQ_SOURCE, HAS_IPV6_MREQ, HAS_IPV6_MREQ_SOURCE, HAS_ISASCII, HAS_ISBLANK, HAS_ISLESS, HAS_KILLPG, HAS_LCHOWN, HAS_LINK, HAS_LINKAT, HAS_LLROUND, HAS_LOCKF, HAS_LOGB, HAS_LOG1P, HAS_LOG2, HAS_LROUND, HAS_LSTAT, HAS_MADVISE, HAS_MBLEN, HAS_MBRLEN, HAS_MBRTOWC, HAS_MBSTOWCS, HAS_MBTOWC, HAS_MEMMEM, HAS_MEMRCHR, HAS_MKDTEMP, HAS_MKFIFO, HAS_MKOSTEMP, HAS_MKSTEMP, HAS_MKSTEMPS, HAS_MMAP, HAS_MPROTECT, HAS_MSG, HAS_MSYNC, HAS_MUNMAP, HAS_NEARBYINT, HAS_NEXTAFTER, HAS_NICE, HAS_NTOHL, HAS_NTOHS, HAS_PATHCONF, HAS_PAUSE, HAS_PHOSTNAME, HAS_PIPE, HAS_PIPE2, HAS_PRCTL, HAS_PTRDIFF_T, HAS_READLINK, HAS_READV, HAS_RECVMSG, HAS_REMQUO, HAS_RENAME, HAS_RENAMEAT, HAS_RINT, HAS_ROUND, HAS_SCALBNL, HAS_SEM, HAS_SENDMSG, HAS_SETEGID, HAS_SETENV, HAS_SETEUID, HAS_SETGROUPS, HAS_SETHOSTENT, HAS_SETLINEBUF, HAS_SETNETENT, HAS_SETPGRP, HAS_SETPGRP2, HAS_SETPRIORITY, HAS_SETPROCTITLE, HAS_SETPROTOENT, HAS_SETREGID, HAS_SETRESGID, HAS_SETRESUID, HAS_SETREUID, HAS_SETRGID, HAS_SETRUID, HAS_SETSERVENT, HAS_SETSID, HAS_SHM, HAS_SIGACTION, HAS_SIGPROCMASK, HAS_SIN6_SCOPE_ID, HAS_SNPRINTF, HAS_STAT, HAS_STRCOLL, HAS_STRERROR_L, HAS_STRLCAT, HAS_STRLCPY, HAS_STRNLEN, HAS_STRTOD, HAS_STRTOL, HAS_STRTOLL, HAS_STRTOQ, HAS_STRTOUL, HAS_STRTOULL, HAS_STRTOUQ, HAS_STRXFRM, HAS_STRXFRM_L, HAS_SYMLINK, HAS_SYS_ERRLIST, HAS_SYSCALL, HAS_SYSCONF, HAS_SYSTEM, HAS_TCGETPGRP, HAS_TCSETPGRP, HAS_TOWLOWER, HAS_TOWUPPER, HAS_TRUNCATE, HAS_TRUNCL, HAS_UALARM, HAS_UMASK, HAS_UNLINKAT, HAS_UNSETENV, HAS_VFORK, HAS_VSNPRINTF, HAS_WAITPID, HAS_WAIT4, HAS_WCRTOMB, HAS_WCSCMP, HAS_WCSTOMBS, HAS_WCSXFRM, HAS_WCTOMB, HAS_WRITEV
And, the reentrant capabilities:
"HAS_CRYPT_R", "HAS_CTERMID_R", "HAS_DRAND48_R", "HAS_ENDHOSTENT_R",
"HAS_ENDNETENT_R", "HAS_ENDPROTOENT_R", "HAS_ENDSERVENT_R",
"HAS_GETGRGID_R", "HAS_GETGRNAM_R", "HAS_GETHOSTBYADDR_R",
"HAS_GETHOSTBYNAME_R", "HAS_GETHOSTENT_R", "HAS_GETLOGIN_R",
"HAS_GETNETBYADDR_R", "HAS_GETNETBYNAME_R", "HAS_GETNETENT_R",
"HAS_GETPROTOBYNAME_R", "HAS_GETPROTOBYNUMBER_R",
"HAS_GETPROTOENT_R", "HAS_GETPWNAM_R", "HAS_GETPWUID_R",
"HAS_GETSERVBYNAME_R", "HAS_GETSERVBYPORT_R", "HAS_GETSERVENT_R",
"HAS_GETSPNAM_R", "HAS_RANDOM_R", "HAS_READDIR_R",
"HAS_SETHOSTENT_R", "HAS_SETNETENT_R", "HAS_SETPROTOENT_R",
"HAS_SETSERVENT_R", "HAS_SRANDOM_R", "HAS_SRAND48_R",
"HAS_STRERROR_R", "HAS_TMPNAM_R", "HAS_TTYNAME_R"
Example usage:
#ifdef HAS_STRNLEN
use strnlen()
#else
use an alternative implementation
#endif
✨ List of #include needed symbols ✨
Okay so this list? It's the do I even have this header file list. Each symbol tells you whether a certain #include file actually exists on the platform. So if your code is gonna reach for the functionality that one of these bad boys is for, you gotta #include it — but only if the matching symbol right here is #defined. More deets live in the matching entry inside config.h, bestie. 🫶
"I_ARPA_INET", "I_BFD", "I_CRYPT", "I_DBM", "I_DLFCN",
"I_EXECINFO", "I_FP", "I_FP_CLASS", "I_GDBM", "I_GDBM_NDBM",
"I_GDBMNDBM", "I_GRP", "I_IEEEFP", "I_INTTYPES", "I_LIBUTIL",
"I_MNTENT", "I_NDBM", "I_NET_ERRNO", "I_NETDB", "I_NETINET_IN",
"I_NETINET_TCP", "I_PROT", "I_PWD", "I_RPCSVC_DBM", "I_SGTTY",
"I_SHADOW", "I_STDBOOL", "I_STDINT", "I_SUNMATH", "I_SYS_ACCESS",
"I_SYS_IOCTL", "I_SYS_MOUNT", "I_SYS_PARAM", "I_SYS_POLL",
"I_SYS_SECURITY", "I_SYS_SELECT", "I_SYS_STAT", "I_SYS_STATVFS",
"I_SYS_SYSCALL", "I_SYS_TIME", "I_SYS_TIME_KERNEL", "I_SYS_TIMES",
"I_SYS_TYPES", "I_SYS_UN", "I_SYS_VFS", "I_SYS_WAIT", "I_SYSLOG",
"I_SYSMODE", "I_SYSUIO", "I_SYSUTSNAME", "I_TERMIO", "I_TERMIOS",
"I_UNISTD", "I_USTAT", "I_VFORK", "I_WCHAR", "I_WCTYPE"
Example usage:
#ifdef I_WCHAR
#include <wchar.h>
#endif
🌍 Global Variables 🌍
These ones are GLOBAL global — like, process-wide, main-character-level scope. They get shared between every interpreter and every thread in a process. And listen: any variable that is not documented right here may be changed or yeeted without notice, so don't use them!! 💀
If you genuinely feel like you need an unlisted variable, first send an email to perl5-porters@perl.org <mailto:perl5-porters@perl.org>. Maybe somebody over there will be like "bestie you can do that without touching an internal variable" and hand you a better way. But if not, you should get an official go-ahead to document it, and then use it. That's the vibe check. ✨
🎀 PL_check 🎀
An array, indexed by opcode, holding functions that get called for the "check" phase of optree building while Perl code is being compiled.
For most (but not all) types of op, once the op has been initially built and populated with child ops, it gets filtered through the check function that the appropriate element of this array points at. The new op comes in as the sole argument to the check function, and the check function returns the completed op.
And what can that check function do? Oh, literally anything:
- It may (as the name suggests, iykyk) check the op for validity and signal errors.
- It may also initialise or modify parts of the ops.
- Or it may do some full-on surgery — adding or removing child ops, or even throwing the op away entirely and returning a completely different op in its place. 💅
This array of function pointers is a super convenient place to hook into the compilation process. An XS module can put its own custom check function in place of any of the standard ones, to influence how a particular type of op gets compiled.
But — and this is important, bestie — a custom check function must never fully replace a standard check function (or even a custom check function from another module). A module that modifies checking has to wrap the preexisting check function instead. And a custom check function has to be selective about when it applies its custom behaviour. In the usual case where it decides not to do anything special with an op, it must chain the preexisting op function.
So check functions end up linked in a chain, with the core's base checker sitting at the very end. 🦋
For thread safety, modules should not write directly to this array. Instead, use the function wrap_op_checker.
🌈 PL_infix_plugin 🌈
NOTE: PL_infix_plugin is experimental and may change or be removed without notice.
NOTE: This API exists entirely for the purpose of making the CPAN moduleXS::Parse::Infixwork. It is not expected that additional modules will make use of it; rather, that they should useXS::Parse::Infixto provide parsing of new infix operators.
Function pointer, pointing at a function used to handle extended infix operators. The function should be declared as
int infix_plugin_function(pTHX_
char *opname, STRLEN oplen,
struct Perl_custom_infix **infix_ptr)
The function is called from the tokenizer whenever a possible infix operator is seen. opname points to the operator name in the parser's input buffer, and oplen gives the maximum number of bytes of it that should be consumed; it is not null-terminated. The function is expected to examine the operator name and possibly other state such as %^H, to determine whether it wants to handle the operator name. 👀
Now, compared to the single stage of PL_keyword_plugin, parsing additional infix operators happens in three separate stages. Why? Because it's got way more complex interactions with the parser, and they need operator precedence rules to work correctly. These stages are co-ordinated by the use of an additional information structure.
If the function wants to handle the infix operator, it must set the variable pointed to by infix_ptr to the address of a structure that provides this additional information about the subsequent parsing stages. If it does not want to handle it, it should make a call to the next function in the chain. 🔗
This structure has the following definition:
struct Perl_custom_infix {
enum Perl_custom_infix_precedence prec;
void (*parse)(pTHX_ SV **opdata,
struct Perl_custom_infix *);
OP *(*build_op)(pTHX_ SV **opdata, OP *lhs, OP *rhs,
struct Perl_custom_infix *);
};
The function must then return an integer giving the number of bytes consumed by the name of this operator. In the case of an operator whose name is composed of identifier characters, this must be equal to oplen. In the case of an operator named by non-identifier characters, this is permitted to be shorter than oplen, and any additional characters after it will not be claimed by the infix operator but instead will be consumed by the tokenizer and parser as normal. ⭐
If the optional parse function is provided, it is called immediately by the parser to let the operator's definition consume any additional syntax from the source code. This should not be used for normal operand parsing, but it may be useful when implementing things like parametric operators or meta-operators that consume more syntax themselves. This function may use the variable pointed to by opdata to provide an SV containing additional data to be passed into the build_op function later on.
The information structure gives the operator precedence level in the prec field. This is used to tell the parser how much of the surrounding syntax before and after should be considered as operands to the operator.
The tokenizer and parser will then continue to operate as normal until enough additional input has been parsed to form both the left- and right-hand side operands to the operator, according to the precedence level. At this point the build_op function is called, being passed the left- and right-hand operands as optree fragments. It is expected to combine them into the resulting optree fragment, which it should return. 🌸
After the build_op function has returned, if the variable pointed to by opdata was set to a non-NULL value, it will then be destroyed by calling SvREFCNT_dec().
For thread safety, modules should not set this variable directly. Instead, use the function wrap_infix_plugin.
However — after all that — the introductory note up top still applies. This variable is provided in core perl only for the benefit of the XS::Parse::Infix module. That module acts as a central registry for infix operators, automatically handling things like deparse support and discovery/reflection, and those abilities only work because it knows all the registered operators. Other modules should not use this interpreter variable directly to implement them, because then those central features would no longer work properly. 🙈
Furthermore, it is likely that this (experimental) API will be replaced in a future Perl version by a more complete API that fully implements the central registry and other semantics currently provided by XS::Parse::Infix, once the module has had sufficient experimental testing time. This current mechanism exists only as an interim measure to get to that stage. 🧚
👑 PL_keyword_plugin 👑
NOTE: PL_keyword_plugin is experimental and may change or be removed without notice.
Function pointer, pointing at a function used to handle extended keywords. The function should be declared as
int keyword_plugin_function(pTHX_
char *keyword_ptr, STRLEN keyword_len,
OP **op_ptr)
The function is called from the tokeniser whenever a possible keyword is seen. keyword_ptr points at the word in the parser's input buffer, and keyword_len gives its length; it is not null-terminated. The function is expected to examine the word, and possibly other state such as %^H, to decide whether it wants to handle it as an extended keyword.
If it does not want to handle it, the function should return KEYWORD_PLUGIN_DECLINE, and the normal parser process will just carry on like nothing happened. 🍓
If the function does want to handle the keyword, it first must parse anything following the keyword that is part of the syntax introduced by the keyword. See "Lexer interface" for details.
When a keyword is being handled, the plugin function has to build a tree of OP structures — that's the parsed code, bestie, the whole tree 🌳. The root goes into op_ptr. Then the function returns a constant that says what syntactic role the thing it just parsed plays: KEYWORD_PLUGIN_STMT if it's a complete statement, or KEYWORD_PLUGIN_EXPR if it's an expression. Real talk: a statement construct can't be used inside an expression (except via do BLOCK and similar), and an expression is *not a complete statement — it needs at least a terminating semicolon, periodt.
When a keyword is handled, the plugin function may also have (compile-time) side effects. It may modify %^H, define functions, and so on. Typically, if side effects are the main purpose of a handler, it does not wish to generate any ops to be included in the normal compilation. In this case it is still required to supply an op tree, but it suffices to generate a single null op. (iconic lazy-girl behaviour honestly 💅)
That's how the *PL_keyword_plugin function needs to behave overall. Conventionally, though, one does not completely replace the existing handler function. Instead, take a copy of PL_keyword_plugin before assigning your own function pointer to it. Your handler function should look for keywords that it is interested in and handle those. Where it is not interested, it should call the saved plugin function, passing on the arguments it received. Thus PL_keyword_plugin actually points at a chain of handler functions, all of which have an opportunity to handle keywords, and only the last function in the chain (built into the Perl core) will normally return KEYWORD_PLUGIN_DECLINE. it's giving relay race energy 🏃♀️✨
For thread safety, modules should not set this variable directly. Instead, use the function wrap_keyword_plugin. do NOT skip this one bestie, thread safety is not a vibe you want to gamble on 😭
PL_phase
A value that indicates the current Perl interpreter's phase. Possible values include PERL_PHASE_CONSTRUCT, PERL_PHASE_START, PERL_PHASE_CHECK, PERL_PHASE_INIT, PERL_PHASE_RUN, PERL_PHASE_END, and PERL_PHASE_DESTRUCT.
For example, the following determines whether the interpreter is in global destruction:
if (PL_phase == PERL_PHASE_DESTRUCT) {
// we are in global destruction
}
PL_phase was introduced in Perl 5.14; in prior perls you can use PL_dirty (boolean) to determine whether the interpreter is in global destruction. (Use of PL_dirty is discouraged since 5.14.) we do not use the old one anymore okay 🎀
enum perl_phase PL_phase
🦋 GV Handling and Stashes 🦋
A GV is a structure which corresponds to a Perl typeglob, i.e., *foo. It is a structure that holds a pointer to a scalar, an array, a hash etc, corresponding to $foo, @foo, %foo. one struct holding the whole squad 🫶
GVs are usually found as values in stashes (symbol table hashes) where Perl stores its global variables.
A stash is a hash that contains all variables that are defined within a package. See "Stashes and Globs" in perlguts
amagic_call
Perform the overloaded (active magic) operation given by method. method is one of the values found in overload.h.
flags affects how the operation is performed, as follows:
AMGf_noleft—leftis not to be used in this operation.AMGf_noright—rightis not to be used in this operation.AMGf_unary— The operation is done only on just one operand.AMGf_assign— The operation changes one of the operands, e.g.,$x += 1
SV * amagic_call( SV *left, SV *right, int method,
int dir)
SV * Perl_amagic_call(pTHX_ SV *left, SV *right, int method,
int dir)
amagic_deref_call
Perform method overloading dereferencing on ref, returning the dereferenced result. method must be one of the dereference operations given in overload.h.
If overloading is inactive on ref, returns ref itself. no drama, you just get your ref back ✨
SV * amagic_deref_call( SV *ref, int method)
SV * Perl_amagic_deref_call(pTHX_ SV *ref, int method)
gv_add_by_type
Make sure there is a slot of type type in the GV gv.
GV * gv_add_by_type( GV *gv, svtype type)
GV * Perl_gv_add_by_type(pTHX_ GV *gv, svtype type)
Gv_AMupdate
Recalculates overload magic in the package given by stash.
Returns:
1on success and there is some overload0if there is no overload-1if some error occurred and it couldn't croak (becausedestructingis true).
int Gv_AMupdate( HV *stash, bool destructing)
int Perl_Gv_AMupdate(pTHX_ HV *stash, bool destructing)
gv_autoload_pv, gv_autoload_pvn, gv_autoload_sv, gv_autoload4
These each search for an AUTOLOAD method, returning NULL if not found, or else returning a pointer to its GV, while setting the package $AUTOLOAD variable to the name (fully qualified). Also, if found and the GV's CV is an XSUB, the CV's PV will be set to the name, and its stash will be set to the stash of the GV. look at it doing four things at once, multitasking queen 👑
Searching is done in MRO order, as specified in "gv_fetchmeth", beginning with stash if it isn't NULL.
gv_autoload4 has a method parameter; the others a flags one (both types explained below). Otherwise, the forms differ only in how the name is specified.
In gv_autoload_pv, namepv is a C language NUL-terminated string.
In gv_autoload_pvn and gv_autoload4, name points to the first byte of the name, and an additional parameter, len, specifies its length in bytes. Hence, *name may contain embedded-NUL characters. embedded NULs, we love a chaotic string 😭
In gv_autoload_sv, *namesv is an SV, and the name is the PV extracted from that using "SvPV". If the SV is marked as being in UTF-8, the extracted PV will also be.
The other way to indicate that the name is encoded as UTF-8 is to set the SVf_UTF8 bit in flags for the forms that have that parameter. The name is never considered to be UTF-8 in gv_autoload4.
The method parameter in gv_autoload4 is used only to indicate that the name is for a method (non-zero), or not (zero). The other forms use the GV_AUTOLOAD_ISMETHOD bit in flags to indicate this.
The only other significant value in flags currently is GV_SUPER to indicate, if set, to skip searching for the name in stash.
GV * gv_autoload_pv ( NOCHECK HV *stash,
const char *namepv, U32 flags)
GV * Perl_gv_autoload_pv (pTHX_ NOCHECK HV *stash,
const char *namepv, U32 flags)
GV * gv_autoload_pvn( NOCHECK HV *stash,
const char *name, STRLEN len,
U32 flags)
GV * Perl_gv_autoload_pvn(pTHX_ NOCHECK HV *stash,
const char *name, STRLEN len,
U32 flags)
GV * gv_autoload_sv ( NOCHECK HV *stash, SV *namesv,
U32 flags)
GV * Perl_gv_autoload_sv (pTHX_ NOCHECK HV *stash, SV *namesv,
U32 flags)
GV * gv_autoload4 ( HV *stash, const char *name,
STRLEN len, I32 method
)
gv_AVadd, gv_HVadd, gv_IOadd, gv_SVadd
Make sure there is a slot of the given type (AV, HV, IO, SV) in the GV gv.
GV * gv_AVadd( GV *gv)
GV * Perl_gv_AVadd(pTHX_ GV *gv)
GV * gv_HVadd( GV *gv)
GV * Perl_gv_HVadd(pTHX_ GV *gv)
GV * gv_IOadd( GV *gv)
GV * Perl_gv_IOadd(pTHX_ GV *gv)
GV * gv_SVadd( GV *gv)
GV * Perl_gv_SVadd(pTHX_ GV *gv)
gv_const_sv
If gv is a typeglob whose subroutine entry is a constant sub eligible for inlining, or gv is a placeholder reference that would be promoted to such a typeglob, then returns the value returned by the sub. Otherwise, returns NULL. either you're a constant queen or you're NULL, no in between 💜
SV * gv_const_sv( GV *gv)
SV * Perl_gv_const_sv(pTHX_ GV *gv)
gv_efullname3, gv_efullname4, gv_fullname3, gv_fullname4
Place the full package name of gv into sv. The gv_e* forms return instead the effective package name (see "HvENAME").
If prefix is non-NULL, it is considered to be a C language NUL-terminated string, and the stored name will be prefaced with it.
The other difference between the functions is that the *4 forms have an extra parameter, keepmain. If true an initial main:: in the name is kept; if false it is stripped. With the *3 forms, it is always kept. main:: stays or main:: goes, pick your fighter 🌸
void gv_efullname3( SV *sv, const GV *gv,
const char *prefix)
void Perl_gv_efullname3(pTHX_ SV *sv, const GV *gv,
const char *prefix)
void gv_efullname4( SV *sv, const GV *gv,
const char *prefix, bool keepmain)
void Perl_gv_efullname4(pTHX_ SV *sv, const GV *gv,
const char *prefix, bool keepmain)
void gv_fullname3 ( SV *sv, const GV *gv,
const char *prefix)
void Perl_gv_fullname3 (pTHX_ SV *sv, const GV *gv,
const char *prefix)
void gv_fullname4 ( SV *sv, const GV *gv,
const char *prefix, bool keepmain)
void Perl_gv_fullname4 (pTHX_ SV *sv, const GV *gv,
const char *prefix, bool keepmain)
gv_fetchfile, gv_fetchfile_flags
These return the debugger glob for the file (compiled by Perl) whose name is given by the name parameter.
There are currently exactly two differences between these functions. two. exactly two. we counted 💅
The name parameter to gv_fetchfile is a C string, meaning it is NUL-terminated; whereas the name parameter to gv_fetchfile_flags is a Perl string, whose length (in bytes) is passed in via the namelen parameter. This means the name may contain embedded NUL characters. namelen doesn't exist in plain gv_fetchfile.
The other difference is that gv_fetchfile_flags has an extra flags parameter, which is currently completely ignored, but allows for possible future extensions. ignored for now, but it's there for the plot of a future season 🎀
GV * gv_fetchfile ( const char *name)
GV * Perl_gv_fetchfile (pTHX_ const char *name)
GV * gv_fetchfile_flags( const char * const name,
const STRLEN len,
const U32 flags)
GV * Perl_gv_fetchfile_flags(pTHX_ const char * const name,
const STRLEN len,
const U32 flags)
💖 gv_fetchmeth, gv_fetchmeth_autoload, gv_fetchmeth_pv, gv_fetchmeth_pv_autoload, gv_fetchmeth_pvn, gv_fetchmeth_pvn_autoload, gv_fetchmeth_sv, gv_fetchmeth_sv_autoload 💖
okay so these are all the same girl in different outfits. each one hunts for a glob named name that has a defined subroutine in it. found it? you get the GV of that glob. not found? you get NULL. that's it, that's the whole thing. 💅
bestie, you almost certainly want the gv_fetchmethod family instead. i'm just saying. fr fr.
the search always goes in this exact order, and some steps get skipped depending on stuff. first match wins, search over, no second-guessing:
stashgets searched first — unlessstashis eitherNULLorGV_SUPERis set inflags.- stashes you can reach through
@ISAget searched next. and the searching goes inMROorder. 🦋 UNIVERSAL::gets searched — unlessGV_NOUNIVERSALis set.- autoloaded subroutines get looked for last, but only for the forms whose names end in
_autoload, and only whenstashis notNULLandGV_SUPERis not set.
quick note: gv_fetchmeth_pv and gv_fetchmeth_pv_autoload don't have a flags parameter at all, so everywhere above just pretend flags is zero for those two. ✨
the level argument should be either 0 or -1. nothing else. no improvising.
- If
-1— no method caching happens. zero. none. no cap. - If
0—- if
GV_SUPERis not set inflags, the method that got found is cached instash. - if
GV_SUPERis set inflags, the method is cached in the super cache forstash. - if the method is not found, a negative cache entry</> gets added.
- heads up tho: subroutines found in
UNIVERSAL::are not cached. though this may change, apparently. 🙈
- if
the GV these hand back might be a method cache entry, which Perl code literally cannot see. so when you're calling call_sv, do not use the GV directly. use the method's CV instead, which you grab off the GV with the GvCV macro. and for an autoloaded subroutine with no stub, GvCV() of the result may be zero. 😭
the only other flags value that actually matters is SVf_UTF8, which says name should be treated as being encoded in UTF-8. and since plain gv_fetchmeth and gv_fetchmeth_autoload have no flags parameter, their name is never UTF-8.
other than that, these functions behave identically — except as noted below:
- in
gv_fetchmeth_pvandgv_fetchmeth_pv_autoload,nameis a C language NUL-terminated string. - in
gv_fetchmeth,gv_fetchmeth_pvn,gv_fetchmeth_autoload, andgv_fetchmeth_pvn_autoload,namepoints to the first byte of the name, and there's an extra parameter,len, that says how long it is in bytes. so yeah, the name can contain embedded-NUL characters. iconic honestly. - in
gv_fetchmeth_svandgv_fetchmeth_sv_autoload,*nameis an SV, and the name is the PV pulled out of it usingSvPV. if the SV is marked as being in UTF-8, the extracted PV will be too. and puttingSVf_UTF8inflagsforces the name to be treated as UTF-8 even if the SV isn't marked that way. 💅
GV * gv_fetchmeth ( NOCHECK HV *stash,
const char *name,
STRLEN len, I32 level)
GV * gv_fetchmeth_autoload ( NOCHECK HV *stash,
const char *name,
STRLEN len, I32 level)
GV * gv_fetchmeth_pv ( NOCHECK HV *stash,
const char *name,
I32 level, U32 flags)
GV * Perl_gv_fetchmeth_pv (pTHX_ NOCHECK HV *stash,
const char *name,
I32 level, U32 flags)
GV * gv_fetchmeth_pv_autoload ( NOCHECK HV *stash,
const char *name,
I32 level, U32 flags)
GV * Perl_gv_fetchmeth_pv_autoload (pTHX_ NOCHECK HV *stash,
const char *name,
I32 level, U32 flags)
GV * gv_fetchmeth_pvn ( NOCHECK HV *stash,
const char *name,
STRLEN len, I32 level,
U32 flags)
GV * Perl_gv_fetchmeth_pvn (pTHX_ NOCHECK HV *stash,
const char *name,
STRLEN len, I32 level,
U32 flags)
GV * gv_fetchmeth_pvn_autoload( NOCHECK HV *stash,
const char *name,
STRLEN len, I32 level,
U32 flags)
GV * Perl_gv_fetchmeth_pvn_autoload(pTHX_ NOCHECK HV *stash,
const char *name,
STRLEN len, I32 level,
U32 flags)
GV * gv_fetchmeth_sv ( NOCHECK HV *stash,
SV *namesv, I32 level,
U32 flags)
GV * Perl_gv_fetchmeth_sv (pTHX_ NOCHECK HV *stash,
SV *namesv, I32 level,
U32 flags)
GV * gv_fetchmeth_sv_autoload ( NOCHECK HV *stash,
SV *namesv, I32 level,
U32 flags)
GV * Perl_gv_fetchmeth_sv_autoload (pTHX_ NOCHECK HV *stash,
SV *namesv, I32 level,
U32 flags)
🌸 gv_fetchmethod & gv_fetchmethod_autoload 🌸
both of these return the glob holding the subroutine you'd call to invoke the method on the stash. and plot twist — when autoloading is in the picture, that might literally be the glob for AUTOLOAD. and in that case the matching $AUTOLOAD variable is already set up for you. slay. ✨
the third parameter of gv_fetchmethod_autoload decides whether AUTOLOAD lookup happens when the method you asked for isn't there:
- non-zero — yes bestie, go look for
AUTOLOAD. - zero — no, don't go looking for
AUTOLOAD.
and calling gv_fetchmethod is exactly the same as calling gv_fetchmethod_autoload with a non-zero autoload parameter. same thing, shorter name. 💅
these functions also grant the SUPER token as a prefix of the method name. and heads up: if you wanna hang onto the returned glob for a long time, you gotta check whether it's AUTOLOAD, because later on the call might load a different subroutine since $AUTOLOAD changes its value. use the glob created as a side effect to do that check. 🫶
these have the same side-effects as gv_fetchmeth with level==0. and that whole "don't pass the GV from gv_fetchmeth to call_sv" warning? applies here too. equally. no exceptions.
GV * gv_fetchmethod ( NOCHECK HV *stash,
const char *name)
GV * Perl_gv_fetchmethod (pTHX_ NOCHECK HV *stash,
const char *name)
GV * gv_fetchmethod_autoload( NOCHECK HV *stash,
const char *name,
I32 autoload)
GV * Perl_gv_fetchmethod_autoload(pTHX_ NOCHECK HV *stash,
const char *name,
I32 autoload)
🦋 gv_fetchpv, gv_fetchpvn, gv_fetchpvn_flags, gv_fetchpvs, gv_fetchsv, gv_fetchsv_nomg 🦋
all six of these return the GV of type sv_type whose name comes from the inputs, or NULL if no GV with that name and type could be found. go read Stashes and Globs in perlguts if you need the lore. 📚
the only differences between them are how the input name gets specified, and whether 'get' magic is normally used while getting that name.
and do not be fooled by the fact that only one of them has flags in its name. they all take a flags parameter, actually, and all the flag bits have the same meanings for all
ngl the original page literally trails off right there mid-sentence. that's how it's written, bestie. i'm not gonna invent the rest. 🙈
if any of these flags is set — GV_ADD, GV_ADDMG, GV_ADDWARN, GV_ADDMULTI, or GV_NOINIT — a GV gets created when none already exists for that input name and type. but GV_ADDMG only does the creating for magical GV's. and for all of those flags except GV_NOINIT, gv_init_pvn gets called after the addition. 💖
GV_ADDWARNis for when the caller expects adding won't be necessary because the symbol should already exist — but if it doesn't, add it anyway, plus a warning that it was unexpectedly absent. 😭GV_ADDMULTImeans pretend the GV has been seen before (i.e., suppressUsed oncewarnings).
the flag GV_NOADD_NOINIT makes it so gv_init_pvn is not called if the GV already existed but isn't PVGV.
if the SVf_UTF8 bit is set, the name is treated as being encoded in UTF-8. otherwise the name won't be considered UTF-8 in the pv-named forms, and in the sv forms the UTF-8ness of the underlying SVs is what gets used.
if the flag GV_NOTQUAL is set, the caller is warranting that the input name is a plain symbol name, not qualified with a package. otherwise the name gets checked for being a qualified one. 👑
and now the input-name tea:
- in
gv_fetchpv,nambegis a C string, NUL-terminated, with no intermediate NULs. - in
gv_fetchpvs,nameis a literal C string, hence it's enclosed in double quotes. gv_fetchpvnandgv_fetchpvn_flagsare identical. in these,<nambeg>is a Perl string whose byte length is given byfull_len, and it may contain embedded NULs. 🌈
In gv_fetchsv and gv_fetchsv_nomg, the name gets pulled out of the PV of the input name SV. The only difference between these two twins? 'get' magic normally happens on name in gv_fetchsv, and it's always skipped with gv_fetchsv_nomg. And if you toss GV_NO_SVGMAGIC into the flags param of gv_fetchsv, it behaves exactly like gv_fetchsv_nomg. Same energy, different name. ✨
GV * gv_fetchpv ( const char *nambeg, I32 flags,
const svtype sv_type)
GV * Perl_gv_fetchpv (pTHX_ const char *nambeg, I32 flags,
const svtype sv_type)
GV * gv_fetchpvn ( const char * nambeg,
STRLEN full_len, I32 flags,
const svtype sv_type)
GV * gv_fetchpvn_flags( const char *name, STRLEN len,
I32 flags,
const svtype sv_type)
GV * Perl_gv_fetchpvn_flags(pTHX_ const char *name, STRLEN len,
I32 flags,
const svtype sv_type)
GV * gv_fetchpvs ( "name", I32 flags,
const svtype sv_type)
GV * gv_fetchsv ( SV *name, I32 flags,
const svtype sv_type)
GV * Perl_gv_fetchsv (pTHX_ SV *name, I32 flags,
const svtype sv_type)
GV * gv_fetchsv_nomg ( SV *name, I32 flags,
const svtype sv_type)
gv_fullname3*
gv_fullname4*
Described under gv_efullname3. Go look there bestie, it's a whole thing. 🫶
gv_HVadd*
Described under gv_AVadd. Same vibes, different letter. 💅
gv_init
gv_init_pv
gv_init_pvn
gv_init_sv
Okay so these each convert a scalar into a typeglob. And it's an incoercible typeglob — meaning if you assign a reference to it, that reference lands in one of its slots instead of overwriting the whole thing like typeglobs made by SvSetSV do. Sneaky, right? 😭
Converting any scalar that is SvOK() may produce unpredictable results and is reserved for perl's internal use. Like, don't. Fr fr. 💀
gv is the scalar to be converted.
stash is the parent stash/package, if any.
In gv_init and gv_init_pvn, name and len give the name. The name must be unqualified — meaning it must not include the package name. And listen: if gv is a stash element, it's the caller's responsibility to make sure the name passed to this function matches the name of the element. If it doesn't match? perl's internal bookkeeping gets out of sync. Not iconic behaviour. name may contain embedded NUL characters though, that part's fine.
gv_init_pv is identical to gv_init_pvn, except it takes a NUL-terminated string for the name instead of separate char * and length parameters.
In gv_init_sv, the name is given by sv.
All but gv_init take a flags parameter. Set flags to include SVf_UTF8 if name is a UTF-8 string. In gv_init_sv, if SvUTF8(sv) is non-zero, name will be also be considered to be a UTF-8 string. It's unlikely to be a good idea to pass this particular flag to gv_init_sv, as that would potentially override the (presumaby known) state of sv. 🙈
flags can also take the GV_ADDMULTI flag, which means to pretend that the GV has been seen before (i.e., suppress Used once warnings). Gaslighting the compiler, honestly. 🎀
gv_init is the old form of gv_init_pvn. It does not work with UTF-8 strings, as it has no flags parameter. Setting the multi parameter to non-zero has the same effect as setting the GV_ADDMULTI flag in the other forms.
void gv_init ( GV *gv, HV *stash, const char *name,
STRLEN len, int multi)
void gv_init_pv ( GV *gv, HV *stash, const char *name,
U32 flags)
void Perl_gv_init_pv (pTHX_ GV *gv, HV *stash, const char *name,
U32 flags)
void gv_init_pvn( GV *gv, HV *stash, const char *name,
STRLEN len, U32 flags)
void Perl_gv_init_pvn(pTHX_ GV *gv, HV *stash, const char *name,
STRLEN len, U32 flags)
void gv_init_sv ( GV *gv, HV *stash, SV *namesv,
U32 flags)
void Perl_gv_init_sv (pTHX_ GV *gv, HV *stash, SV *namesv,
U32 flags)
gv_IOadd*
Described under gv_AVadd. You already know where to find it. 🦋
gv_name_set
Sets the name for GV gv to name, which is len bytes long. So yes, it may contain embedded NUL characters. NGL that's lowkey a flex. ✨
If flags contains SVf_UTF8, the name is treated as being encoded in UTF-8; otherwise not.
void gv_name_set( GV *gv, const char *name, U32 len,
U32 flags)
void Perl_gv_name_set(pTHX_ GV *gv, const char *name, U32 len,
U32 flags)
gv_stashpv
gv_stashpvn
gv_stashpvs
gv_stashsv
Quick PSA: gv_stashsv is strongly preferred for performance reasons. Take the hint bestie. 🫶
These each return a pointer to the stash for a specified package.
In gv_stashsv, the package is specified by sv.
In gv_stashpvs, the package is specified by the literal C string enclosed in double quotes.
In the other forms, name specifies the package. In gv_stashpvn, namelen gives the length of the name in bytes, so it may include embedded NUL characters. In gv_stashpv, name ends at the first NUL character. Periodt.
flags is passed to gv_fetchpvn_flags(), so if set to GV_ADD then the package will be created if it does not already exist. If the package does not exist and flags is 0 (or any other setting that does not create packages) then NULL is returned. Bye. 💀
Flags may be one of:
GV_ADD Create and initialize the package if doesn't
already exist
GV_NOADD_NOINIT Don't create the package,
GV_ADDMG GV_ADD iff the GV is magical
GV_NOINIT GV_ADD, but don't initialize
GV_NOEXPAND Don't expand SvOK() entries to PVGV
SVf_UTF8 The name is in UTF-8
The most important of which are probably GV_ADD and SVf_UTF8. 👑
HV * gv_stashpv ( const char *name, I32 flags)
HV * Perl_gv_stashpv (pTHX_ const char *name, I32 flags)
HV * gv_stashpvn( const char *name, U32 namelen,
I32 flags)
HV * Perl_gv_stashpvn(pTHX_ const char *name, U32 namelen,
I32 flags)
HV* gv_stashpvs( "name", I32 create)
HV * gv_stashsv ( SV *sv, I32 flags)
HV * Perl_gv_stashsv (pTHX_ SV *sv, I32 flags)
gv_SVadd*
Described under gv_AVadd. The gv_*add family stays together, as they should. 🌸
GvAV
Return the AV from the GV. That's it, that's the whole job. 🎀
AV* GvAV(GV* gv)
GvCV
Return the CV from the GV.
CV* GvCV(GV* gv)
GvHV
Return the HV from the GV.
HV* GvHV(GV* gv)
GvREFCNT_inc
GvREFCNT_inc_simple
GvREFCNT_inc_simple_NN
These all increment the reference count of the given SV, which must be a GV. They're useful when assigning the result into a typed pointer, because they avoid the need to cast the result to the appropriate type. No casting, no crying. 😌
GV * GvREFCNT_inc (GV *gv)
GV * GvREFCNT_inc_simple (GV *gv)
GV * GvREFCNT_inc_simple_NN(GV *gv)
GvSV
Return the SV from the GV.
Prior to Perl v5.9.3, this would add a scalar if none existed. Nowadays, use GvSVn for that, or compile perl with -DPERL_CREATE_GVSV. See perl5100delta. History lesson over. 📚
SV* GvSV(GV* gv)
GvSVn
Like GvSV, but creates an empty scalar if none already exists. The proactive bestie of the two. 💅
SV* GvSVn(GV* gv)
newGVgen
newGVgen_flags
Create a new, guaranteed to be unique, GV in the package given by the NUL-terminated C language string pack, and return a pointer to it. Guaranteed unique?? Main character energy. 👑
For newGVgen, or if flags in newGVgen_flags is 0, pack is to be considered to be encoded in Latin-1. The only other legal flags value is SVf_UTF8, which indicates pack is to be considered to be encoded in UTF-8.
GV * newGVgen ( const char *pack)
GV * newGVgen_flags( const char *pack, U32 flags)
GV * Perl_newGVgen_flags(pTHX_ const char *pack, U32 flags)
PL_curstash
The stash for the package code will be compiled into.
On threaded perls, each thread has an independent copy of this variable; each initialized at creation time with the current value of the creating thread's copy. Every thread gets its own little clone. 🧚
HV* PL_curstash
PL_defgv
The GV representing *_. Useful for access to $_. You know, that one. 🥰
On threaded perls, each thread has an independent copy of this variable; each initialized at creation time with the current value of the creating thread's copy.
GV * PL_defgv
PL_defoutgv
See setdefout. (It's literally a few lines down, keep scrolling bestie.) 🙈
PL_defstash
Described in perlguts. Different page, same lore. ✨
save_gp
Saves the current GP of gv on the save stack to be restored on scope exit.
If empty is true, replace the GP with a new GP.
If empty is false, mark gv with GVf_INTRO so the next reference assigned is localized, which is how local *foo = $someref; works. Cute, right? 🎀
void save_gp( GV *gv, I32 empty)
void Perl_save_gp(pTHX_ GV *gv, I32 empty)
setdefout
Sets PL_defoutgv, the default file handle for output, to the passed in typeglob. And because PL_defoutgv "owns" a reference on its typeglob, the reference count of the passed in typeglob is increased by one, and the reference count of the typeglob that PL_defoutgv points to is decreased by one. Balanced. Accounted for. Slay. ⚖️💖
void setdefout( GV *gv)
void Perl_setdefout(pTHX_ GV *gv)
🎀 Hook manipulation 🎀
okay so these functions are the ✨convenient✨ and thread-safe way to manipulate hook variables. we love a two-for-one 💖
rcpv_copy
refcount increment a shared memory refcounted string, and when the refcount hits 0 it gets freed using PerlMemShared_free().
It is the callers responsibility to make sure the pv is actually the result of a rcpv_new() call. like, do not just hand it a random string bestie 🙈
Returns the same pointer that was passed in. iconic behaviour, honestly.
new = rcpv_copy(pv);
char * rcpv_copy( char * const pv)
char * Perl_rcpv_copy(pTHX_ char * const pv)
rcpv_free
refcount decrement a shared memory refcounted string, and when the refcount hits 0 it gets freed using perlmemshared_free().
it is the callers responsibility to make sure the pv is the result of a rcpv_new() call. again. same rule, we're consistent here 💅
Always returns NULL, which is the whole point — so you can use it all clean like this:
thing = rcpv_free(thing);
char * rcpv_free( char * const pv)
char * Perl_rcpv_free(pTHX_ char * const pv)
rcpv_new
Create a new shared memory refcounted string with the requested size, with the requested initialization, and a refcount of 1. The actual space allocated is 1 byte more than requested, and rcpv_new() will make sure that extra byte is a null no matter what the flags settings say. ✨built-in safety✨
If the RCPVf_NO_COPY flag is set then the pv argument gets ignored, otherwise the contents of the pv pointer get copied into the new buffer — or, if it's NULL, the function does nothing and returns NULL.
If the RCPVf_USE_STRLEN flag is set then the len argument gets ignored and recomputed using strlen(pv). And listen: it is an error to combine RCPVf_USE_STRLEN and RCPVf_NO_COPY at the same time. do not do it, that's the rule 🚫
Under DEBUGGING, rcpv_new() will assert() if you ask it to create a 0 length shared string — unless the RCPVf_ALLOW_EMPTY flag is set.
The return value is suitable for passing into rcpv_copy() and rcpv_free(). To access the RCPV * from the returned value, use the RCPVx() macro. The 'len' member of the RCPV struct stores the allocated length (including the extra byte), but the RCPV_LEN() macro returns the requested length (not including the extra byte). sneaky little distinction, iykyk 🧚
Note thatrcpv_new()does NOT use a hash table or anything like that to dedupe inputs given the same text content. Each call with a non-nullpvparameter will produce a distinct pointer with its own refcount regardless of the input content. no sharing, no shortcuts, periodt.
char * rcpv_new( const char * const pv, STRLEN len,
U32 flags)
char * Perl_rcpv_new(pTHX_ const char * const pv, STRLEN len,
U32 flags)
wrap_op_checker
Puts a C function into the chain of check functions for a specified op type. This is the preferred way to manipulate the PL_check array. opcode says which type of op is getting affected. new_checker is a pointer to the C function that's being added to that opcode's check chain, and old_checker_p points to the storage location where a pointer to the next function in the chain will be stored. The value of new_checker gets written into the PL_check array, while the value previously stored there gets written to *old_checker_p.
PL_check is global to an entire process, and a module wishing to hook op checking may find itself invoked more than once per process — typically in different threads. To handle that, this function is idempotent. The location *old_checker_p must initially (once per process) contain a null pointer. A C variable of static duration (declared at file scope, typically also marked static to give it internal linkage) will be implicitly initialised appropriately, if it doesn't have an explicit initialiser. This function will only actually modify the check chain if it finds *old_checker_p to be null. It's also thread safe on the small scale — it uses appropriate locking to avoid race conditions when accessing PL_check. 🫶
When this function is called, the function referenced by new_checker must be ready to be called, except for *old_checker_p being unfilled. In a threading situation, new_checker may be called immediately, even before this function has returned. *old_checker_p will always be appropriately set before new_checker is called. If new_checker decides not to do anything special with an op that it's given (which is the usual case for most uses of op check hooking), it must chain the check function referenced by *old_checker_p.
Taken all together, XS code to hook an op checker should typically look something like this:
static Perl_check_t nxck_frob;
static OP *myck_frob(pTHX_ OP *op) {
...
op = nxck_frob(aTHX_ op);
...
return op;
}
BOOT:
wrap_op_checker(OP_FROB, myck_frob, &nxck_frob);
If you want to influence compilation of calls to a specific subroutine, then use cv_set_call_checker_flags rather than hooking checking of all entersub ops. that's the move 💅
void wrap_op_checker( Optype opcode,
Perl_check_t new_checker,
Perl_check_t *old_checker_p)
void Perl_wrap_op_checker(pTHX_ Optype opcode,
Perl_check_t new_checker,
Perl_check_t *old_checker_p)
✨ HV Handling ✨
A HV structure represents a Perl hash. It consists mainly of an array of pointers, each of which points to a linked list of HE structures. The array is indexed by the hash function of the key, so each linked list represents all the hash entries with the same hash value. Each HE contains a pointer to the actual value, plus a pointer to a HEK structure which holds the key and hash value. it's giving linked-list realness 🌸
get_hv
Returns the HV of the specified Perl hash. flags are passed to gv_fetchpv. If GV_ADD is set and the Perl variable does not exist then it will be created. If flags is zero (ignoring SVf_UTF8) and the variable does not exist then NULL is returned.
NOTE: the perl_get_hv() form is deprecated. 👀
HV * get_hv( const char *name, I32 flags)
HV * Perl_get_hv(pTHX_ const char *name, I32 flags)
HE
Described in perlguts. (go look bestie 📖)
HEf_SVKEY
This flag, used in the length slot of hash entries and magic structures, specifies the structure contains an SV* pointer where a char* pointer is to be expected. (For information only--not to be used).
HeHASH
Returns the computed hash stored in the hash entry.
U32 HeHASH(HE* he)
HeKEY
Returns the actual pointer stored in the key slot of the hash entry. The pointer may be either char* or SV*, depending on the value of HeKLEN(). Can be assigned to. The HePV() or HeSVKEY() macros are usually preferable for finding the value of a key.
void* HeKEY(HE* he)
HeKLEN
If this is negative, and amounts to HEf_SVKEY, it indicates the entry holds an SV* key. Otherwise, holds the actual length of the key. Can be assigned to. The HePV() macro is usually preferable for finding key lengths.
STRLEN HeKLEN(HE* he)
HePV
Returns the key slot of the hash entry as a char* value, doing any necessary dereferencing of possibly SV* keys. The length of the string is placed in len (this is a macro, so do not use &len). If you don't care about what the length of the key is, you may use the global variable PL_na, though this is rather less efficient than using a local variable. Remember though, that hash keys in perl are free to contain embedded nulls, so using strlen() or similar is not a good way to find the length of hash keys. This is very similar to the SvPV() macro described elsewhere in this document. See also HeUTF8.
If you're using HePV to get values to pass to newSVpvn() to create a new SV, you should consider using newSVhek(HeKEY_hek(he)) as it is more efficient. tiny optimization, big slay ⭐
char* HePV(HE* he, STRLEN len)
HeSVKEY
Returns the key as an SV*, or NULL if the hash entry does not contain an SV* key.
SV* HeSVKEY(HE* he)
HeSVKEY_force
Returns the key as an SV*. Will create and return a temporary mortal SV* if the hash entry contains only a char* key.
SV* HeSVKEY_force(HE* he)
HeSVKEY_set
Sets the key to a given SV*, taking care to set the appropriate flags to indicate the presence of an SV* key, and returns the same SV*.
SV* HeSVKEY_set(HE* he, SV* sv)
HeUTF8
Returns whether the char * value returned by HePV is encoded in UTF-8, doing any necessary dereferencing of possibly SV* keys. The value returned will be 0 or non-0, not necessarily 1 (or even a value with any low bits set), so do not blindly assign this to a bool variable, as bool may be a typedef for char. 🚨
U32 HeUTF8(HE* he)
HeVAL
Returns the value slot (type SV*) stored in the hash entry. Can be assigned to.
SV *foo= HeVAL(hv);
HeVAL(hv)= sv;
SV* HeVAL(HE* he)
HV
Described in perlguts.
hv_assert
Check that a hash is in an internally consistent state.
void Perl_hv_assert(pTHX_ HV *hv)
hv_bucket_ratio
NOTE: hv_bucket_ratio is experimental and may change or be removed without notice. 🙈
If the hash is tied, dispatches through to the SCALAR tied method. Otherwise, if the hash contains no keys it returns 0. Otherwise it returns a mortal sv containing a string specifying the number of used buckets, followed by a slash, followed by the number of available buckets.
This function is expensive — it must scan all of the buckets to determine which are used, and the count is NOT cached. In a large hash this could be a lot of buckets. 💀
SV * hv_bucket_ratio( HV *hv)
SV * Perl_hv_bucket_ratio(pTHX_ HV *hv)
hv_clear
Frees all the elements of a hash, leaving it empty. The XS equivalent of %hash = (). See also hv_undef.
See av_clear for a note about the hash possibly being invalid on return.
void hv_clear( HV *hv)
void Perl_hv_clear(pTHX_ HV *hv)
hv_clear_placeholders
ok bestie picture this 💭 — you've got a restricted hash, some of its keys are marked readonly, and then one of those keys gets deleted. but ngl, it does not actually get deleted. instead it gets assigned a value of &PL_sv_placeholder, which tags it so that future ops (like iterating over the hash, for example) just ignore it entirely 🙈 but the key can still totally have a value reassigned to it at some later point. this function is the one that clears all those placeholder keys out of the hash. want to see it in action? go look at Hash::Util::lock_keys() for an example of its use ✨
void hv_clear_placeholders( HV *hv)
void Perl_hv_clear_placeholders(pTHX_ HV *hv)
hv_copy_hints_hv
this is basically newHVhv but specialised, for copying %^H specifically 🎀 ohv has to be a pointer to a hash (it may have %^H magic, but should generally be non-magical), or it can be NULL (which gets interpreted as an empty hash). the content of ohv gets copied into a brand new hash, and that new hash gets the %^H-specific magic added to it. you get a pointer to the new hash back. slay.
HV * hv_copy_hints_hv( HV * const ohv)
HV * Perl_hv_copy_hints_hv(pTHX_ HV * const ohv)
hv_delete
hv_deletes
hv_delete_ent
all three of these delete a key/value pair in the hash 💅 the value's SV is removed from the hash, made mortal, and handed back to whoever called it. it's giving cleanup crew energy.
in hv_deletes, the key has to be a C language string literal, enclosed in double quotes. it is never treated as being in UTF-8. and there's no length_parameter.
in hv_delete, the absolute value of klen is the length of the key — so yes, the key may contain embedded NUL characters, no problem. if klen is negative, the key is assumed to be in UTF-8-encoded Unicode 🌈
in hv_delete_ent, the key is the PV in keysv, including its length and UTF8ness. hash can be a valid precomputed hash value, or 0 if you want it computed for you.
in all three of them, the flags value will normally be zero. but if you set it to G_DISCARD, then NULL gets returned. NULL also gets returned if the key isn't found — so like, keep an eye on that 👀
SV * hv_delete ( HV *hv, const char *key, I32 klen,
I32 flags)
SV * Perl_hv_delete (pTHX_ HV *hv, const char *key, I32 klen,
I32 flags)
SV * hv_deletes ( HV *hv, "key", U32 flags)
SV * hv_delete_ent( HV *hv, SV *keysv, I32 flags,
U32 hash)
SV * Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags,
U32 hash)
hv_exists
hv_existss
hv_exists_ent
these each return a boolean telling you whether the specified hash key exists or not. they literally only differ in how the key gets specified 🦋
in hv_existss, the key must be a C language string literal, enclosed in double quotes. it is never treated as being in UTF-8. there is no length_parameter.
in hv_exists, the absolute value of klen is the length of the key. if klen is negative, the key is assumed to be in UTF-8-encoded Unicode. and key may contain embedded NUL characters 💜
in hv_exists_ent, the key is specified by the SV keysv; its UTF8ness is the same as that SV. there's an extra parameter, hash, which can be a valid precomputed hash value, or 0 to ask for it to be computed.
bool hv_exists ( HV *hv, const char *key, I32 klen)
bool Perl_hv_exists (pTHX_ HV *hv, const char *key, I32 klen)
bool hv_existss ( HV *hv, "key")
bool hv_exists_ent( HV *hv, SV *keysv, U32 hash)
bool Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
hv_fetch
hv_fetchs
these each return the SV that corresponds to the specified key in the hash. again, they only differ in how the key is specified ✨
in hv_fetchs, the key must be a C language string literal, enclosed in double quotes. it is never treated as being in UTF-8. there is no length_parameter.
in hv_fetch, the absolute value of klen is the length of the key. if klen is negative, the key is assumed to be in UTF-8-encoded Unicode. key may contain embedded NUL characters 💅
in both of them, if lval is set, then the fetch will be part of a store. what that means: if there's no value in the hash associated with the given key, then one gets created and a pointer to it gets returned. the SV* it points to can be assigned to. but always check that the return value is non-null before dereferencing it to an SV* — i cannot stress this enough bestie 😭
for more info on how to use this function on tied hashes, see "Understanding the Magic of Tied Hashes and Arrays" in perlguts.
SV ** hv_fetch ( HV *hv, const char *key, I32 klen,
I32 lval)
SV ** Perl_hv_fetch (pTHX_ HV *hv, const char *key, I32 klen,
I32 lval)
SV** hv_fetchs( HV* hv, "key", I32 lval)
hv_fetch_ent
returns the hash entry corresponding to the specified key in the hash. hash must be a valid precomputed hash number for the given key, or 0 if you want the function to compute it for you. IF lval is set then the fetch will be part of a store. make sure the return value is non-null before accessing it, fr fr. also, when hv is a tied hash, the return value is a pointer to a static location — so be sure to make a copy of the structure if you need to store it somewhere, otherwise it's giving 💀
for more info on using this on tied hashes, see "Understanding the Magic of Tied Hashes and Arrays" in perlguts.
HE * hv_fetch_ent( HV *hv, SV *keysv, I32 lval,
U32 hash)
HE * Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval,
U32 hash)
hv_fetchs*
described up there under "hv_fetch" 🫶 go take a peek.
hv_iterinit
this sets up a starting point so you can traverse a hash table 🌸 it returns the number of keys in the hash, including placeholders (i.e. the same as HvTOTALKEYS(hv)). ngl the return value is currently only meaningful for hashes without tie magic.
NOTE: before version 5.004_65,hv_iterinitused to return the number of hash buckets that happen to be in use. if you still need that esoteric value, you can get it through the macroHvFILL(hv). iykyk.
I32 hv_iterinit( HV *hv)
I32 Perl_hv_iterinit(pTHX_ HV *hv)
hv_iterkey
returns the key from wherever the hash iterator is currently sitting. see "hv_iterinit" 👑
char * hv_iterkey( HE *entry, I32 *retlen)
char * Perl_hv_iterkey(pTHX_ HE *entry, I32 *retlen)
hv_iterkeysv
returns the key as an SV* from the iterator's current position. the return value will always be a mortal copy of the key, no exceptions 💖 also see "hv_iterinit".
SV * hv_iterkeysv( HE *entry)
SV * Perl_hv_iterkeysv(pTHX_ HE *entry)
hv_iternext
returns entries from a hash iterator. see "hv_iterinit" 🌈
here's a cute little detail: you may call hv_delete or hv_delete_ent on the hash entry that the iterator is currently pointing to, without losing your place or invalidating your iterator. iconic. BUT — note that in this case the current entry is deleted from the hash with your iterator holding the last reference to it. your iterator gets flagged to free the entry on the next call to hv_iternext, so you must not discard your iterator immediately, else the entry will leak 😱 call hv_iternext to trigger the resource deallocation.
HE * hv_iternext( HV *hv)
HE * Perl_hv_iternext(pTHX_ HV *hv)
hv_iternext_flags
NOTE: hv_iternext_flags is experimental and may change or be removed without notice. 💀
returns entries from a hash iterator. see "hv_iterinit" and "hv_iternext". the flags value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is set, then the placeholder keys (for restricted hashes) will be returned in addition to the normal keys. by default, placeholders get automatically skipped over 🙈 currently a placeholder is implemented with a value that is &PL_sv_placeholder. note that the implementation of placeholders and restricted hashes may change, and the implementation currently is insufficiently abstracted for any change to be tidy 😭
HE * hv_iternext_flags( HV *hv, I32 flags)
HE * Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
hv_iternextsv
does an hv_iternext, an hv_iterkey, and an hv_iterval all in one single operation. multitasking queen ✨
SV * hv_iternextsv( HV *hv, char **key, I32 *retlen)
SV * Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
hv_iterval
returns the value from wherever the hash iterator is currently sitting. see "hv_iterkey" 🎀
SV * hv_iterval( HV *hv, HE *entry)
SV * Perl_hv_iterval(pTHX_ HV *hv, HE *entry)
hv_ksplit
attempts to grow the hash hv so that it has at least newmax buckets available. perl picks the actual number for its own convenience, bestie, not yours 💅
this is literally the same as doing the following in Perl code:
keys %hv = newmax;
void hv_ksplit( HV *hv, IV newmax)
void Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
hv_magic
adds magic to a hash 🪄 see "sv_magic".
void hv_magic( HV *hv, GV *gv, int how)
void Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
hv_name_set
hv_name_sets
these each set the name of the stash hv to the specified name. they differ only in how that name is specified, as usual 🌸
in hv_name_sets, the name is a literal C string, enclosed in double quotes.
in hv_name_set, name points to the first byte of the name, and an additional parameter, len, specifies its length in bytes. so yes, the name may contain embedded-NUL characters 💜
if SVf_UTF8 is set in flags, the name is treated as being in UTF-8; otherwise it's not.
if HV_NAME_SETALL is set in flags, then both the name and the effective name get set. double the name, double the power 👑
void hv_name_set ( HV *hv, const char *name, U32 len,
U32 flags)
void Perl_hv_name_set (pTHX_ HV *hv, const char *name, U32 len,
U32 flags)
void hv_name_sets( HV *hv, "name", U32 flags)
hv_scalar
evaluates the hash in scalar context and returns the result ✨
when the hash is tied, it dispatches through to the SCALAR method. otherwise, it returns a mortal SV containing the number of keys in the hash. cute, right? 💖
hv_scalar 💅
ok so heads up bestie: before perl 5.25 this function returned what hv_bucket_ratio() returns now. so if you're reading old code and squinting, that's why. 👀
SV * hv_scalar( HV *hv)
SV * Perl_hv_scalar(pTHX_ HV *hv)
hv_store, hv_stores 🛒
these two both store an SV val with the specified key in the hash hv. they return NULL if the operation failed, or if the value didn't actually need to be stored in the hash (hi, tied hashes 💀). otherwise you can dereference the result to get the original SV*.
the only difference between them is how you spell the hash key.
- in
hv_stores, the key has to be a C language string literal, wrapped in double quotes. it is never treated as being in UTF-8. there is nolength_parameter. nope, not one. 🙅 - in
hv_store,keyis eitherNULLor it points to the first byte of the string specifying the key, and its length in bytes comes from the absolute value of an extra parameter,klen. aNULLkey means the key is treated asundef, andklengets ignored; otherwise the key string may contain embedded-NUL bytes. ifklenis negative, the string is treated as being encoded in UTF-8; otherwise it is not.
hv_store also has another extra parameter, hash — a precomputed hash of the key string, or zero if you haven't precomputed it. that parameter is omitted from hv_stores, because it gets computed automatically at compile time. slay. ✨
if hv is NULL, then NULL is returned and no action is taken.
if val is NULL, it's treated as being undef; otherwise you (the caller) are responsible for suitably incrementing the reference count of val before the call, and decrementing it if the function returned NULL. effectively, a successful hv_store takes ownership of one reference to val. this is usually what you want — a newly created SV has a reference count of one, so if all your code does is create SVs and store them in a hash, hv_store will own the only reference to the new SV, and your code doesn't need to do anything further to tidy up. iconic behaviour honestly. 💖
hv_store is not implemented as a call to "hv_store_ent", and it does not create a temporary SV for the key, so if your key data isn't already in SV form, then use hv_store in preference to hv_store_ent.
see "Understanding the Magic of Tied Hashes and Arrays" in perlguts for more info on how to use this function on tied hashes. 🪄
SV ** hv_store ( HV *hv, const char *key, I32 klen,
SV *val, U32 hash)
SV ** Perl_hv_store (pTHX_ HV *hv, const char *key, I32 klen,
SV *val, U32 hash)
SV ** hv_stores( HV *hv, "key", SV *val)
hv_store_ent 📥
stores val in a hash. the hash key is specified as key. the hash parameter is the precomputed hash value; if it's zero, then Perl will compute it. the return value is the new hash entry so created. it'll be NULL if the operation failed or if the value didn't need to be actually stored within the hash (again, tied hashes, we see you 👀). otherwise the contents of the return value can be accessed using the He? macros described here. note that the caller is responsible for suitably incrementing the reference count of val before the call, and decrementing it if the function returned NULL. effectively a successful hv_store_ent takes ownership of one reference to val. this is usually what you want; a newly created SV has a reference count of one, so if all your code does is create SVs and store them in a hash, hv_store will own the only reference to the new SV, and your code doesn't need to do anything further to tidy up.
now here's the plot twist: hv_store_ent only reads the key; unlike val it does not take ownership of it, so maintaining the correct reference count on key is entirely the caller's responsibility. the reason it doesn't take ownership is that key isn't used after this function returns, and so it can be freed immediately. 🫶
hv_store is not implemented as a call to hv_store_ent, and does not create a temporary SV for the key, so if your key data isn't already in SV form, then use hv_store in preference to hv_store_ent.
see "Understanding the Magic of Tied Hashes and Arrays" in perlguts for more information on how to use this function on tied hashes.
HE * hv_store_ent( HV *hv, SV *key, SV *val, U32 hash)
HE * Perl_hv_store_ent(pTHX_ HV *hv, SV *key, SV *val, U32 hash)
hv_stores* 🌟
described under "hv_store" (scroll up, bestie, it's right there 💜)
hv_undef 🗑️
undefines the hash. it's the XS equivalent of undef(%hash).
as well as freeing all the elements of the hash (like hv_clear()), this also frees any auxiliary data and storage associated with the hash.
see "av_clear" for a note about the hash possibly being invalid on return. ⚠️
void hv_undef(HV *hv)
HvENAME 🏷️
returns the effective name of a stash, or NULL if there isn't one. the effective name represents a location in the symbol table where this stash resides. it gets updated automatically when packages are aliased or deleted. a stash that's no longer in the symbol table has no effective name. this name is preferable to HvNAME for use in MRO linearisations and isa caches. ✨
char* HvENAME(HV* stash)
HvENAMELEN 📏
returns the length of the stash's effective name.
STRLEN HvENAMELEN(HV *stash)
HvENAMEUTF8 🌈
returns true if the effective name is in UTF-8 encoding.
unsigned char HvENAMEUTF8(HV *stash)
HvFILL 🪣
returns the number of hash buckets that happen to be in use.
as of perl 5.25 this function is used only for debugging purposes, and the number of used hash buckets is not in any way cached, so this function can be costly to execute — it has to iterate over all the buckets in the hash. 😭
STRLEN HvFILL(HV *const hv)
HvHasAUX 🔍
returns true if the HV has a struct xpvhv_aux extension. use this to check whether it's valid to call HvAUX().
bool HvHasAUX(HV *const hv)
HvNAME 🏷️
returns the package name of a stash, or NULL if stash isn't a stash. see "SvSTASH", "CvSTASH".
char* HvNAME(HV* stash)
HvNAMELEN 📐
returns the length of the stash's name.
Disfavored forms of HvNAME and HvNAMELEN; suppress mention of them
STRLEN HvNAMELEN(HV *stash)
HvNAMEUTF8 🌸
returns true if the name is in UTF-8 encoding.
unsigned char HvNAMEUTF8(HV *stash)
HvREFCNT_inc, HvREFCNT_inc_simple, HvREFCNT_inc_simple_NN ➕
these all increment the reference count of the given SV, which must be a HV. they're useful when assigning the result into a typed pointer, because they avoid the need to cast the result to the appropriate type. little quality-of-life win, we love to see it. 🫶
HV * HvREFCNT_inc (HV *hv)
HV * HvREFCNT_inc_simple (HV *hv)
HV * HvREFCNT_inc_simple_NN(HV *hv)
newHV 🎀
creates a new HV. the reference count is set to 1.
HV * newHV()
HV * Perl_newHV(pTHX)
newHVhv 🦋
the content of ohv is copied to a new hash. a pointer to the new hash is returned.
HV * newHVhv( HV *hv)
HV * Perl_newHVhv(pTHX_ HV *hv)
Nullhv 🚫
DEPRECATED!it is planned to removeNullhvfrom a future release of Perl. do not use it for new code; remove it from existing code. fr fr, retire it. 💀
Null HV pointer.
(deprecated - use (HV *)NULL instead)
PERL_HASH #️⃣
described in perlguts.
void PERL_HASH(U32 hash, char *key, STRLEN klen)
PL_modglobal 🌍
PL_modglobal is a general purpose, interpreter global HV for use by extensions that need to keep information on a per-interpreter basis. in a pinch, it can also be used as a symbol table for extensions to share data among each other. it's a good idea to use keys prefixed by the package name of the extension that owns the data. 👑
on threaded perls, each thread has an independent copy of this variable; each initialized at creation time with the current value of the creating thread's copy.
HV* PL_modglobal
💾 Input/Output 💾
do_close 🚪
close an I/O stream. this implements Perl "close" in perlfunc.
gv is the glob associated with the stream.
is_explict is true if this is an explicit close of the stream; false if it's part of another operation, such as closing a pipe (which involves implicitly closing both ends).
returns true if successful; otherwise returns false and sets errno to indicate the cause. 😤
bool do_close( GV *gv, bool is_explicit)
bool Perl_do_close(pTHX_ GV *gv, bool is_explicit)
IoDIRP, IOf_FLUSH, IOf_UNTAINT, IoFLAGS, IoIFP, IoOFP, IoTYPE 🌀
described in perlguts.
DIR * IoDIRP (IO *io)
U8 IoFLAGS(IO *io)
PerlIO * IoIFP (IO *io)
PerlIO * IoOFP (IO *io)
char IoTYPE (IO *io)
my_chsize 📏
the C library chsize(3) if available, or a Perl implementation of it.
I32 my_chsize( int fd, Off_t length)
I32 Perl_my_chsize(pTHX_ int fd, Off_t length)
my_dirfd 📁
the C library "dirfd(3)" if available, or a Perl implementation of it, or die if not easily emulatable. 💀
int my_dirfd(DIR *dir)
int Perl_my_dirfd(pTHX_ DIR *dir)
my_pclose 🔒
a wrapper for the C library pclose(3). don't use the latter, because the Perl version knows things that interact with the rest of the perl interpreter. trust the wrapper bestie. 🙏
I32 my_pclose( PerlIO *ptr)
I32 Perl_my_pclose(pTHX_ PerlIO *ptr)
my_popen 🔓
a wrapper for the C library popen(3). don't use the latter, because the Perl version knows things that interact with the rest of the perl interpreter.
PerlIO * my_popen( const char *cmd, const char *mode)
PerlIO * Perl_my_popen(pTHX_ const char *cmd, const char *mode)
newIO 🆕
create a new IO, setting the reference count to 1.
IO * newIO()
IO * Perl_newIO(pTHX)
PERL_FLUSHALL_FOR_CHILD 🚰
this defines a way to flush all output buffers. this may be a performance issue, so we allow people to disable it. also, if we're using stdio, there are broken implementations of fflush(NULL) out there — Solaris being the most prominent. 😬
void PERL_FLUSHALL_FOR_CHILD
okay bestie we're deep in PerlIO territory now, and here's the full roster of functions that live over in perlapio — every single one, no skips, we don't gatekeep here 💅✨
PerlIO_apply_layersPerlIO_binmodePerlIO_canset_cntPerlIO_clearerrPerlIO_closePerlIO_debugPerlIO_eofPerlIO_errorPerlIO_exportFILEPerlIO_fast_getsPerlIO_fdopenPerlIO_filenoPerlIO_fillPerlIO_findFILEPerlIO_flushPerlIO_get_basePerlIO_get_bufsizPerlIO_get_cntPerlIO_get_ptrPerlIO_getcPerlIO_getposPerlIO_has_basePerlIO_has_cntptrPerlIO_importFILEPerlIO_openPerlIO_printfPerlIO_putcPerlIO_putsPerlIO_readPerlIO_releaseFILEPerlIO_reopenPerlIO_rewindPerlIO_seekPerlIO_set_cntPerlIO_set_ptrcntPerlIO_setlinebufPerlIO_setposPerlIO_stderrPerlIO_stdinPerlIO_stdoutPerlIO_stdoutfPerlIO_tellPerlIO_ungetcPerlIO_unreadPerlIO_vprintfPerlIO_write
and literally all of these? Described in perlapio. 🫶
and here are their C signatures, typed out exactly so you can vibe with every single parameter 🔥:
int PerlIO_apply_layers(pTHX_ PerlIO *f,
const char *mode,
const char *layers)
int PerlIO_binmode (pTHX_ PerlIO *f, int ptype,
int imode,
const char *layers)
int PerlIO_canset_cnt ( PerlIO *f)
void PerlIO_clearerr ( PerlIO *f)
void Perl_PerlIO_clearerr (pTHX_ PerlIO *f)
int PerlIO_close ( PerlIO *f)
int Perl_PerlIO_close (pTHX_ PerlIO *f)
void PerlIO_debug ( const char *fmt, ...)
int PerlIO_eof ( PerlIO *f)
int Perl_PerlIO_eof (pTHX_ PerlIO *f)
int PerlIO_error ( PerlIO *f)
int Perl_PerlIO_error (pTHX_ PerlIO *f)
FILE * PerlIO_exportFILE ( PerlIO *f,
const char *mode)
int PerlIO_fast_gets ( PerlIO *f)
PerlIO * PerlIO_fdopen ( int fd,
const char *mode)
int PerlIO_fileno ( PerlIO *f)
int Perl_PerlIO_fileno (pTHX_ PerlIO *f)
int PerlIO_fill ( PerlIO *f)
int Perl_PerlIO_fill (pTHX_ PerlIO *f)
FILE * PerlIO_findFILE ( PerlIO *f)
int PerlIO_flush ( PerlIO *f)
int Perl_PerlIO_flush (pTHX_ PerlIO *f)
STDCHAR * PerlIO_get_base ( PerlIO *f)
STDCHAR * Perl_PerlIO_get_base (pTHX_ PerlIO *f)
SSize_t PerlIO_get_bufsiz ( PerlIO *f)
SSize_t Perl_PerlIO_get_bufsiz (pTHX_ PerlIO *f)
SSize_t PerlIO_get_cnt ( PerlIO *f)
SSize_t Perl_PerlIO_get_cnt (pTHX_ PerlIO *f)
STDCHAR * PerlIO_get_ptr ( PerlIO *f)
STDCHAR * Perl_PerlIO_get_ptr (pTHX_ PerlIO *f)
int PerlIO_getc ( PerlIO *d)
int PerlIO_getpos ( PerlIO *f, SV *save)
int PerlIO_has_base ( PerlIO *f)
int PerlIO_has_cntptr ( PerlIO *f)
PerlIO * PerlIO_importFILE ( FILE *stdio,
const char *mode)
PerlIO * PerlIO_open ( const char *path,
const char *mode)
int PerlIO_printf ( PerlIO *f,
const char *fmt, ...)
int PerlIO_putc ( PerlIO *f, int ch)
int PerlIO_puts ( PerlIO *f,
const char *string)
SSize_t PerlIO_read ( PerlIO *f, void *vbuf,
Size_t count)
SSize_t Perl_PerlIO_read (pTHX_ PerlIO *f, void *vbuf,
Size_t count)
void PerlIO_releaseFILE ( PerlIO *f, FILE *stdio)
PerlIO * PerlIO_reopen ( const char *path,
const char *mode,
PerlIO *old)
void PerlIO_rewind ( PerlIO *f)
int PerlIO_seek ( PerlIO *f,
Off_t offset,
int whence)
int Perl_PerlIO_seek (pTHX_ PerlIO *f,
Off_t offset,
int whence)
void PerlIO_set_cnt ( PerlIO *f, SSize_t cnt)
void Perl_PerlIO_set_cnt (pTHX_ PerlIO *f, SSize_t cnt)
void PerlIO_set_ptrcnt ( PerlIO *f,
STDCHAR *ptr,
SSize_t cnt)
void Perl_PerlIO_set_ptrcnt (pTHX_ PerlIO *f,
STDCHAR *ptr,
SSize_t cnt)
void PerlIO_setlinebuf ( PerlIO *f)
void Perl_PerlIO_setlinebuf (pTHX_ PerlIO *f)
int PerlIO_setpos ( PerlIO *f, SV *saved)
PerlIO * PerlIO_stderr ()
PerlIO * Perl_PerlIO_stderr (pTHX)
PerlIO * PerlIO_stdin ()
PerlIO * Perl_PerlIO_stdin (pTHX)
PerlIO * PerlIO_stdout ()
PerlIO * Perl_PerlIO_stdout (pTHX)
int PerlIO_stdoutf ( const char *fmt, ...)
Off_t PerlIO_tell ( PerlIO *f)
Off_t Perl_PerlIO_tell (pTHX_ PerlIO *f)
int PerlIO_ungetc ( PerlIO *f, int ch)
SSize_t PerlIO_unread ( PerlIO *f,
const void *vbuf,
Size_t count)
SSize_t Perl_PerlIO_unread (pTHX_ PerlIO *f,
const void *vbuf,
Size_t count)
int PerlIO_vprintf ( PerlIO *f,
const char *fmt,
va_list args)
SSize_t PerlIO_write ( PerlIO *f,
const void *vbuf,
Size_t count)
SSize_t Perl_PerlIO_write (pTHX_ PerlIO *f,
const void *vbuf,
Size_t count)
next up: the flag squad 🎀 these babies are all chillin' over in perliol:
PERLIO_F_APPENDPERLIO_F_CANREADPERLIO_F_CANWRITEPERLIO_F_CRLFPERLIO_F_EOFPERLIO_F_ERRORPERLIO_F_FASTGETSPERLIO_F_LINEBUFPERLIO_F_OPENPERLIO_F_RDBUFPERLIO_F_TEMPPERLIO_F_TRUNCATEPERLIO_F_UNBUFPERLIO_F_UTF8PERLIO_F_WRBUF
Described in perliol. 🦋
more perlapio residents, these ones rockin' a lil' asterisk ✨:
PerlIO_fast_gets*PerlIO_fdopen*PerlIO_fileno*PerlIO_fill*PerlIO_findFILE*PerlIO_flush*
Described in perlapio. 💖
PERLIO_FUNCS_CAST— casts the pointerfuncto be of typePerlIO_funcs *. slay. 💅
PerlIO_funcs * PERLIO_FUNCS_CAST(PerlIO * func)
PERLIO_FUNCS_DECL— declaresftabto be a PerlIO function table, aka of typePerlIO_funcs. 🎀
PERLIO_FUNCS_DECL(PerlIO * ftab)
PerlIO_get_base*PerlIO_get_bufsiz*PerlIO_get_cnt*PerlIO_get_ptr*PerlIO_getc*PerlIO_getpos*PerlIO_has_base*PerlIO_has_cntptr*PerlIO_importFILE*
Described in perlapio. 🌸
PERLIO_K_BUFFEREDPERLIO_K_CANCRLFPERLIO_K_FASTGETSPERLIO_K_MULTIARGPERLIO_K_RAW
Described in perliol. 🧚
PERLIO_NOT_STDIOPerlIO_open*PerlIO_printf*PerlIO_putc*PerlIO_puts*PerlIO_read*PerlIO_releaseFILE*PerlIO_reopen*PerlIO_rewind*PerlIO_seek*PerlIO_set_cnt*PerlIO_set_ptrcnt*PerlIO_setlinebuf*PerlIO_setpos*PerlIO_stderr*PerlIO_stdin*PerlIO_stdout*PerlIO_stdoutf*PerlIO_tell*PerlIO_ungetc*PerlIO_unread*PerlIO_vprintf*PerlIO_write*
Described in perlapio. 🌈
PL_maxsysfd
Described in perliol. 👑
repeatcpy— makescountcopies of thelenbytes starting atfrom, and drops them into memory starting atto, which has to be big enough to hold them all. do not skimp on that buffer, bestie 😭
void repeatcpy(char *to, const char *from, SSize_t len,
IV count)
void Perl_repeatcpy(char *to, const char *from, SSize_t len,
IV count)
SvVSTRING— if the given SV has vstring magic, it stores its length into the variablelen, and returns the string pointer. if it doesn't? it returnsNULL. periodt. 💅
this one's a wrapper around the sv_vstring_get function that conveniently takes the address of the len variable, in a form similar to the SvPV macro family. iykyk. ✨
const char * SvVSTRING(SV* sv, STRLEN len)
USE_STDIO
Described in perlapio. 🌈
#ifdef USE_STDIO
✨ Integer ✨
okay bestie, this whole cluster is about 🫶 integers 🫶 — all the little typedefs and magic symbols that tell you how big a number can get on whatever cursed machine you're compiling on. no cap, you're gonna want these.
CASTI32
this one gets defined when your C compiler can cast negative or large floating point numbers down to 32-bit ints. so it's giving "trust me, the cast will work" energy. ✨
HAS_INT64_T
defined when the C compiler supports int64_t. usually you gotta include inttypes.h for it, but sometimes sys/types.h alone is enough. slay.
HAS_LONG_LONG
defined when the C compiler supports long long. simple as that, periodt.
HAS_QUAD
if this is defined, congrats babe, there's a 64-bit integer type on your platform: Quad_t, plus its unsigned bestie Uquad_t. and QUADKIND will be exactly one of QUAD_IS_INT, QUAD_IS_LONG, QUAD_IS_LONG_LONG, QUAD_IS_INT64_T, or QUAD_IS___INT64. 🎀
INTMAX_C
hands you a token your C compiler recognizes for the constant number, using the widest integer type on the machine. so like, if the machine has long longs, INTMAX_C(-1) would yield:
-1LL
see also, for example, INT32_C. and use IV to declare variables of the maximum usable size on this platform. 👑
INTMAX_C(number)
INTSIZE
contains the value of sizeof(int), so the C preprocessor can make decisions based on it. very demure, very mindful.
INT16_C, INT32_C, INT64_C
each one returns a token the C compiler recognizes for the constant number of the corresponding integer type on the machine.
if the machine has no 64-bit type, INT64_C is undefined. use INTMAX_C to get the largest type available on the platform. 💅
I16 INT16_C(number)
I32 INT32_C(number)
I64 INT64_C(number)
IV, I8, I16, I32, I64
described in perlguts. (iykyk 🌸)
IV_MAX
the largest signed integer that fits in an IV on this platform. big number energy. 🔥
IV IV_MAX
IV_MIN
the negative signed integer furthest away from 0 that fits in an IV on this platform.
⚠️ bestie listen — it is easy to get undefined C behavior with this value. the macros (currently only available for internal use)NEGATE_2UVin perlintern,ABS_IV_MINin perlintern, andNEGATE_2IVin perlintern avoid undefined behavior when finding the opposite signed equivalent value. use those, don't rawdog it. 😭
IV IV_MIN
IVSIZE
contains the sizeof(IV). that's it, that's the tweet.
IVTYPE
defines the C type used for Perl's IV.
I8*
described in perlguts.
I8SIZE
contains the sizeof(I8).
I8TYPE
defines the C type used for Perl's I8.
I16*
described in perlguts.
I16SIZE
contains the sizeof(I16).
I16TYPE
defines the C type used for Perl's I16.
I32*
described in perlguts.
I32df
defines the format string used for printing a Perl I32 as a signed decimal integer. iconic. ✨
I32SIZE
contains the sizeof(I32).
I32TYPE
defines the C type used for Perl's I32.
I64*
described in perlguts.
I64SIZE
contains the sizeof(I64).
I64TYPE
defines the C type used for Perl's I64.
line_t
the typedef to use when you're declaring variables that are gonna hold line numbers. 📏
LONGLONGSIZE
contains the size of a long long, so the C preprocessor can make decisions based on it. only defined if the system supports long long. no long long, no symbol, sorry babe. 🙈
LONGSIZE
contains the value of sizeof(long), so the C preprocessor can make decisions based on it.
memzero
sets the l bytes starting at d to all zeroes. the way that it just... zeroes your memory. 🫶
void memzero(void * d, Size_t l)
PERL_INT_FAST8_T, PERL_INT_FAST16_T, PERL_UINT_FAST8_T, PERL_UINT_FAST16_T
these are equivalent to the correspondingly-named C99 typedefs on platforms that have those. and on platforms that don't, they evaluate to int and unsigned int instead — so you can portably take advantage of this C99 feature no matter where you land. slay, portability queen. 👑
PERL_INT_MAX, PERL_INT_MIN, PERL_LONG_MAX, PERL_LONG_MIN, PERL_QUAD_MAX, PERL_QUAD_MIN, PERL_SHORT_MAX, PERL_SHORT_MIN, PERL_UCHAR_MAX, PERL_UCHAR_MIN, PERL_UINT_MAX, PERL_UINT_MIN, PERL_ULONG_MAX, PERL_ULONG_MIN, PERL_UQUAD_MAX, PERL_UQUAD_MIN, PERL_USHORT_MAX, PERL_USHORT_MIN
these give you the largest and smallest number representable on the current platform in variables of the corresponding types. the whole glow-up squad. 🌈
for signed types, the smallest representable number is the most negative number — the one furthest away from zero.
for C99 and later compilers, these correspond to things like INT_MAX, which are available to the C code. but these constants, furnished by Perl, let code compiled on earlier compilers portably have access to the same constants. so it's giving "backwards compatibility but make it fashion". 💅
int PERL_INT_MAX
int PERL_INT_MIN
long PERL_LONG_MAX
long PERL_LONG_MIN
IV PERL_QUAD_MAX
IV PERL_QUAD_MIN
short PERL_SHORT_MAX
short PERL_SHORT_MIN
U8 PERL_UCHAR_MAX
U8 PERL_UCHAR_MIN
unsigned int PERL_UINT_MAX
unsigned int PERL_UINT_MIN
unsigned long PERL_ULONG_MAX
unsigned long PERL_ULONG_MIN
UV PERL_UQUAD_MAX
UV PERL_UQUAD_MIN
unsigned short PERL_USHORT_MAX
unsigned short PERL_USHORT_MIN
PERL_UINT_FAST8_T*, PERL_UINT_FAST16_T*
described under PERL_INT_FAST8_T. go read that one, bestie. 🦋
SHORTSIZE
contains the value of sizeof(short), so the C preprocessor can make decisions based on it.
UINTMAX_C
hands you a token your C compiler recognizes for the constant number, using the widest unsigned integer type on the machine. so if the machine has longs, UINTMAX_C(1) would yield:
1UL
see also, for example, UINT32_C. and use UV to declare variables of the maximum usable size on this platform. 🎀
UINTMAX_C(number)
UINT16_C, UINT32_C, UINT64_C
each one returns a token the C compiler recognizes for the constant number of the corresponding unsigned integer type on the machine.
if the machine has no 64-bit type, UINT64_C is undefined. use UINTMAX_C to get the largest type available on the platform. 🔥
U16 UINT16_C(number)
U32 UINT32_C(number)
U64 UINT64_C(number)
UV, U8, U16, U32, U64
described in perlguts.
UV_MAX
the largest unsigned integer that fits in a UV on this platform. ✨
UV UV_MAX
UV_MIN
the smallest unsigned integer that fits in a UV on this platform. it should equal zero. (and like... of course it should, it's unsigned, but the docs said it so we're saying it. 💜)
UV UV_MIN
UVSIZE
contains the sizeof(UV).
UVTYPE
defines the C type used for Perl's UV.
U8*
described in perlguts.
U8SIZE
contains the sizeof(U8).
U8TYPE
defines the C type used for Perl's U8.
U16*
described in perlguts.
U16SIZE
contains the sizeof(U16).
U16TYPE
defines the C type used for Perl's U16.
U32*
described in perlguts.
U32of
defines the format string used for printing a Perl U32 as an unsigned octal integer. 🐙
U32SIZE
contains the sizeof(U32).
U32TYPE
defines the C type used for Perl's U32.
U32uf
defines the format string used for printing a Perl U32 as an unsigned decimal integer.
U32Xf
defines the format string used for printing a Perl U32 as an unsigned hexadecimal integer in uppercase ABCDEF. loud and proud. 📣
U32xf
defines the format string used for printing a Perl U32 as an unsigned hexadecimal integer in lowercase abcdef. quiet and cozy. 🌸
U64*
described in perlguts.
U64SIZE
contains the sizeof(U64).
U64TYPE
defines the C type used for Perl's U64.
WIDEST_UTYPE
yields the widest unsigned integer type on the platform — currently either U32 or U64. main character energy, obviously. 👑
use it in declarations like:
WIDEST_UTYPE my_uv;
or in casts:
my_uv = (WIDEST_UTYPE) val;
and that's the integer buffet, bestie. take what you need, the macros are all labeled. 🫶
✨ I/O Formats ✨
These are used for formatting the corresponding type. For example, instead of saying
Perl_newSVpvf(pTHX_ "Create an SV with a %d in it\n", iv);
use
Perl_newSVpvf(pTHX_ "Create an SV with a " IVdf " in it\n", iv);
This keeps you from having to know if, say an IV, needs to be printed as %d, %ld, or something else. 💅
"HvNAMEf","HvNAMEf_QUOTEDPREFIX"— Described in perlguts."IVdf"— This symbol defines the format string used for printing a Perl IV as a signed decimal integer."NVef"— This symbol defines the format string used for printing a Perl NV using%e-ish floating point format."NVff"— This symbol defines the format string used for printing a Perl NV using%f-ish floating point format."NVgf"— This symbol defines the format string used for printing a Perl NV using%g-ish floating point format."PERL_PRIeldbl"— This symbol, if defined, contains the string used by stdio to format long doubles (format'e') for output."PERL_PRIfldbl"— This symbol, if defined, contains the string used by stdio to format long doubles (format'f') for output."PERL_PRIgldbl"— This symbol, if defined, contains the string used by stdio to format long doubles (format'g') for output."PERL_SCNfldbl"— This symbol, if defined, contains the string used by stdio to format long doubles (format'f') for input."PRINTF_FORMAT_NULL_OK"— Allows"__printf__"format to be null when checking printf-style."SVf","SVf_QUOTEDPREFIX","SVfARG","UTF8f","UTF8f_QUOTEDPREFIX","UTF8fARG"— Described in perlguts.
SVfARG (SV *sv)
UTF8fARG(bool is_utf8, Size_t byte_len, char *str)
"UVf"—"DEPRECATED!"It is planned to remove"UVf"from a future release of Perl. Do not use it for new code; remove it from existing code.
Obsolete form of "UVuf", which you should convert to instead use
const char * UVf
"UVof"— This symbol defines the format string used for printing a Perl UV as an unsigned octal integer."UVuf"— This symbol defines the format string used for printing a Perl UV as an unsigned decimal integer."UVXf"— This symbol defines the format string used for printing a Perl UV as an unsigned hexadecimal integer in uppercase"ABCDEF"."UVxf"— This symbol defines the format string used for printing a Perl UV as an unsigned hexadecimal integer in lowercase abcdef.
🦋 Lexer interface 🦋
This is the lower layer of the Perl parser, managing characters and tokens. ✨
"BHK"— Described in perlguts."lex_bufutf8"— NOTE:"lex_bufutf8"is experimental and may change or be removed without notice.
Indicates whether the octets in the lexer buffer ("PL_parser->linestr") should be interpreted as the UTF-8 encoding of Unicode characters. If not, they should be interpreted as Latin-1 characters. This is analogous to the "SvUTF8" flag for scalars.
In UTF-8 mode, it is not guaranteed that the lexer buffer actually contains valid UTF-8. Lexing code must be robust in the face of invalid encoding.
The actual "SvUTF8" flag of the "PL_parser->linestr" scalar is significant, but not the whole story regarding the input character encoding. Normally, when a file is being read, the scalar contains octets and its "SvUTF8" flag is off, but the octets should be interpreted as UTF-8 if the "use utf8" pragma is in effect. During a string eval, however, the scalar may have the "SvUTF8" flag on, and in this case its octets should be interpreted as UTF-8 unless the "use bytes" pragma is in effect. This logic may change in the future; use this function instead of implementing the logic yourself.
bool lex_bufutf8()
bool Perl_lex_bufutf8(pTHX)
"lex_discard_to"— NOTE:"lex_discard_to"is experimental and may change or be removed without notice.
Discards the first part of the "PL_parser->linestr" buffer, up to "ptr". The remaining content of the buffer will be moved, and all pointers into the buffer updated appropriately. "ptr" must not be later in the buffer than the position of "PL_parser->bufptr": it is not permitted to discard text that has yet to be lexed.
Normally it is not necessarily to do this directly, because it suffices to use the implicit discarding behaviour of "lex_next_chunk" and things based on it. However, if a token stretches across multiple lines, and the lexing code has kept multiple lines of text in the buffer for that purpose, then after completion of the token it would be wise to explicitly discard the now-unneeded earlier lines, to avoid future multi-line tokens growing the buffer without bound.
void lex_discard_to( char *ptr)
void Perl_lex_discard_to(pTHX_ char *ptr)
"lex_grow_linestr"— NOTE:"lex_grow_linestr"is experimental and may change or be removed without notice.
Reallocates the lexer buffer ("PL_parser->linestr") to accommodate at least "len" octets (including terminating "NUL"). Returns a pointer to the reallocated buffer. This is necessary before making any direct modification of the buffer that would increase its length. "lex_stuff_pvn" provides a more convenient way to insert text into the buffer.
Do not use "SvGROW" or "sv_grow" directly on "PL_parser->linestr"; this function updates all of the lexer's variables that point directly into the buffer.
char * lex_grow_linestr( STRLEN len)
char * Perl_lex_grow_linestr(pTHX_ STRLEN len)
"lex_next_chunk"— NOTE:"lex_next_chunk"is experimental and may change or be removed without notice.
Reads in the next chunk of text to be lexed, appending it to "PL_parser->linestr". This should be called when lexing code has looked to the end of the current chunk and wants to know more. It is usual, but not necessary, for lexing to have consumed the entirety of the current chunk at this time.
If "PL_parser->bufptr" is pointing to the very end of the current chunk (i.e., the current chunk has been entirely consumed), normally the current chunk will be discarded at the same time that the new chunk is read in. If "flags" has the "LEX_KEEP_PREVIOUS" bit set, the current chunk will not be discarded. If the current chunk has not been entirely consumed, then it will not be discarded regardless of the flag.
Returns true if some new text was added to the buffer, or false if the buffer has reached the end of the input text.
bool lex_next_chunk( U32 flags)
bool Perl_lex_next_chunk(pTHX_ U32 flags)
"lex_peek_unichar"— NOTE:"lex_peek_unichar"is experimental and may change or be removed without notice.
Looks ahead one (Unicode) character in the text currently being lexed. Returns the codepoint (unsigned integer value) of the next character, or -1 if lexing has reached the end of the input text. To consume the peeked character, use "lex_read_unichar".
If the next character is in (or extends into) the next chunk of input text, the next chunk will be read in. Normally the current chunk will be discarded at the same time, but if "flags" has the "LEX_KEEP_PREVIOUS" bit set, then the current chunk will not be discarded.
If the input is being interpreted as UTF-8 and a UTF-8 encoding error is encountered, an exception is generated.
I32 lex_peek_unichar( U32 flags)
I32 Perl_lex_peek_unichar(pTHX_ U32 flags)
"lex_read_space"— NOTE:"lex_read_space"is experimental and may change or be removed without notice.
Reads optional spaces, in Perl style, in the text currently being lexed. The spaces may include ordinary whitespace characters and Perl-style comments. "#line" directives are processed if encountered. "PL_parser->bufptr" is moved past the spaces, so that it points at a non-space character (or the end of the input text).
If spaces extend into the next chunk of input text, the next chunk will be read in. Normally the current chunk will be discarded at the same time, but if "flags" has the "LEX_KEEP_PREVIOUS" bit set, then the current chunk will not be discarded.
void lex_read_space( U32 flags)
void Perl_lex_read_space(pTHX_ U32 flags)
"lex_read_to"— NOTE:"lex_read_to"is experimental and may change or be removed without notice.
Consume text in the lexer buffer, from "PL_parser->bufptr" up to "ptr". This advances "PL_parser->bufptr" to match "ptr", performing the correct bookkeeping whenever a newline character is passed. This is the normal way to consume lexed text.
Interpretation of the buffer's octets can be abstracted out by using the slightly higher-level functions "lex_peek_unichar" and "lex_read_unichar".
void lex_read_to( char *ptr)
void Perl_lex_read_to(pTHX_ char *ptr)
"lex_read_unichar"— NOTE:"lex_read_unichar"is experimental and may change or be removed without notice.
Reads the next (Unicode) character in the text currently being lexed. Returns the codepoint (unsigned integer value) of the character read, and moves "PL_parser->bufptr" past the character, or returns -1 if lexing has reached the end of the input text. To non-destructively examine the next character, use "lex_peek_unichar" instead.
If the next character is in (or extends into) the next chunk of input text, the next chunk will be read in. Normally the current chunk will be discarded at the same time, but if "flags" has the "LEX_KEEP_PREVIOUS" bit set, then the current chunk will not be discarded.
If the input is being interpreted as UTF-8 and a UTF-8 encoding error is encountered, an exception is generated.
I32 lex_read_unichar( U32 flags)
I32 Perl_lex_read_unichar(pTHX_ U32 flags)
"lex_start"— NOTE:"lex_start"is experimental and may change or be removed without notice.- The code to be parsed comes from
lineandrsfp. Soline, if it's non-null, hands over a string (in SV form) holding the code to parse. And bestie, a copy of the string gets made, so if you go messing withlineafterwards, parsing does not care, it's unbothered, it's moved on 💅. rsfp, if non-null, gives you an input stream that the code gets read from so it can be parsed. And if both are non-null? Then the code inlinecomes first and it must consist of complete lines of input, andrsfpsupplies the remainder of the source. Teamwork fr fr 🫶.- The
flagsparameter is reserved for future use. Currently it is only used by perl internally, so extensions should always pass zero. Not optional, not vibes-based, just zero ✨.
void lex_start( SV *line, PerlIO *rsfp, U32 flags)
void Perl_lex_start(pTHX_ SV *line, PerlIO *rsfp, U32 flags)
✨ lex_stuff_pv / lex_stuff_pvn / lex_stuff_pvs / lex_stuff_sv ✨
🚨 NOTE: all these forms are experimental and may change or be removed without notice. So like, don't build your whole personality on them, okay? 😭
These each insert characters into the lexer buffer (PL_parser->linestr), immediately after the current lexing point (PL_parser->bufptr), reallocating the buffer if necessary. Which means lexing code running later will see those characters as if they had appeared in the input. It is not recommended to do this as part of normal parsing, and most uses of this facility run the risk of the inserted characters being interpreted in an unintended manner. It's giving "I did a little trolling" and the trolling is at your own expense 💀.
In lex_stuff_pvs, the string to be inserted is a literal C string, enclosed in double quotes. Cute, simple, iconic 🎀.
In lex_stuff_pv and lex_stuff_pvn, the string to be inserted is represented by the octets starting at pv. In lex_stuff_pv, the first NUL octet terminates the string. In lex_stuff_pvn, len octets will be used, hence the string may contain embedded NUL characters. NGL that's the whole point of the n version, she contains multitudes 🌈.
In all three cases, these octets are interpreted as either UTF-8 or Latin-1, according to whether or not the LEX_STUFF_UTF8 flag is set in flags. Vibe check happens via that flag, no cap.
In lex_stuff_sv, the string to be inserted is the string value of sv. And flags must be 0. The string is interpreted as either UTF-8 or Latin-1, according to whether or not sv has its UTF-8 flag set. So sv tells you which way it swings 💜.
In all three forms, the characters are recoded for the lexer buffer, according to how the buffer is currently being interpreted (lex_bufutf8). Recoding arc, we love a transformation ✨.
void lex_stuff_pv ( const char *pv, U32 flags)
void Perl_lex_stuff_pv (pTHX_ const char *pv, U32 flags)
void lex_stuff_pvn( const char *pv, STRLEN len,
U32 flags)
void Perl_lex_stuff_pvn(pTHX_ const char *pv, STRLEN len,
U32 flags)
void lex_stuff_pvs( "pv", U32 flags)
void lex_stuff_sv ( SV *sv, U32 flags)
void Perl_lex_stuff_sv (pTHX_ SV *sv, U32 flags)
🌸 lex_unstuff 🌸
🚨 NOTE: lex_unstuff is experimental and may change or be removed without notice. Same disclaimer, same energy, stay safe out there bestie.
Discards text about to be lexed, from PL_parser->bufptr up to ptr. Text following ptr will be moved, and the buffer shortened. This hides the discarded text from any lexing code that runs later, as if the text had never appeared. It's giving "this never happened" and honestly? Respect 🙈.
This is not the normal way to consume lexed text. For that, use lex_read_to. Don't be weird about it, there's a whole dedicated function for that 💅.
void lex_unstuff( char *ptr)
void Perl_lex_unstuff(pTHX_ char *ptr)
✨ parse_arithexpr ✨
🚨 NOTE: parse_arithexpr is experimental and may change or be removed without notice.
Parse a Perl arithmetic expression. This may contain operators of precedence down to the bit shift operators. The expression must be followed (and thus terminated) either by a comparison or lower-precedence operator or by something that would normally terminate an expression such as semicolon. If flags has the PARSE_OPTIONAL bit set, then the expression is optional, otherwise it is mandatory. It is up to the caller to ensure that the dynamic parser state (PL_parser et al) is correctly set to reflect the source of the code to be parsed and the lexical context for the expression. That part is on you, not on me, no takebacks 🫶.
The op tree representing the expression is returned. If an optional expression is absent, a null pointer is returned, otherwise the pointer will be non-null. Math but make it a pointer 🧚.
If an error occurs in parsing or compilation, in most cases a valid op tree is returned anyway. The error is reflected in the parser state, normally resulting in a single exception at the top level of parsing which covers all the compilation errors that occurred. Some compilation errors, however, will throw an exception immediately. So like, sometimes it throws hands immediately, sometimes it saves it all for the finale 😭.
OP * parse_arithexpr( U32 flags)
OP * Perl_parse_arithexpr(pTHX_ U32 flags)
🌈 parse_barestmt 🌈
🚨 NOTE: parse_barestmt is experimental and may change or be removed without notice.
Parse a single unadorned Perl statement. This may be a normal imperative statement or a declaration that has compile-time effect. It does not include any label or other affixture. It is up to the caller to ensure that the dynamic parser state (PL_parser et al) is correctly set to reflect the source of the code to be parsed and the lexical context for the statement. Bare minimum, literally, that's the name 🌸.
The op tree representing the statement is returned. This may be a null pointer if the statement is null, for example if it was actually a subroutine definition (which has compile-time side effects). If not null, it will be ops directly implementing the statement, suitable to pass to newSTATEOP. It will not normally include a nextstate or equivalent op (except for those embedded in a scope contained entirely within the statement). Okay so the nuance here is real, read it twice bestie 💜.
If an error occurs in parsing or compilation, in most cases a valid op tree (most likely null) is returned anyway. The error is reflected in the parser state, normally resulting in a single exception at the top level of parsing which covers all the compilation errors that occurred. Some compilation errors, however, will throw an exception immediately.
The flags parameter is reserved for future use, and must always be zero. Zero. Zilch. Nada. Periodt 💅.
OP * parse_barestmt( U32 flags)
OP * Perl_parse_barestmt(pTHX_ U32 flags)
🎀 parse_block 🎀
🚨 NOTE: parse_block is experimental and may change or be removed without notice.
Parse a single complete Perl code block. This consists of an opening brace, a sequence of statements, and a closing brace. The block constitutes a lexical scope, so my variables and various compile-time effects can be contained within it. It is up to the caller to ensure that the dynamic parser state (PL_parser et al) is correctly set to reflect the source of the code to be parsed and the lexical context for the statement. Curly braces are the moment, we stan a scope 👑.
The op tree representing the code block is returned. This is always a real op, never a null pointer. It will normally be a lineseq list, including nextstate or equivalent ops. No ops to construct any kind of runtime scope are included by virtue of it being a block. So like, the block is a block, not automatically a runtime scope, don't get it twisted 🦋.
If an error occurs in parsing or compilation, in most cases a valid op tree (most likely null) is returned anyway. The error is reflected in the parser state, normally resulting in a single exception at the top level of parsing which covers all the compilation errors that occurred. Some compilation errors, however, will throw an exception immediately.
The flags parameter is reserved for future use, and must always be zero.
OP * parse_block( U32 flags)
OP * Perl_parse_block(pTHX_ U32 flags)
✨ parse_fullexpr ✨
🚨 NOTE: parse_fullexpr is experimental and may change or be removed without notice.
Parse a single complete Perl expression. This allows the full expression grammar, including the lowest-precedence operators such as or. The expression must be followed (and thus terminated) by a token that an expression would normally be terminated by: end-of-file, closing bracketing punctuation, semicolon, or one of the keywords that signals a postfix expression-statement modifier. If flags has the PARSE_OPTIONAL bit set, then the expression is optional, otherwise it is mandatory. It is up to the caller to ensure that the dynamic parser state (PL_parser et al) is correctly set to reflect the source of the code to be parsed and the lexical context for the expression. The full grammar, no cuts, main character energy 🔥.
The op tree representing the expression is returned. If an optional expression is absent, a null pointer is returned, otherwise the pointer will be non-null.
If an error occurs in parsing or compilation, in most cases a valid op tree is returned anyway. The error is reflected in the parser state, normally resulting in a single exception at the top level of parsing which covers all the compilation errors that occurred. Some compilation errors, however, will throw an exception immediately.
OP * parse_fullexpr( U32 flags)
OP * Perl_parse_fullexpr(pTHX_ U32 flags)
💖 parse_fullstmt 💖
🚨 NOTE: parse_fullstmt is experimental and may change or be removed without notice.
Parse a single complete Perl statement. This may be a normal imperative statement or a declaration that has compile-time effect, and may include optional labels. It is up to the caller to ensure that the dynamic parser state (PL_parser et al) is correctly set to reflect the source of the code to be parsed and the lexical context for the statement. Full statement, full send, labels included 🌟.
The op tree representing the statement is returned. This may be a null pointer if the statement is null, for example if it was actually a subroutine definition (which has compile-time side effects). If not null, it will be the result of a newSTATEOP call, normally including a nextstate or equivalent op.
Okay so if an error happens during parsing or compilation, in most cases you still get a valid op tree back anyway (and honestly, it's most likely null). The error gets reflected in the parser state instead, which normally means one single exception at the top level of parsing that covers all the compilation errors that went down. But some compilation errors? Yeah, those throw an exception immediately, no chill. 💀
The flags parameter is reserved for future use, and it must always be zero. Non-negotiable, bestie.
OP * parse_fullstmt( U32 flags)
OP * Perl_parse_fullstmt(pTHX_ U32 flags)
✨ parse_label ✨
NOTE: parse_label is experimental and may change or be removed without notice. 🙈
Parses a single label, possibly optional, of the type that can prefix a Perl statement. It is up to you, the caller, to make sure the dynamic parser state (PL_parser et al) is correctly set to reflect the source of the code being parsed. If flags has the PARSE_OPTIONAL bit set, then the label is optional; otherwise it's mandatory, mandatory, mandatory. 💅
The name of the label comes back as a fresh scalar. If an optional label is absent, you get a null pointer. That's it, that's the whole vibe.
If an error occurs in parsing — which can only happen when the label is mandatory — a valid label is returned anyway. The error is reflected in the parser state, and normally that results in a single exception at the top level of parsing which covers all the compilation errors that occurred.
SV * parse_label( U32 flags)
SV * Perl_parse_label(pTHX_ U32 flags)
🌸 parse_listexpr 🌸
NOTE: parse_listexpr is experimental and may change or be removed without notice. 🙈
Parses a Perl list expression. This one can contain operators of precedence down to the comma operator. The expression must be followed (and thus terminated) either by a low-precedence logic operator such as or, or by something that would normally terminate an expression such as semicolon. If flags has the PARSE_OPTIONAL bit set, then the expression is optional, otherwise it's mandatory. It is up to the caller to ensure that the dynamic parser state (PL_parser et al) is correctly set to reflect the source of the code to be parsed and the lexical context for the expression. Two things, don't forget the second one. 🫶
The op tree representing the expression is returned. If an optional expression is absent, a null pointer is returned; otherwise the pointer will be non-null.
If an error occurs in parsing or compilation, in most cases a valid op tree is returned anyway. The error is reflected in the parser state, normally resulting in a single exception at the top level of parsing which covers all the compilation errors that occurred. Some compilation errors, however, will throw an exception immediately. 😭
OP * parse_listexpr( U32 flags)
OP * Perl_parse_listexpr(pTHX_ U32 flags)
🦋 parse_stmtseq 🦋
NOTE: parse_stmtseq is experimental and may change or be removed without notice. 🙈
Parses a sequence of zero or more Perl statements. These may be normal imperative statements (including optional labels), or declarations that have compile-time effect, or literally any mixture of the two. The statement sequence ends when a closing brace or end-of-file is encountered in a place where a new statement could have validly started. It is up to the caller to ensure that the dynamic parser state (PL_parser et al) is correctly set to reflect the source of the code to be parsed and the lexical context for the statements.
The op tree representing the statement sequence is returned. This may be a null pointer if the statements were all null — for example if there were no statements at all, or if there were only subroutine definitions (which have compile-time side effects, sneaky little things). If it's not null, it will be a lineseq list, normally including nextstate or equivalent ops.
If an error occurs in parsing or compilation, in most cases a valid op tree is returned anyway. The error is reflected in the parser state, normally resulting in a single exception at the top level of parsing which covers all the compilation errors that occurred. Some compilation errors, however, will throw an exception immediately.
The flags parameter is reserved for future use, and must always be zero.
OP * parse_stmtseq( U32 flags)
OP * Perl_parse_stmtseq(pTHX_ U32 flags)
🎀 parse_subsignature 🎀
NOTE: parse_subsignature is experimental and may change or be removed without notice. 🙈
Parses a subroutine signature declaration. That's the contents of the parentheses following a named or anonymous subroutine declaration when the signatures feature is enabled. Heads up though: this function neither expects nor consumes the opening and closing parentheses around the signature — handling those is the caller's job, not ours. 👑
This function must only be called during parsing of a subroutine, i.e. after start_subparse has been called. It might allocate lexical variables on the pad for the current subroutine.
The op tree to unpack the arguments from the stack at runtime is returned. This op tree should appear at the beginning of the compiled function. The caller may wish to use op_append_list to build their function body after it, or splice it together with the body before calling newATTRSUB.
The flags parameter is reserved for future use, and must always be zero.
OP * parse_subsignature( U32 flags)
OP * Perl_parse_subsignature(pTHX_ U32 flags)
💜 parse_termexpr 💜
NOTE: parse_termexpr is experimental and may change or be removed without notice. 🙈
Parses a Perl term expression. This may contain operators of precedence down to the assignment operators. The expression must be followed (and thus terminated) either by a comma or lower-precedence operator, or by something that would normally terminate an expression such as semicolon. If flags has the PARSE_OPTIONAL bit set, then the expression is optional, otherwise it's mandatory. It is up to the caller to ensure that the dynamic parser state (PL_parser et al) is correctly set to reflect the source of the code to be parsed and the lexical context for the expression.
The op tree representing the expression is returned. If an optional expression is absent, a null pointer is returned; otherwise the pointer will be non-null.
If an error occurs in parsing or compilation, in most cases a valid op tree is returned anyway. The error is reflected in the parser state, normally resulting in a single exception at the top level of parsing which covers all the compilation errors that occurred. Some compilation errors, however, will throw an exception immediately. 💀
OP * parse_termexpr( U32 flags)
OP * Perl_parse_termexpr(pTHX_ U32 flags)
⭐ PL_parser ⭐
Pointer to a structure encapsulating the state of the parsing operation currently in progress. You can locally change this pointer to perform a nested parse without interfering with the state of an outer parse — iconic behaviour honestly. Individual members of PL_parser have their own documentation, keep scrolling. 💖
🌈 PL_parser->bufend 🌈
NOTE: PL_parser->bufend is experimental and may change or be removed without notice. 🙈
Direct pointer to the end of the chunk of text currently being lexed — aka the end of the lexer buffer. This is equal to SvPVX(PL_parser->linestr) + SvCUR(PL_parser->linestr). A NUL character (zero octet) is always located at the end of the buffer, and it does not count as part of the buffer's contents. It's just vibing there as a terminator. ✨
🧚 PL_parser->bufptr 🧚
NOTE: PL_parser->bufptr is experimental and may change or be removed without notice. 🙈
Points to the current position of lexing inside the lexer buffer. Characters around this point may be freely examined, within the range delimited by SvPVX(PL_parser->linestr) and PL_parser->bufend. The octets of the buffer may be intended to be interpreted as either UTF-8 or Latin-1, as indicated by lex_bufutf8.
Lexing code (whether in the Perl core or not) moves this pointer past the characters that it consumes. It is also expected to perform some bookkeeping whenever a newline character is consumed. This movement can be more conveniently performed by the function lex_read_to, which handles newlines appropriately. Use the helper, bestie, don't do it by hand. 🫶
Interpretation of the buffer's octets can be abstracted out by using the slightly higher-level functions lex_peek_unichar and lex_read_unichar.
🍓 PL_parser->linestart 🍓
NOTE: PL_parser->linestart is experimental and may change or be removed without notice. 🙈
Points to the start of the current line inside the lexer buffer. This is useful for indicating at which column an error occurred, and honestly not much else. It must be updated by any lexing code that consumes a newline; the function lex_read_to handles this detail for you.
👑 PL_parser->linestr 👑
NOTE: PL_parser->linestr is experimental and may change or be removed without notice. 🙈
Buffer scalar containing the chunk currently under consideration of the text currently being lexed. This is always a plain string scalar (for which SvPOK is true). It is not intended to be used as a scalar by normal scalar means; instead, refer to the buffer directly by the pointer variables described below.
The lexer maintains various char* pointers to things in the PL_parser->linestr buffer. If PL_parser->linestr is ever reallocated, all of these pointers must be updated. Do not attempt to do this manually — that's a one-way ticket to bug city 😭 — but rather use lex_grow_linestr if you need to reallocate the buffer.
The content of the text chunk in the buffer is commonly exactly one complete line of input, up to and including a newline terminator, but there are situations where it is otherwise. The octets of the buffer may be intended to be interpreted as either UTF-8 or Latin-1. The function lex_bufutf8 tells you which. Do not use the SvUTF8 flag on this scalar, which may disagree with it. Rude of it, but that's the rule. 💅
For direct examination of the buffer, the variable PL_parser->bufend points to the end of the buffer. The current lexing position is pointed to by PL_parser->bufptr. Direct use of these pointers is usually preferable to examination of the scalar through normal scalar means.
"resume_compcv_and_save"
okay so bestie, this one resumes a buffer that a previous suspend_compcv call put on pause 💖 — and it does it in a way that it'll get re-suspended at the end of the scope, so you can pick it back up again later. The catch? You gotta use it inside an ENTER/LEAVE scoped pair, no exceptions, no cap.
void resume_compcv_and_save(struct suspended_compcv *buffer)
"resume_compcv_final"
Same vibe, different ending 🎬 — this resumes the parser state that suspend_compcv saved, but for the final time, right before it gets compiled into a full CV. Also lives inside an ENTER/LEAVE scoped pair. Periodt.
void resume_compcv_final(struct suspended_compcv *buffer)
"suspend_compcv"
This is the icon, the moment ✨ — it implements part of the whole "suspended compilation CV" concept, which is literally just a way to pause the parser and compiler mid-CV so you can come back to it later. Main character energy.
So what it does: it saves the current state of the subroutine under compilation (PL_compcv) into the buffer you hand it. Use it initially to create the state in the buffer, and place it as the very last thing before a LEAVE inside a block. Like so:
ENTER;
start_subparse(0);
...
suspend_compcv(&buffer);
LEAVE;
Once it's suspended, you can come back and continue the parsing from where it stopped using either resume_compcv_final or resume_compcv_and_save. It's giving pause-and-resume 🦋.
void suspend_compcv( struct suspended_compcv *buffer)
void Perl_suspend_compcv(pTHX_ struct suspended_compcv *buffer)
"valid_identifier_pve"
Returns true if the string from s up to end would count as a valid Perl identifier 💅. That means it has to start with a character matching isIDFIRST, and everything after has to match isIDCONT. An empty string (i.e. when end is s) returns false — sorry, empty strings don't get to be identifiers.
If flags has the SVf_UTF8 bit set, then the string is presumed to be UTF-8 encoded, and the suitable Unicode character test functions get used instead. Fancy.
bool valid_identifier_pve( const char *s,
const char *end, U32 flags)
bool Perl_valid_identifier_pve(pTHX_ const char *s,
const char *end, U32 flags)
"valid_identifier_pvn"
Basically the same energy as above, but with a length instead of an end pointer 💖. Returns true if the string s of length len would be a valid Perl identifier. Starts with isIDFIRST, continues with isIDCONT. An empty string (when len is zero) returns false.
If flags contains SVf_UTF8, the string is presumed UTF-8 encoded and the Unicode character test functions come out to play 🌸.
bool valid_identifier_pvn( const char *s, STRLEN len,
U32 flags)
bool Perl_valid_identifier_pvn(pTHX_ const char *s, STRLEN len,
U32 flags)
"valid_identifier_sv"
Returns true if the SV you hand it contains a non-empty string whose characters match up according to valid_identifier_pvn. Returns false if you give it NULL, an undefined SV, or an SV that doesn't hold a non-empty string. Iconic but strict 😤.
ngl, important detail: this does not invoke get magic on the SV beforehand.
bool valid_identifier_sv( SV *sv)
bool Perl_valid_identifier_sv(pTHX_ SV *sv)
"wrap_infix_plugin"
⚠️ NOTE: wrap_infix_plugin is experimental and may change or be removed without notice. Don't get attached bestie 😭.
⚠️ NOTE: This API exists entirely for the purpose of making the CPAN moduleXS::Parse::Infixwork. It is not expected that additional modules will make use of it; rather, that they should useXS::Parse::Infixto provide parsing of new infix operators.
So what it does: it puts a C function into the chain of infix plugins 🔗. This is the preferred way to manipulate the PL_infix_plugin variable. new_plugin is a pointer to the C function being added to the infix plugin chain, and old_plugin_p points to a storage location where a pointer to the next function in the chain will be stored. The value of new_plugin gets written into the PL_infix_plugin variable, while whatever was previously stored there gets written to *old_plugin_p.
Direct access to PL_infix_plugin should be avoided. Just... don't 💅.
void wrap_infix_plugin( Perl_infix_plugin_t new_plugin,
Perl_infix_plugin_t *old_plugin_p)
void Perl_wrap_infix_plugin(pTHX_
Perl_infix_plugin_t new_plugin,
Perl_infix_plugin_t *old_plugin_p)
"wrap_keyword_plugin"
⚠️ NOTE: wrap_keyword_plugin is experimental and may change or be removed without notice. Same energy, same warning 🫶.
Puts a C function into the chain of keyword plugins. This is the preferred way to manipulate the PL_keyword_plugin variable. new_plugin is a pointer to the C function being added to the keyword plugin chain, and old_plugin_p points to the storage location where a pointer to the next function in the chain will be stored. The value of new_plugin is written into PL_keyword_plugin, while the value previously stored there is written to *old_plugin_p.
Now here's the tea ☕: PL_keyword_plugin is global to an entire process, and a module wishing to hook keyword parsing may find itself invoked more than once per process — typically in different threads. To handle that, this function is idempotent. The location *old_plugin_p must initially (once per process) contain a null pointer. A C variable of static duration (declared at file scope, typically also marked static to give it internal linkage) will be implicitly initialised appropriately if it doesn't have an explicit initialiser. This function will only actually modify the plugin chain if it finds *old_plugin_p to be null. Also it's thread safe on the small scale — it uses appropriate locking to avoid race conditions when accessing PL_keyword_plugin. No cap.
When this function is called, the function referenced by new_plugin must be ready to be called, except for *old_plugin_p being unfilled. In a threading situation, new_plugin may be called immediately, even before this function has returned 😳. *old_plugin_p will always be appropriately set before new_plugin is called. If new_plugin decides not to do anything special with the identifier it's given (which is the usual case for most calls to a keyword plugin), it must chain the plugin function referenced by *old_plugin_p.
Taken all together, XS code to install a keyword plugin should typically look something like this:
static Perl_keyword_plugin_t next_keyword_plugin;
static OP *my_keyword_plugin(pTHX_
char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
{
if (memEQs(keyword_ptr, keyword_len,
"my_new_keyword")) {
...
} else {
return next_keyword_plugin(aTHX_
keyword_ptr, keyword_len, op_ptr);
}
}
BOOT:
wrap_keyword_plugin(my_keyword_plugin,
&next_keyword_plugin);
Direct access to PL_keyword_plugin should be avoided. Periodt 💅.
void wrap_keyword_plugin(Perl_keyword_plugin_t new_plugin,
Perl_keyword_plugin_t *old_plugin_p)
void Perl_wrap_keyword_plugin(pTHX_
Perl_keyword_plugin_t new_plugin,
Perl_keyword_plugin_t *old_plugin_p)
🌍 Locales 🌍
"DECLARATION_FOR_LC_NUMERIC_MANIPULATION"
This macro is meant to be used as a statement ✨. It declares a private variable (whose name begins with an underscore) that the other macros in this section need. If you fail to include it correctly, you should get a syntax error — so like, don't skip it 🙈. And for compatibility with C89 C compilers, it should be placed in a block before any executable statements.
void DECLARATION_FOR_LC_NUMERIC_MANIPULATION
"HAS_DUPLOCALE"
This symbol, if defined, means the duplocale routine is available to duplicate a locale object 🦋.
"HAS_FREELOCALE"
This symbol, if defined, means the freelocale routine is available to deallocate the resources associated with a locale object. Clean up after yourself bestie 💖.
"HAS_LC_MONETARY_2008"
This symbol, if defined, means the localeconv routine is available and has the additional members added in POSIX 1003.1-2008. Glow up era 💅.
"HAS_LOCALECONV"
This symbol, if defined, means the localeconv routine is available for numeric and monetary formatting conventions 💰.
"HAS_LOCALECONV_L"
This symbol, if defined, means the localeconv_l routine is available to query certain information about a locale 🔍.
"HAS_NEWLOCALE"
This symbol, if defined, means the newlocale routine is available to return a new locale object or modify an existing locale object 🌈.
"HAS_NL_LANGINFO"
This symbol, if defined, means the nl_langinfo routine is available to return locale data. You'll also need langinfo.h, and therefore I_LANGINFO too 📎.
"HAS_NL_LANGINFO_L"
This symbol, if defined, means the nl_langinfo_l routine is available to return locale data. And yes, you'll also need langinfo.h, and therefore I_LANGINFO.
"HAS_QUERYLOCALE"
This symbol, if defined, means the querylocale routine is available to return the name of the locale for a category mask 🏷️.
"HAS_SETLOCALE"
This symbol, if defined, means the setlocale routine is available to handle locale-specific ctype implementations.
"HAS_SETLOCALE_R"
This symbol, if defined, means the setlocale_r routine is available to setlocale re-entrantly 🔁.
"HAS_USELOCALE"
This symbol, if defined, means the uselocale routine is available to set the current locale for the calling thread 🧵.
"I_LANGINFO"
This symbol, if defined, tells the C program that langinfo.h exists and should be included 📥.
#ifdef I_LANGINFO
#include <langinfo.h>
#endif
"I_LOCALE"
This symbol, if defined, tells the C program that it should include locale.h 🎀.
#ifdef I_LOCALE
#include <locale.h>
#endif
🦋 I_XLOCALE 🦋
Okay so, if this symbol is defined, it's basically the C program being like “yup, the header xlocale.h is available.” Also peek at NEED_XLOCALE_H for the full tea. 💖
#ifdef I_XLOCALE
#include <xlocale.h>
#endif
✨ IN_LOCALE ✨
This one evaluates to TRUE if the plain locale pragma without a parameter (use locale) is in effect. Signature: bool IN_LOCALE
💅 IN_LOCALE_COMPILETIME 💅
Evaluates to TRUE if, when compiling a perl program (including an eval), the plain locale pragma without a parameter (use locale) is in effect. Signature: bool IN_LOCALE_COMPILETIME
🌸 IN_LOCALE_RUNTIME 🌸
Evaluates to TRUE if, when executing a perl program (including an eval), the plain locale pragma without a parameter (use locale) is in effect. Signature: bool IN_LOCALE_RUNTIME
🎀 NEED_XLOCALE_H 🎀
If defined, this symbol tells the C program to include xlocale.h to get newlocale() and its friends. Slay.
👑 Perl_langinfo* and Perl_langinfo8* 👑
Y’all, these are described under sv_langinfo — go peek there. 💅
🌈 PERL_LC_ALL_CATEGORY_POSITIONS_INIT 🌈
When defined, this symbol gives the C initializer for an array whose element [0] is the first category in the string returned by setlocale(LC_ALL, NULL) when not all categories are the same, on systems that use positional notation. After element [0] comes LC_ALL_SEPARATOR, then the category given by element [1], and so on. Iconic.
⭐ PERL_LC_ALL_SEPARATOR ⭐
If defined, this symbol gives the string returned by setlocale(LC_ALL, NULL) to separate categories that are in different locales on systems that use positional notation as opposed to 'name=value' pairs. An example on some platforms could be the '/' in "C/"de_DE"/C/"en_UK"/C/C". iykyk
💖 PERL_LC_ALL_USES_NAME_VALUE_PAIRS 💖
If defined, this symbol indicates to the C program that the string returned by setlocale(LC_ALL, NULL) uses 'name=value;' pairs to indicate what each category's locale is when they aren't all set to the same locale. For example, ""LC_NUMERIC"=C;"LC_CTYPE"="de_DE";..." When not defined, the system uses positional notation. Periodt.
🧚 Perl_localeconv 🧚
This is a thread-safe version of the libc localeconv(3). It's the same as POSIX::localeconv (returning a hash of the localeconv() fields), but directly callable from XS code. The hash is mortalized, so it must be dealt with immediately, bestie. Signature: HV * Perl_localeconv(pTHX)
🔥 Perl_setlocale 🔥
This is an (almost) drop-in replacement for the system setlocale(3), taking the same parameters, and returning the same information, except that it returns the correct underlying LC_NUMERIC locale. Regular setlocale will instead return "C" if the underlying locale has a non-dot decimal point character, or a non-empty thousands separator for displaying floating point numbers. Why? Because perl keeps that locale category such that it has a dot and empty separator, changing the locale briefly during the operations where the underlying one is required. Perl_setlocale knows about this, and compensates; regular setlocale doesn't.
Another reason it isn't completely a drop-in replacement: it's declared to return const char *, whereas the system setlocale omits the const (presumably because its API was specified long ago, and can't be updated; it is illegal to change the information setlocale returns; doing so leads to segfaults). Do not do that.
Finally, Perl_setlocale works under all circumstances, whereas plain setlocale can be completely ineffective on some platforms under some configurations.
Changing the locale is not a good idea when more than one thread is running, except on systems where the predefined variable ${^SAFE_LOCALES} is non-zero. This is because on such systems the locale is global to the whole process and not local to just the thread calling the function. So changing it in one thread instantaneously changes it in all. On some such systems, the system setlocale() is ineffective, returning the wrong information, and failing to actually change the locale. z/OS refuses to try to change the locale once a second thread is created. Perl_setlocale should give you accurate results of what actually happened on these problematic platforms, returning NULL if the system forbade the locale change.
The return points to a per-thread static buffer, which is overwritten the next time Perl_setlocale is called from the same thread.
Signature: const char * Perl_setlocale(const int category, const char *locale)
🎀 RESTORE_LC_NUMERIC 🎀
This is used in conjunction with one of the macros STORE_LC_NUMERIC_SET_TO_NEEDED and STORE_LC_NUMERIC_FORCE_TO_UNDERLYING to properly restore the LC_NUMERIC state.
A call to DECLARATION_FOR_LC_NUMERIC_MANIPULATION must have been made to declare at compile time a private variable used by this macro and the two STORE ones. This macro should be called as a single statement, not an expression, but with an empty argument list, like this:
{
DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
...
RESTORE_LC_NUMERIC();
...
}
Signature: void RESTORE_LC_NUMERIC()
🌟 SETLOCALE_ACCEPTS_ANY_LOCALE_NAME 🌟
If defined, this symbol indicates that the setlocale routine is available and it accepts any input locale name as valid. Periodt.
💜 STORE_LC_NUMERIC_FORCE_TO_UNDERLYING 💜
This is used by XS code that is LC_NUMERIC locale-aware to force the locale for category LC_NUMERIC to be what perl thinks is the current underlying locale. (The perl interpreter could be wrong about what the underlying locale actually is if some C or XS code has called the C library function setlocale(3) behind its back; calling sync_locale before calling this macro will update perl's records.)
A call to DECLARATION_FOR_LC_NUMERIC_MANIPULATION must have been made to declare at compile time a private variable used by this macro. This macro should be called as a single statement, not an expression, but with an empty argument list, like this:
{
DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
...
STORE_LC_NUMERIC_FORCE_TO_UNDERLYING();
...
RESTORE_LC_NUMERIC();
...
}
The private variable is used to save the current locale state, so that the requisite matching call to RESTORE_LC_NUMERIC can restore it.
On threaded perls not operating with thread-safe functionality, this macro uses a mutex to force a critical section. Therefore the matching RESTORE should be close by, and guaranteed to be called.
Signature: void STORE_LC_NUMERIC_FORCE_TO_UNDERLYING()
🍓 STORE_LC_NUMERIC_SET_TO_NEEDED 🍓
This is used to help wrap XS or C code that is LC_NUMERIC locale-aware. This locale category is generally kept set to a locale where the decimal radix character is a dot, and the separator between groups of digits is empty. This is because most XS code that reads floating point numbers is expecting them to have this syntax.
This macro makes sure the current LC_NUMERIC state is set properly, to be aware of locale if the call to the XS or C code from the Perl program is from within the scope of a use locale; or to ignore locale if the call is instead from outside such scope.
This macro is the start of wrapping the C or XS code; the wrap ending is done by calling the RESTORE_LC_NUMERIC macro after the operation. Otherwise the state can be changed that will adversely affect other XS code.
A call to DECLARATION_FOR_LC_NUMERIC_MANIPULATION must have been made to declare at compile time a private variable used by this macro. This macro should be called as a single statement, not an expression, but with an empty argument list, like this:
{
DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
...
STORE_LC_NUMERIC_SET_TO_NEEDED();
...
RESTORE_LC_NUMERIC();
...
}
On threaded perls not operating with thread-safe functionality, this macro uses a mutex to force a critical section. Therefore the matching RESTORE should be close by, and guaranteed to be called; see WITH_LC_NUMERIC_SET_TO_NEEDED for a more contained way to ensure that.
Signature: void STORE_LC_NUMERIC_SET_TO_NEEDED()
🌈 STORE_LC_NUMERIC_SET_TO_NEEDED_IN 🌈
Same as STORE_LC_NUMERIC_SET_TO_NEEDED with in_lc_numeric provided as the precalculated value of IN_LC(LC_NUMERIC). It is the caller's responsibility to ensure that the status of PL_compiling and PL_hints cannot have changed since the precalculation.
Signature: void STORE_LC_NUMERIC_SET_TO_NEEDED_IN(bool in_lc_numeric)
🧚 sv_langinfo, Perl_langinfo, Perl_langinfo8 🧚
These perform the equivalent functionality as the system nl_langinfo(3), taking the same item parameter values, but are preferred over calling that directly because they are portable to platforms lacking that function, are thread-safe, and can automatically handle UTF-8 strings.
The simplest to use is sv_langinfo. It returns an SV containing the correct PV and UTF8ness, requiring no extra muss or fuss from you. New code should use this form.
Perl_langinfo and Perl_langinfo8 are retained for backwards compatibility. Perl_langinfo is an (almost) drop-in replacement for the system nl_langinfo(3), but exists on systems that lack a native nl_langinfo.
Perl_langinfo8 is identical to Perl_langinfo except for an additional parameter, a pointer to a variable declared as utf8ness_t, into which it returns to you how you should treat the returned string with regards to it being encoded in UTF-8 or not.
Perl_langinfo and Perl_langinfo8 share private per-thread memory that will be changed the next time either one of them is called with any input, but not before.
Concerning the differences between these functions and plain nl_langinfo():
a. Perl_langinfo8 has an extra parameter, described above. Besides this, the other reason they aren't quite a drop-in replacement is actually an advantage. The constness of the return allows the compiler to catch attempts to write into the returned buffer, which is illegal and could cause run-time crashes. So don't write into it, bestie.
- b. They deliver the correct results for the
RADIXCHARandTHOUSEPitems, and you don't have to write any extra code for it. Why would you need extra code, bestie? Because those two come from theLC_NUMERIClocale category, which Perl normally keeps pinned so that the radix is a dot and the separator is the empty string — no matter what the underlying locale is supposed to be. So to get the results you'd expect, you'd have to temporarily toggle into the underlying locale and then toggle back. (You could use plainnl_langinfoplusSTORE_LC_NUMERIC_FORCE_TO_UNDERLYINGfor this, but then you'd miss out on all the other perks ofPerl_langinfo()💁 and also: not keepingLC_NUMERICin the C (or equivalent) locale would break a lot of CPAN, which is out here expecting the radix (decimal point) character to be a dot. 😭) - c. The system function they replace can have its static return buffer trashed — not just by a subsequent call to that same function, but by a
freelocale,setlocale, or any other locale change.sv_langinfosidesteps this problem entirely; the returned buffer of the other two isn't changed until the next call to one or the other, so the buffer is never in a trashed state. ✨ - d. The return buffer of
Perl_langinfoandPerl_langinfo8is per-thread, so it's also never overwritten by a call to these functions from another thread — unlike the function they replace. 🧵💅 - e. But most importantly?? They work on systems that don't have
nl_langinfo, such as Windows, which makes your code way more portable. 🪟🌈 Of the fifty-some possible items specified by the POSIX 2008 standard (https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/langinfo.h.html), only one is completely unimplemented — though on non-Windows platforms, another significant one is not fully implemented. They use various techniques to recover the other items, including callinglocaleconv(3)andstrftime(3), both of which are specified in C89, so they should always be available. Laterstrftime()versions have additional capabilities. If an item isn't available on your system, this returns either the value associated with the C locale, or simply""— whichever is more appropriate.
⚠️ It is important to note that, when called with an item that is recovered by usinglocaleconv, the buffer from any previous explicit call tolocaleconv(3)will be overwritten. But honestly you shouldn't be usinglocaleconvanyway, because it is very much not thread-safe, and it suffers from the same problems outlined in item b. above for the fields it returns that are controlled by theLC_NUMERIClocale category. Instead, dodge all of those problems by callingPerl_localeconv, which is thread-safe — or by using the methods given in perlcall to callPOSIX::localeconv(), which is also thread-safe. 🫶
The details for those items which may deviate from what this emulation returns and what a native nl_langinfo() would return are specified in I18N::Langinfo.
SV * sv_langinfo ( const nl_item item)
SV * Perl_sv_langinfo (pTHX_ const nl_item item)
const char * Perl_langinfo ( const nl_item item)
const char * Perl_langinfo8( const nl_item item,
utf8ness_t *utf8ness)
💅 switch_to_global_locale
This function copies the locale state of the calling thread into the program's global locale, and converts the thread to use that global locale. 🌍
It exists so that Perl can safely be used with C libraries that access the global locale and which can't be converted to not access it. Effectively, that means libraries that call setlocale(3) on non-Windows systems. (For portability, it's a good idea to use it on Windows too.)
The downside of using it? It disables the services that Perl provides to hide locale gotchas from your code. The service you'll most likely miss is the one about the radix character (decimal point) in floating point numbers. Code executed after this function is called can no longer just assume that this character is correct for the current circumstances. 🙈
To return to Perl control and restart the gotcha-prevention services, call sync_locale. Behavior is undefined for any pure Perl code that executes while the switch is in effect. 👀
The global locale and the per-thread locales are independent. As long as just one thread converts to the global locale, everything works smoothly. But if more than one does, they can easily interfere with each other, and races are likely. On Windows systems prior to Visual Studio 15 (at which point Microsoft fixed a bug), races can occur (even if only one thread has been converted to the global locale), but only if you use the following operations:
POSIX::localeconv
I18N::Langinfo, items "CRNCYSTR" and "THOUSEP"
sv_langinfo in perlapi, items "CRNCYSTR" and "THOUSEP"
The first item is not fixable (except by upgrading to a later Visual Studio release), but it would be possible to work around the latter two items by having Perl change its algorithm for calculating these to use Windows API functions (likely GetNumberFormat and GetCurrencyFormat); patches welcome. 🛠️✨
XS code should never call plain setlocale, but should instead be converted to either call Perl_setlocale (which is a drop-in for the system setlocale) or use the methods given in perlcall to call POSIX::setlocale. Either one will transparently and properly handle all cases of single- vs multi-thread, POSIX 2008-supported or not. 💖
void switch_to_global_locale()
void Perl_switch_to_global_locale(pTHX)
🌸 sync_locale
This function copies the state of the program global locale into the calling thread, and converts that thread to using per-thread locales, if it wasn't already, and the platform supports them. The LC_NUMERIC locale is toggled into the standard state (using the C locale's conventions), if not within the lexical scope of use locale. 🎀
Perl will now consider itself to have control of the locale. 👑
Since unthreaded perls have only a global locale, this function is a no-op without threads. (Nothing to sync, bestie! 💀)
This function is intended for use with C libraries that do locale manipulation. It lets Perl accommodate the use of them. Call this function before transferring back to Perl space, so that it knows what state the C code has left things in. 🦋
XS code should not manipulate the locale on its own. Instead, Perl_setlocale can be used at any time to query or change the locale (though changing the locale is antisocial and dangerous on multi-threaded systems that don't have multi-thread safe locale operations. (See "Multi-threaded operation" in perllocale).) ⚠️
Using the libc setlocale(3) function should be avoided. Nevertheless, certain non-Perl libraries called from XS do call it, and their behavior may not be able to be changed. This function, along with switch_to_global_locale, can be used to get seamless behavior in these circumstances — as long as only one thread is involved. 🌈
If the library has an option to turn off its locale manipulation, doing that is preferable to using this mechanism. Gtk is such a library. 💅
The return value is a boolean: TRUE if the global locale at the time of call was in effect for the caller; and FALSE if a per-thread locale was in effect.
bool sync_locale()
bool Perl_sync_locale(pTHX)
✨ WITH_LC_NUMERIC_SET_TO_NEEDED
This macro invokes the supplied statement or block within the context of a STORE_LC_NUMERIC_SET_TO_NEEDED .. RESTORE_LC_NUMERIC pair if required, so e.g.:
WITH_LC_NUMERIC_SET_TO_NEEDED(
SNPRINTF_G(fv, ebuf, sizeof(ebuf), precis)
);
is equivalent to:
{
#ifdef USE_LOCALE_NUMERIC
DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
STORE_LC_NUMERIC_SET_TO_NEEDED();
#endif
SNPRINTF_G(fv, ebuf, sizeof(ebuf), precis);
#ifdef USE_LOCALE_NUMERIC
RESTORE_LC_NUMERIC();
#endif
}
void WITH_LC_NUMERIC_SET_TO_NEEDED(block)
🦋 WITH_LC_NUMERIC_SET_TO_NEEDED_IN
Same as WITH_LC_NUMERIC_SET_TO_NEEDED, with in_lc_numeric provided as the precalculated value of IN_LC(LC_NUMERIC). It is the caller's responsibility to ensure that the status of PL_compiling and PL_hints cannot have changed since the precalculation. 👀
void WITH_LC_NUMERIC_SET_TO_NEEDED_IN(bool in_lc_numeric, block)
✨ Magic ✨
okay so bestie, "Magic" is this special data that gets attached to SV structures so they can have "magical" properties 💫. The second any Perl code tries to read from, or assign to, an SV that's marked as magical, it calls the 'get' or 'set' function that goes with that SV's magic. A get gets called before reading an SV, so it can update its internal value first — like, get on $. writes the line number of the last read filehandle into the SV's IV slot. And set gets called after an SV has been written to, so it can actually use its changed value — set on $/ copies the SV's new value over to the PL_rs global variable. iconic behaviour tbh 🎀
Magic is implemented as a linked list of MAGIC structures attached to the SV. Every MAGIC struct holds the type of the magic, a pointer to an array of functions that implement the get(), set(), length() etc functions, plus space for some flags and pointers. For example, a tied variable has a MAGIC structure that contains a pointer to the object associated with the tie. no cap.
mg_clear
Clear something magical that the SV represents. See "sv_magic".
int mg_clear( SV *sv)
int Perl_mg_clear(pTHX_ SV *sv)
mg_copy
Copies the magic from one SV to another. See "sv_magic".
int mg_copy( SV *sv, SV *nsv, const char *key,
I32 klen)
int Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key,
I32 klen)
mg_find
Finds the magic pointer for "type" matching the SV. See "sv_magic".
MAGIC * mg_find(const SV *sv, int type)
MAGIC * Perl_mg_find(const SV *sv, int type)
mg_findext
Finds the magic pointer of "type" with the given "vtbl" for the "SV". See "sv_magicext".
MAGIC * mg_findext(const SV *sv, int type,
const MGVTBL *vtbl)
MAGIC * Perl_mg_findext(const SV *sv, int type,
const MGVTBL *vtbl)
mg_free
Free any magic storage used by the SV. See "sv_magic".
int mg_free( SV *sv)
int Perl_mg_free(pTHX_ SV *sv)
mg_free_type
Remove any magic of type "how" from the SV "sv". See "sv_magic".
void mg_free_type( SV *sv, int how)
void Perl_mg_free_type(pTHX_ SV *sv, int how)
mg_freeext
Remove any magic of type "how" using virtual table "vtbl" from the SV "sv". See "sv_magic".
mg_freeext(sv, how, NULL) is equivalent to mg_free_type(sv, how).
void mg_freeext( SV *sv, int how, const MGVTBL *vtbl)
void Perl_mg_freeext(pTHX_ SV *sv, int how, const MGVTBL *vtbl)
mg_get
Do magic before a value is retrieved from the SV. The type of SV must be >= SVt_PVMG. See "sv_magic".
int mg_get( SV *sv)
int Perl_mg_get(pTHX_ SV *sv)
mg_magical
Turns on the magical status of an SV. See "sv_magic".
void mg_magical(SV *sv)
void Perl_mg_magical(SV *sv)
mg_set
Do magic after a value is assigned to the SV. See "sv_magic".
int mg_set( SV *sv)
int Perl_mg_set(pTHX_ SV *sv)
okay now a whole little squad of magic constants and friends, bestie — they're all Described in perlguts:
MGf_COPYMGf_DUPMGf_LOCALMGVTBLPERL_MAGIC_arylenPERL_MAGIC_arylen_pPERL_MAGIC_backrefPERL_MAGIC_bmPERL_MAGIC_checkcallPERL_MAGIC_collxfrmPERL_MAGIC_dbfilePERL_MAGIC_dblinePERL_MAGIC_debugvarPERL_MAGIC_defelemPERL_MAGIC_destructPERL_MAGIC_envPERL_MAGIC_envelemPERL_MAGIC_extPERL_MAGIC_extvaluePERL_MAGIC_fmPERL_MAGIC_hintsPERL_MAGIC_hintselemPERL_MAGIC_hookPERL_MAGIC_hookelemPERL_MAGIC_isaPERL_MAGIC_isaelemPERL_MAGIC_lvrefPERL_MAGIC_nkeysPERL_MAGIC_nonelemPERL_MAGIC_overload_tablePERL_MAGIC_posPERL_MAGIC_qrPERL_MAGIC_regdataPERL_MAGIC_regdatumPERL_MAGIC_regex_globalPERL_MAGIC_rhashPERL_MAGIC_sharedPERL_MAGIC_shared_scalarPERL_MAGIC_sigPERL_MAGIC_sigelemPERL_MAGIC_substrPERL_MAGIC_svPERL_MAGIC_symtabPERL_MAGIC_taintPERL_MAGIC_tiedPERL_MAGIC_tiedelemPERL_MAGIC_tiedscalarPERL_MAGIC_utf8PERL_MAGIC_uvarPERL_MAGIC_uvar_elemPERL_MAGIC_vecPERL_MAGIC_vstring
SvTIED_obj
Described in perlinterp.
SvTIED_obj(SV *sv, MAGIC *mg)
🧠 Memory Management 🧠
dump_mstats
When it's enabled by compiling with -DDEBUGGING_MSTATS, it prints out statistics about malloc as two lines of numbers — one showing the length of the free list for each size category, and the second showing the number of mallocs - frees for each size category. 💀
"s", if it's not NULL, is used as a phrase to include in the output, like "after compilation".
void dump_mstats( const char *s)
void Perl_dump_mstats(pTHX_ const char *s)
HAS_MALLOC_GOOD_SIZE
This symbol, if it's defined, indicates that the malloc_good_size routine is available for use.
HAS_MALLOC_SIZE
This symbol, if it's defined, indicates that the malloc_size routine is available for use.
HASATTRIBUTE_MALLOC
Can we handle GCC attribute for malloc-style functions. 🤔
I_MALLOCMALLOC
This symbol, if it's defined, indicates to the C program that it should include malloc/malloc.h.
#ifdef I_MALLOCMALLOC
#include <mallocmalloc.h>
#endif
MYMALLOC
This symbol, if it's defined, indicates that we're using our own malloc. main character energy fr.
Newx / safemalloc
The XSUB-writer's interface to the C malloc function.
Memory obtained by this should ONLY be freed with Safefree. 🚨 do not play with this one bestie.
In 5.9.3, Newx() and friends replace the older New() API, and drops the first parameter, x, a debug aid which allowed callers to identify themselves. This aid has been superseded by a new build option, PERL_MEM_LOG (see "PERL_MEM_LOG" in perlhacktips). The older API is still there for use in XS modules supporting older perls.
void Newx (void* ptr, int nitems, type)
void* safemalloc(size_t size)
Newxc
The XSUB-writer's interface to the C malloc function, with cast. See also "Newx".
Memory obtained by this should ONLY be freed with Safefree.
void Newxc(void* ptr, int nitems, type, cast)
Newxz / safecalloc
The XSUB-writer's interface to the C calloc function. The allocated memory is zeroed with memzero. See also "Newx".
Memory obtained by this should ONLY be freed with Safefree.
void Newxz (void* ptr, int nitems, type)
void* safecalloc(size_t nitems, size_t item_size)
PERL_MALLOC_WRAP
This symbol, if it's defined, indicates that we'd like malloc wrap checks.
Renew / saferealloc
The XSUB-writer's interface to the C realloc function.
Memory obtained by this should ONLY be freed with Safefree.
void Renew (void* ptr, int nitems, type)
void* saferealloc(void *ptr, size_t size)
Renewc
The XSUB-writer's interface to the C realloc function, with cast.
Memory obtained by this should ONLY be freed with Safefree.
void Renewc(void* ptr, int nitems, type, cast)
safecalloc*
Described under "Newxz"
Safefree
The XSUB-writer's interface to the C free function.
This should ONLY be used on memory obtained using Newx and friends. no exceptions bestie 💅
void Safefree(void* ptr)
safemalloc*
Described under "Newx"
saferealloc*
Described under "Renew"
safesyscalloc
Safe version of system's calloc()
Malloc_t safesyscalloc(MEM_SIZE elements, MEM_SIZE size)
Malloc_t Perl_safesyscalloc(MEM_SIZE elements, MEM_SIZE size)
safesysfree
Safe version of system's free()
Free_t safesysfree(Malloc_t where)
Free_t Perl_safesysfree(Malloc_t where)
safesysmalloc
Paranoid version of system's malloc() 😭
Malloc_t safesysmalloc(MEM_SIZE nbytes)
Malloc_t Perl_safesysmalloc(MEM_SIZE nbytes)
safesysrealloc
Paranoid version of system's realloc()
Malloc_t safesysrealloc(Malloc_t where, MEM_SIZE nbytes)
Malloc_t Perl_safesysrealloc(Malloc_t where, MEM_SIZE nbytes)
🦋 MRO 🦋
Okay bestie, these functions are all about the method resolution order of perl classes — aka the whole "who do I inherit from and in what order" drama. Also peep perlmroapi. 💅
HvMROMETA— Described in perlmroapi.
struct mro_meta * HvMROMETA(HV *hv)
mro_get_from_name— Hands you back the mro that was previously registered undername, orNULLif it was never registered at all. See"mro_register". 🎀
const struct mro_alg * Perl_mro_get_from_name(pTHX_ SV *name)
mro_get_linear_isa— Returns the mro linearisation for the given stash. By default this is literally whatevermro_get_linear_isa_dfsreturns, unless some other MRO is in effect for the stash. The return value is a read-onlyAV*whose values are string SVs giving class names.
Bestie, heads up: you are responsible for SvREFCNT_inc() on the return value if you plan to store it anywhere semi-permanently. Otherwise it might get deleted right out from under you the next time the cache is invalidated. 😭 Not a vibe.
AV * mro_get_linear_isa( HV *stash)
AV * Perl_mro_get_linear_isa(pTHX_ HV *stash)
MRO_GET_PRIVATE_DATA— Described in perlmroapi.
SV* MRO_GET_PRIVATE_DATA(struct mro_meta *const smeta,
const struct mro_alg *const which)
mro_method_changed_in— Invalidates method caching on any child classes of the given stash, so they might actually notice the changes in this one. ✨
Ideally, all instances of PL_sub_generation++ in perl source outside of mro.c should be replaced by calls to this.
Perl automatically handles most of the common ways a method might be redefined. But there are a few ways you could change a method in a stash without the cache code noticing, and in those cases you need to call this method afterwards:
- Directly manipulating the stash HV entries from XS code.
- Assigning a reference to a readonly scalar constant into a stash entry in order to create a constant subroutine (like
constant.pmdoes).
This same method is available from pure perl via, mro::method_changed_in(classname).
void mro_method_changed_in( HV *stash)
void Perl_mro_method_changed_in(pTHX_ HV *stash)
mro_register— Registers a custom mro plugin. See perlmroapi for the details on this and other mro functions. 🫶
void Perl_mro_register(pTHX_ const struct mro_alg *mro)
mro_set_mro— Setsmetato the value contained in the registered mro plugin whose name isname.
Croaks if name hasn't been registered. 💀
void Perl_mro_set_mro(pTHX_ struct mro_meta * const meta,
SV * const name)
mro_set_private_data— Described in perlmroapi.
SV * Perl_mro_set_private_data(pTHX_
struct mro_meta * const smeta,
const struct mro_alg * const which,
SV * const data)
🌸 Multicall Functions 🌸
dMULTICALL— Declares local variables for a multicall. See"LIGHTWEIGHT CALLBACKS"in perlcall.
dMULTICALL;
MULTICALL— Makes a lightweight callback. See"LIGHTWEIGHT CALLBACKS"in perlcall. 💖
MULTICALL;
POP_MULTICALL— The closing bracket for a lightweight callback. See"LIGHTWEIGHT CALLBACKS"in perlcall.
POP_MULTICALL;
PUSH_MULTICALL— The opening bracket for a lightweight callback. See"LIGHTWEIGHT CALLBACKS"in perlcall.
PUSH_MULTICALL(CV* the_cv);
⭐ Numeric Functions ⭐
Atof*— Described under"my_atof".grok_atoUV— Parses a string, looking for a decimal unsigned integer. 🔢
On entry, pv points to the beginning of the string; valptr points to a UV that will receive the converted value, if found; endptr is either NULL or points to a variable that points to one byte beyond the point in pv that this routine should examine. If endptr is NULL, pv is assumed to be NUL-terminated.
Returns FALSE if pv doesn't represent a valid unsigned integer value (with no leading zeros). Otherwise it returns TRUE, and sets *valptr to that value.
If you constrain the portion of pv that is looked at by this function (by passing a non-NULL endptr), and if the initial bytes of that portion form a valid value, it will return TRUE, setting *endptr to the byte following the final digit of the value. But if there is no constraint at what's looked at, all of pv must be valid in order for TRUE to be returned. *endptr is unchanged from its value on input if FALSE is returned.
The only characters this accepts are the decimal digits '0'..'9'.
As opposed to atoi(3) or strtol(3), grok_atoUV does NOT allow optional leading whitespace, nor negative inputs. If such features are required, the calling code needs to explicitly implement those.
Note that this function returns FALSE for inputs that would overflow a UV, or have leading zeros. So a single 0 is accepted, but not 00 nor 01, 002, etc.
Background: atoi has severe problems with illegal inputs, it cannot be used for incremental parsing, and therefore should be avoided. atoi and strtol are also affected by locale settings, which can also be seen as a bug (global state controlled by user environment). 🙈
bool grok_atoUV(const char *pv, UV *valptr,
const char **endptr)
bool Perl_grok_atoUV(const char *pv, UV *valptr,
const char **endptr)
grok_bin— Converts a string representing a binary number to numeric form. 🧚
On entry start and *len_p give the string to scan, *flags gives conversion flags, and result should be NULL or a pointer to an NV. The scan stops at the end of the string, or at just before the first invalid character. Unless PERL_SCAN_SILENT_ILLDIGIT is set in *flags, encountering an invalid character (except NUL) will also trigger a warning. On return *len_p is set to the length of the scanned string, and *flags gives output flags.
If the value is <= UV_MAX it is returned as a UV, the output flags are clear, and nothing is written to *result. If the value is > UV_MAX, grok_bin returns UV_MAX, sets PERL_SCAN_GREATER_THAN_UV_MAX in the output flags, and writes an approximation of the correct value into *result (which is an NV; or the approximation is discarded if result is NULL).
The binary number may optionally be prefixed with 0b or b unless PERL_SCAN_DISALLOW_PREFIX is set in *flags on entry.
If PERL_SCAN_ALLOW_UNDERSCORES is set in *flags then any or all pairs of digits may be separated from each other by a single underscore; also a single leading underscore is accepted.
UV grok_bin( const char *start, STRLEN *len_p,
I32 *flags, NV *result)
UV Perl_grok_bin(pTHX_ const char *start, STRLEN *len_p,
I32 *flags, NV *result)
grok_hex— Converts a string representing a hex number to numeric form. 🌈
On entry start and *len_p give the string to scan, *flags gives conversion flags, and result should be NULL or a pointer to an NV. The scan stops at the end of the string, or at just before the first invalid character. Unless PERL_SCAN_SILENT_ILLDIGIT is set in *flags, encountering an invalid character (except NUL) will also trigger a warning. On return *len_p is set to the length of the scanned string, and *flags gives output flags.
If the value is <= UV_MAX it is returned as a UV, the output flags are clear, and nothing is written to *result. If the value is > UV_MAX, grok_hex returns UV_MAX, sets PERL_SCAN_GREATER_THAN_UV_MAX in the output flags, and writes an approximation of the correct value into *result (which is an NV; or the approximation is discarded if result is NULL).
The hex number may optionally be prefixed with 0x or x unless PERL_SCAN_DISALLOW_PREFIX is set in *flags on entry.
If PERL_SCAN_ALLOW_UNDERSCORES is set in *flags then any or all pairs of digits may be separated from each other by a single underscore; also a single leading underscore is accepted.
UV grok_hex( const char *start, STRLEN *len_p,
I32 *flags, NV *result)
UV Perl_grok_hex(pTHX_ const char *start, STRLEN *len_p,
I32 *flags, NV *result)
grok_infnan— A lil helper forgrok_number(), it accepts various ways of spelling "infinity" or "not a number", and returns one of the following flag combinations: 🎀
IS_NUMBER_INFINITY
IS_NUMBER_NAN
IS_NUMBER_INFINITY | IS_NUMBER_NEG
IS_NUMBER_NAN | IS_NUMBER_NEG
0
possibly |-ed with IS_NUMBER_TRAILING.
If an infinity or a not-a-number is recognized, *sp will point to one byte past the end of the recognized string. If the recognition fails, zero is returned, and *sp will not move.
int grok_infnan( const char **sp, const char *send)
int Perl_grok_infnan(pTHX_ const char **sp, const char *send)
grok_numbergrok_number_flags— Look for a number in thelenbytes starting atpv. If one isn't found, return0; otherwise return its type (and optionally its value). Ingrok_numberalllenbytes must be either leadingisSPACEcharacters or part of the number. The same is true ingrok_number_flagsunlessflagscontains thePERL_SCAN_TRAILINGbit, which allows for trailing non-numeric text. (This is the only difference between the two functions.) 🔥
The returned type is the ORing of various bits (#defined in perl.h) as described below:
- If the number is negative, the returned type will include the
IS_NUMBER_NEGbit. - If the absolute value of the integral portion of the found number fits in a UV, the returned type will include the
IS_NUMBER_IN_UVbit. If it won't fit, instead theIS_NUMBER_GREATER_THAN_UV_MAXbit will be included. - If the found number is not an integer, the returned type will include the
IS_NUMBER_NOT_INTbit. This happens either if the number is expressed in exponentialenotation, or if it includes a decimal point (radix) character. If exponential notation is used, then neitherIS_NUMBER_IN_UVnorIS_NUMBER_GREATER_THAN_UV_MAXbits are set. Otherwise, the integer part of the number is used to determine theIS_NUMBER_IN_UVandIS_NUMBER_GREATER_THAN_UV_MAXbits.
So like, if the number it found is a string saying it's infinity? The IS_NUMBER_INFINITY and IS_NUMBER_NOT_INT bits get included in the returned type. And if the found number is a string saying it is not a number, then the IS_NUMBER_NAN and IS_NUMBER_NOT_INT bits get included too. 💅
You can get the number's absolute integral value handed back to you by calling these functions with a non-NULL valuep argument. If the returned type includes the IS_NUMBER_IN_UV bit, *valuep will be set to the correct value. Otherwise... bestie it could well have been zapped with garbage 😭.
In grok_number_flags, when flags contains the PERL_SCAN_TRAILING bit, and trailing non-numeric text was found, the returned type will include the IS_NUMBER_TRAILING bit.
int grok_number ( const char *pv, STRLEN len,
UV *valuep)
int Perl_grok_number (pTHX_ const char *pv, STRLEN len,
UV *valuep)
int grok_number_flags( const char *pv, STRLEN len,
UV *valuep, U32 flags)
int Perl_grok_number_flags(pTHX_ const char *pv, STRLEN len,
UV *valuep, U32 flags)
✨ grok_numeric_radix ✨
✨ GROK_NUMERIC_RADIX ✨
These two? Literally identical, twins, same energy 🫶.
They scan and skip for a numeric decimal separator (the radix).
bool grok_numeric_radix( const char **sp,
const char *send)
bool Perl_grok_numeric_radix(pTHX_ const char **sp,
const char *send)
bool GROK_NUMERIC_RADIX( const char **sp,
const char *send)
✨ grok_oct ✨
Converts a string representing an octal number into numeric form, periodt.
On entry, start and *len_p give the string to scan, *flags gives conversion flags, and result should be NULL or a pointer to an NV. The scan stops at the end of the string, or right before the first invalid character. Unless PERL_SCAN_SILENT_ILLDIGIT is set in *flags, bumping into an invalid character (except NUL) will also trigger a warning 👀. On return, *len_p is set to the length of the scanned string, and *flags gives output flags.
If the value is <= UV_MAX it's returned as a UV, the output flags are clear, and nothing gets written to *result. If the value is > UV_MAX, grok_oct returns UV_MAX, sets PERL_SCAN_GREATER_THAN_UV_MAX in the output flags, and writes an approximation of the correct value into *result (which is an NV; or the approximation is discarded if result is NULL).
If PERL_SCAN_ALLOW_UNDERSCORES is set in *flags, then any or all pairs of digits may be separated from each other by a single underscore; also a single leading underscore is accepted. Understood? 💜
The PERL_SCAN_DISALLOW_PREFIX flag is always treated as being set for this function. No debate.
UV grok_oct( const char *start, STRLEN *len_p,
I32 *flags, NV *result)
UV Perl_grok_oct(pTHX_ const char *start, STRLEN *len_p,
I32 *flags, NV *result)
✨ isinfnan ✨
Perl_isinfnan() is a little utility function that returns true if the NV argument is either an infinity or a NaN, and false otherwise. Wanna test in more detail? Use Perl_isinf() and Perl_isnan().
This is also the logical inverse of Perl_isfinite(). The way they're opposites, iconic 🔥.
bool isinfnan(NV nv)
bool Perl_isinfnan(NV nv)
✨ my_atof ✨
✨ Atof ✨
Each of these is atof(3), but like, properly working with Perl's locale handling — accepting a dot radix character always, but also the current locale's radix character if and only if called from within the lexical scope of a Perl use locale statement. Conditional queen ✨
N.B. s must be NUL terminated, no cap.
NV my_atof( const char *s)
NV Perl_my_atof(pTHX_ const char *s)
NV Atof ( const char * const s)
✨ my_strtod ✨
✨ Strtod ✨
Identical, bestie. Same function, different name energy.
They act like the libc strtod(3) function, with three exceptions:
- Their return value is an NV. Plain
strtodreturns a double precision value. - Plain
strtodalways is expecting the radix character (or string) to be the one specified by the underlying locale the program is executing in. This is almost universally a dot (U+002E) or a comma (U+002C).
In contrast, these expect the radix to be a dot, except when called from within the scope of use locale, in which case they act like plain strtod, expecting the radix to be the one specified by the current locale.
- These are available even on platforms that lack plain
strtod(). Slay 💅
NV my_strtod(const char * const s, char **e)
NV Perl_my_strtod(const char * const s, char **e)
NV Strtod (const char * const s, char **e)
✨ PERL_ABS ✨
Typeless abs or fabs, etc. (The usage below makes it look like it's for integers, but ngl it works for any type.) Use this instead of the C library ones, because those force their argument to be what they're expecting, which can potentially lead to disaster 💀. But also beware: this evaluates its argument twice, so no x++. Do not do that. 🙈
int PERL_ABS(int x)
✨ Perl_acos ✨
✨ Perl_asin ✨
✨ Perl_atan ✨
✨ Perl_atan2 ✨
✨ Perl_ceil ✨
✨ Perl_cos ✨
✨ Perl_cosh ✨
✨ Perl_exp ✨
✨ Perl_floor ✨
✨ Perl_fmod ✨
✨ Perl_frexp ✨
✨ Perl_isfinite ✨
✨ Perl_isinf ✨
✨ Perl_isnan ✨
✨ Perl_ldexp ✨
✨ Perl_log ✨
✨ Perl_log10 ✨
✨ Perl_modf ✨
✨ Perl_pow ✨
✨ Perl_sin ✨
✨ Perl_sinh ✨
✨ Perl_sqrt ✨
✨ Perl_tan ✨
✨ Perl_tanh ✨
These perform the corresponding mathematical operation on the operand(s), using the libc function built for the task that has just enough precision for an NV on this platform. If no such function with sufficient precision exists, the highest precision one available is used instead. Making it work, always 🌈
NV Perl_acos (NV x)
NV Perl_asin (NV x)
NV Perl_atan (NV x)
NV Perl_atan2 (NV x, NV y)
NV Perl_ceil (NV x)
NV Perl_cos (NV x)
NV Perl_cosh (NV x)
NV Perl_exp (NV x)
NV Perl_floor (NV x)
NV Perl_fmod (NV x, NV y)
NV Perl_frexp (NV x, int *exp)
IV Perl_isfinite(NV x)
IV Perl_isinf (NV x)
IV Perl_isnan (NV x)
NV Perl_ldexp (NV x, int exp)
NV Perl_log (NV x)
NV Perl_log10 (NV x)
NV Perl_modf (NV x, NV *iptr)
NV Perl_pow (NV x, NV y)
NV Perl_sin (NV x)
NV Perl_sinh (NV x)
NV Perl_sqrt (NV x)
NV Perl_tan (NV x)
NV Perl_tanh (NV x)
✨ Perl_signbit ✨
NOTE: Perl_signbit is experimental and may change or be removed without notice. So like, don't build your whole personality on it 😭.
Return a non-zero integer if the sign bit on an NV is set, and 0 if it is not.
If Configure detects this system has a signbit() that will work with our NVs, then we just use it via the #define in perl.h. Otherwise, we fall back on this implementation. The main use of this function is catching -0.0. Sneaky little negative zero 🎀
Configure notes: This function is called 'Perl_signbit' instead of a plain 'signbit' because it's easy to imagine a system having a signbit() function or macro that doesn't happen to work with our particular choice of NVs. We shouldn't just re-#define signbit as Perl_signbit and expect the standard system headers to be happy. Also, this is a no-context function (no pTHX_) because Perl_signbit() is usually re-#defined in perl.h as a simple macro call to the system's signbit(). Users should just always call Perl_signbit().
int Perl_signbit(NV f)
✨ Perl_sin* ✨
✨ Perl_sinh* ✨
✨ Perl_sqrt* ✨
✨ Perl_tan* ✨
✨ Perl_tanh* ✨
Described up under Perl_acos. Go peek 👑
✨ PL_hexdigit ✨
This array, indexed by an integer, converts that value into the character that represents it. For example, if the input is 8, the return will be a string whose first character is '8'. What's actually returned is a pointer into a string. All you're interested in is the first character of that string. To get uppercase letters (for the values 10..15), add 16 to the index. Hence, PL_hexdigit[11] is 'b', and PL_hexdigit[11+16] is 'B'. Adding 16 to an index whose representation is '0'..'9' yields the same as not adding 16. Indices outside the range 0..31 result in (bad) undefined behavior. So like, stay in range bestie 💀
✨ READ_XDIGIT ✨
Returns the value of an ASCII-range hex digit and advances the string pointer. Behaviour is only well defined when isXDIGIT(*str) is true.
U8 READ_XDIGIT(char str*)
✨ scan_bin ✨
For backwards compatibility. Use grok_bin instead.
NV scan_bin( const char *start, STRLEN len,
STRLEN *retlen)
NV Perl_scan_bin(pTHX_ const char *start, STRLEN len,
STRLEN *retlen)
✨ scan_hex ✨
For backwards compatibility. Use grok_hex instead.
NV scan_hex( const char *start, STRLEN len,
STRLEN *retlen)
NV Perl_scan_hex(pTHX_ const char *start, STRLEN len,
STRLEN *retlen)
✨ scan_oct ✨
For backwards compatibility. Use grok_oct instead.
NV scan_oct( const char *start, STRLEN len,
STRLEN *retlen)
NV Perl_scan_oct(pTHX_ const char *start, STRLEN len,
STRLEN *retlen)
✨ Strtod* ✨
Described under my_strtod. Go look 💜
✨ Strtol ✨
Platform and configuration independent strtol. This expands to the appropriate strotol-like function based on the platform and Configure options>. For example it could expand to strtoll or strtoq instead of strtol.
NV Strtol(const char * const s, char ** e, int base)
✨ Strtoul ✨
Platform and configuration independent strtoul. This expands to the appropriate strotoul-like function based on the platform and Configure options>. For example it could expand to strtoull or strtouq instead of strtoul.
NV Strtoul(const char * const s, char ** e, int base)
🌈 OPTREES 🌈
✨ alloccopstash ✨
NOTE: alloccopstash is experimental and may change or be removed without notice. It's giving "don't get attached" 😭.
okay so only available under threaded builds, bestie — this one grabs a whole entry in PL_stashpad for the stash you hand it. 🎀
PADOFFSET alloccopstash( HV *hv)
PADOFFSET Perl_alloccopstash(pTHX_ HV *hv)
✨ "BINOP" ✨
Described in perlguts. Go read it there, babe, it's giving homework but it's fine. 📚
🦋 "block_end" 🦋
Handles compile-time scope exit. So "floor" is the savestack index that "block_start" handed you, and "seq" is the body of the block. Returns the block, possibly modified — so it can come back different, no cap. 💅
OP * block_end( I32 floor, OP *seq)
OP * Perl_block_end(pTHX_ I32 floor, OP *seq)
🌸 "block_start" 🌸
Handles compile-time scope entry. It arranges for hints to be restored on block exit, and it also handles pad sequence numbers so your lexical variables scope correctly — the way that it just quietly keeps everything in line? iconic. ✨ Returns a savestack index for use with "block_end".
int block_start( int full)
int Perl_block_start(pTHX_ int full)
💖 "ck_entersub_args_list" 💖
Does the default fixup of the arguments part of an "entersub" op tree. Which, in human terms, means applying list context to every single one of the argument ops. 🫶
This is the standard treatment for a call marked with "&", or a method call, or a call through a subroutine reference, or literally any other call where the callee can't be identified at compile time, or a call where the callee has no prototype.
OP * ck_entersub_args_list( OP *entersubop)
OP * Perl_ck_entersub_args_list(pTHX_ OP *entersubop)
⭐ "ck_entersub_args_proto" ⭐
Does the fixup of the arguments part of an "entersub" op tree, but based on a subroutine prototype. This makes all kinds of modifications to the argument ops — from applying context, all the way up to inserting "refgen" ops — plus checking the number and syntactic types of the arguments, exactly as the prototype directs. 💅
This is the standard treatment for a subroutine call that's not marked with "&", where the callee can be identified at compile time and does have a prototype.
"protosv" supplies the subroutine prototype to be applied to the call. It can be a normal defined scalar, in which case the string value gets used. OR, for convenience, it can be a subroutine object (a "CV*" that's been cast to "SV*") which has a prototype. Either way, the prototype you supply does not need to match the actual callee referenced by the op tree. 🙈
If the argument ops disagree with the prototype — say, an unacceptable number of arguments — a valid op tree still gets returned anyway. The error is reflected in the parser state, which normally results in a single exception at the top level of parsing that covers all the compilation errors that happened. In the error message, the callee is referred to by the name defined by the "namegv" parameter.
OP * ck_entersub_args_proto( OP *entersubop,
GV *namegv, SV *protosv)
OP * Perl_ck_entersub_args_proto(pTHX_ OP *entersubop,
GV *namegv, SV *protosv)
🌈 "ck_entersub_args_proto_or_list" 🌈
Does the fixup of the arguments part of an "entersub" op tree — either based on a subroutine prototype, or using the default list-context processing. Whichever, bestie. It's the standard treatment for a subroutine call not marked with "&", where the callee can be identified at compile time. 👑
"protosv" supplies the subroutine prototype to be applied to the call, or it indicates that there's no prototype. It may be a normal scalar — if it's defined then the string value gets used as the prototype, and if it's undefined then there's no prototype. OR, for convenience, it can be a subroutine object (a "CV*" cast to "SV*"), whose prototype gets used if it has one. The prototype (or the lack of one) that you supply, in whichever form, does not need to match the actual callee referenced by the op tree. 🦋
If the argument ops disagree with the prototype — like, an unacceptable number of arguments — a valid op tree still comes back anyway. The error lives in the parser state, normally resulting in a single exception at the top level of parsing that covers all the compilation errors that occurred. In the error message, the callee is referred to by the name defined by the "namegv" parameter.
OP * ck_entersub_args_proto_or_list( OP *entersubop,
GV *namegv,
SV *protosv)
OP * Perl_ck_entersub_args_proto_or_list(pTHX_ OP *entersubop,
GV *namegv,
SV *protosv)
🎀 "cv_const_sv" 🎀
If "cv" is a constant sub that's eligible for inlining, this returns the constant value that the sub returns. Otherwise? Returns "NULL". That's it, that's the whole vibe check. 💀
Constant subs can be created with "newCONSTSUB", or as described in "Constant Functions" in perlsub.
SV * cv_const_sv(const CV * const cv)
SV * Perl_cv_const_sv(const CV * const cv)
💜 "cv_get_call_checker" 💜
The original form of "cv_get_call_checker_flags", and it does not return checker flags. So heads up: when you're using a checker function that came back from this one, it is only safe to call it with a genuine GV as its "namegv" argument. Only safe. Don't get cute with it. 😭
void cv_get_call_checker( CV *cv,
Perl_call_checker *ckfun_p,
SV **ckobj_p)
void Perl_cv_get_call_checker(pTHX_ CV *cv,
Perl_call_checker *ckfun_p,
SV **ckobj_p)
✨ "cv_get_call_checker_flags" ✨
Retrieves the function that's gonna be used to fix up a call to "cv". Specifically, that function gets applied to an "entersub" op tree for a subroutine call, not marked with "&", where the callee can be identified at compile time as "cv". 🫶
The C-level function pointer comes back in *ckfun_p, an SV argument for it comes back in *ckobj_p, and control flags come back in *ckflags_p. The function is meant to be called like this:
entersubop = (*ckfun_p)(aTHX_ entersubop, namegv, (*ckobj_p));
In that call, "entersubop" is a pointer to the "entersub" op, which the check function is allowed to replace, and "namegv" supplies the name that the check function should use to refer to the callee of the "entersub" op if it needs to emit any diagnostics. And yeah, you're permitted to apply the check function in non-standard situations — like to a call to a different subroutine, or to a method call. Live your life. 💅
"namegv" may not actually be a GV. If the "CALL_CHECKER_REQUIRE_GV" bit is clear in *ckflags_p, it's permitted to pass a CV or another SV instead — anything that can be used as the first argument to "cv_name". But if the "CALL_CHECKER_REQUIRE_GV" bit is set in *ckflags_p, then the check function requires "namegv" to be a genuine GV. No exceptions, periodt. 👑
By default, the check function is Perl_ck_entersub_args_proto_or_list, the SV parameter is "cv" itself, and the "CALL_CHECKER_REQUIRE_GV" flag is clear. That's what implements standard prototype processing. And it can be changed, for a particular subroutine, with "cv_set_call_checker_flags". 🔥
If the "CALL_CHECKER_REQUIRE_GV" bit is set in "gflags", that indicates the caller only knows about the genuine-GV version of "namegv", so accordingly the corresponding bit will always be set in *ckflags_p, no matter what the check function recorded as its requirements. If the "CALL_CHECKER_REQUIRE_GV" bit is clear in "gflags", that indicates the caller knows about the possibility of passing something other than a GV as "namegv", so accordingly the corresponding bit may be either set or clear in *ckflags_p — indicating the check function's recorded requirements. 🌸
"gflags" is a bitset passed into "cv_get_call_checker_flags", and in it, only the "CALL_CHECKER_REQUIRE_GV" bit currently has a defined meaning (see above, bestie). All other bits should be clear. 😭
void cv_get_call_checker_flags(CV *cv, U32 gflags,
Perl_call_checker *ckfun_p,
SV **ckobj_p,
U32 *ckflags_p)
void Perl_cv_get_call_checker_flags(pTHX_ CV *cv, U32 gflags,
Perl_call_checker *ckfun_p,
SV **ckobj_p,
U32 *ckflags_p)
🍓 "cv_set_call_checker" 🍓
The original form of "cv_set_call_checker_flags", and it just passes it the "CALL_CHECKER_REQUIRE_GV" flag for backward-compatibility. What that flag setting does: the check function is guaranteed to get a genuine GV as its "namegv" argument. Nice, right? 💖
void cv_set_call_checker( CV *cv,
Perl_call_checker ckfun,
SV *ckobj)
void Perl_cv_set_call_checker(pTHX_ CV *cv,
Perl_call_checker ckfun,
SV *ckobj)
🧚 "cv_set_call_checker_flags" 🧚
Sets the function that will be used to fix up a call to "cv". Specifically, that function gets applied to an "entersub" op tree for a subroutine call, not marked with "&", where the callee can be identified at compile time as "cv". ⭐
The C-level function pointer is supplied in "ckfun", an SV argument for it is supplied in "ckobj", and control flags are supplied in "ckflags". The function should be defined like this:
STATIC OP * ckfun(pTHX_ OP *op, GV *namegv, SV *ckobj)
And it's intended to be called like this:
entersubop = ckfun(aTHX_ entersubop, namegv, ckobj);
In that call, "entersubop" is a pointer to the "entersub" op, which may be replaced by the check function, and "namegv" supplies the name that the check function should use to refer to the callee of the "entersub" op if it needs to emit any diagnostics. And yes, you're permitted to apply the check function in non-standard situations — like to a call to a different subroutine, or to a method call. 🌈
"namegv" may not actually be a GV. For efficiency, perl may pass a CV or another SV instead. Whatever gets passed can be used as the first argument to "cv_name". And if you want to force perl to pass a GV, you can do that by including "CALL_CHECKER_REQUIRE_GV" in the "ckflags". 👑
ckflags is a bitset, and rn literally only one bit in it has a defined meaning: CALL_CHECKER_REQUIRE_GV (scroll up for what that one does, bestie 💅). Every other bit? Should be clear. Zero. Nada. Don't be cute with it.
And if you wanna know what the current setting is for one specific CV? That's literally what cv_get_call_checker_flags is for 🫶
void cv_set_call_checker_flags( CV *cv,
Perl_call_checker ckfun,
SV *ckobj, U32 ckflags)
void Perl_cv_set_call_checker_flags(pTHX_ CV *cv,
Perl_call_checker ckfun,
SV *ckobj, U32 ckflags)
finalize_optree
Okay so this one finalizes the optree ✨. Call it directly after the complete optree is built, no detours. It does some extra checking that the normal ck_xxx functions just can't do, and it makes the tree thread-safe. Slay.
void finalize_optree( OP *o)
void Perl_finalize_optree(pTHX_ OP *o)
forbid_outofblock_ops
🚨 NOTE: forbid_outofblock_ops is experimental and may change or get yeeted entirely without notice. No cap.
So this checks an optree that implements a block, making sure there are no control-flow ops trying to dip out of that block. Any OP_RETURN is forbidden, and so is any OP_GOTO. Loops get analysed, so any LOOPEX op (OP_NEXT, OP_LAST or OP_REDO) that affects a loop containing it within the block is permitted — but the ones that don't are forbidden. Sorry babes 💀
If any of these forbidden constructions get detected, it throws an exception, using the op name and the blockname argument to build a suitable message.
And listen — this function alone is not enough to guarantee the optree won't do any of these forbidden activities at runtime 😭. Because it might call a different function that performs a non-local LOOPEX, or a string-eval() that does a goto, or like, various other things. It's intended purely as a compile-time check for the ones that could be detected statically. Depending on the circumstance you're using it for, you may need additional runtime checks.
Also note, currently all OP_GOTO ops are forbidden, even in cases where they'd otherwise be perfectly safe to execute. That might get permitted in a later version. For now? Nope 🚫
void forbid_outofblock_ops( OP *o,
const char *blockname)
void Perl_forbid_outofblock_ops(pTHX_ OP *o,
const char *blockname)
LINKLIST
Give it the root of an optree and it links the tree up in execution order using the op_next pointers, then hands you back the first op that gets executed 🔥. Already been done before? It will not redo it — you just get o->op_next back. And if o->op_next isn't set yet, then o should be at least an UNOP.
OP* LINKLIST(OP *o)
LISTOP
LOGOP
LOOP
Described in perlguts 🦋 go read that one, bestie.
newANONATTRSUB
Constructs a nameless (anonymous) Perl subroutine, and also does some surrounding jobs while it's at it.
This is the same as newATTRSUB_x in perlintern, with its o_is_gv parameter set to FALSE and its o parameter set to NULL. More deets? See newATTRSUB_x in perlintern 💜
OP * newANONATTRSUB( I32 floor, OP *proto, OP *attrs,
OP *block)
OP * Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs,
OP *block)
newANONHASH
Constructs, checks, and returns an anonymous hash op 🎀
OP * newANONHASH( OP *o)
OP * Perl_newANONHASH(pTHX_ OP *o)
newANONLIST
Constructs, checks, and returns an anonymous list op 🌸
OP * newANONLIST( OP *o)
OP * Perl_newANONLIST(pTHX_ OP *o)
newANONSUB
Constructs a nameless (anonymous) Perl subroutine without attributes, and yes, also performing some surrounding jobs.
This is the same as newATTRSUB_x in perlintern, with its o_is_gv parameter set to FALSE, and its o and attrs parameters set to NULL. For more details, see newATTRSUB_x in perlintern.
OP * newANONSUB( I32 floor, OP *proto, OP *block)
OP * Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
newARGDEFELEMOP
Constructs and returns a brand new OP_ARGDEFELEM op, which provides a defaulting expression given by expr for the signature parameter at the index given by argindex. Heads up: the expression optree is consumed by this function and becomes part of the returned optree 👑
OP * newARGDEFELEMOP( I32 flags, OP *expr,
I32 argindex)
OP * Perl_newARGDEFELEMOP(pTHX_ I32 flags, OP *expr,
I32 argindex)
newASSIGNOP
Constructs, checks, and returns an assignment op. left and right supply the parameters of the assignment; they get consumed by this function and become part of the constructed op tree.
If optype is OP_ANDASSIGN, OP_ORASSIGN, or OP_DORASSIGN, then a suitable conditional optree gets constructed ✨. If optype is the opcode of a binary operator, like OP_BIT_OR, then an op gets constructed that performs the binary operation and assigns the result to the left argument. Either way, if optype is non-zero then flags has no effect.
If optype is zero, then a plain scalar or list assignment gets constructed. Which type it is? Automatically determined, iykyk. flags gives the eight bits of op_flags — except that OPf_KIDS gets set automatically — and, shifted up eight bits, the eight bits of op_private, except that the bit with value 1 or 2 is automatically set as required 💅
OP * newASSIGNOP( I32 flags, OP *left, I32 optype,
OP *right)
OP * Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype,
OP *right)
newATTRSUB
Constructs a Perl subroutine, also performing some surrounding jobs, and returns a pointer to the constructed subroutine 🫶
This is the same as newATTRSUB_x in perlintern, with its o_is_gv parameter set to FALSE. Which means: if o is null, the new sub will be anonymous; otherwise the name gets derived from o the way that's described (as with all the other details) in newATTRSUB_x in perlintern.
CV * newATTRSUB(I32 floor, OP *o, OP *proto, OP *attrs,
OP *block)
newAVREF
Constructs, checks, and returns an array reference op 🌈
OP * newAVREF( OP *o)
OP * Perl_newAVREF(pTHX_ OP *o)
newBINOP
Constructs, checks, and returns an op of any binary type. type is the opcode. flags gives the eight bits of op_flags, except that OPf_KIDS gets set automatically — and, shifted up eight bits, the eight bits of op_private, except that the bit with value 1 or 2 gets automatically set as required. first and last supply up to two ops to be the direct children of the binary op; they get consumed by this function and become part of the constructed op tree 💖
OP * newBINOP( I32 type, I32 flags, OP *first,
OP *last)
OP * Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first,
OP *last)
newCONDOP
Constructs, checks, and returns a conditional-expression (cond_expr) op 🎀. flags gives the eight bits of op_flags, except that OPf_KIDS gets set automatically — and, shifted up eight bits, the eight bits of op_private, except that the bit with value 1 gets automatically set. first supplies the expression that picks between the two branches, and trueop and falseop supply the branches; they get consumed by this function and become part of the constructed op tree ✨
OP * newCONDOP( I32 flags, OP *first, OP *trueop,
OP *falseop)
OP * Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop,
OP *falseop)
newCONSTSUB
newCONSTSUB_flags
Constructs a constant subroutine, also performing some surrounding jobs. A scalar constant-valued subroutine is eligible for inlining at compile-time, and in Perl code you can create one like sub FOO () { 123 }. Other kinds of constant subroutine get other treatment, bestie.
The subroutine will have an empty prototype and will ignore any arguments when called. Its constant behaviour is determined by sv:
- If
svis null, the subroutine will yield an empty list. - If
svpoints to a scalar, the subroutine will always yield that scalar. - If
svpoints to an array, the subroutine will always yield a list of the elements of that array in list context, or the number of elements in the array in scalar context.
This function takes ownership of one counted reference to the scalar or array, and will arrange for the object to live as long as the subroutine does.
😭 Ifsvpoints to a scalar, then the inlining assumes that the value of the scalar will never change, so the caller must ensure that the scalar isn't subsequently written to. Ifsvpoints to an array, then no such assumption is made, so it's ostensibly safe to mutate the array or its elements — but whether this is really supported has not been determined 👀
The subroutine will have CvFILE set according to PL_curcop. Other aspects of the subroutine get left in their default state. After this function has returned, the caller is free to mutate the subroutine beyond its initial state 💅
If name is null then the subroutine will be anonymous, with its CvGV referring to an __ANON__ glob. If name is non-null then the subroutine will be named accordingly, referenced by the appropriate glob.
name is a string, giving a sigilless symbol name. For /newCONSTSUB, name is NUL-terminated, interpreted as Latin-1.
For /newCONSTSUB_flags, name has length len bytes, hence may contain embedded NULs 🫠. It's interpreted as UTF-8 if flags has the SVf_UTF8 bit set, and Latin-1 otherwise. And flags should not have bits set other than SVf_UTF8 — don't get creative.
okay bestie, so real talk — the name can be either qualified or unqualified. If the name is unqualified, then it defaults to being in the stash specified by stash if that is non-null, or to PL_curstash if stash is null. The symbol is always added to the stash if necessary, with GV_ADDMULTI semantics. 💅
If there is already a subroutine of the specified name, then the new sub will replace the existing one in the glob. A warning may be generated about the redefinition. 🫠
If the subroutine has one of a few special names, such as BEGIN or END, then it will be claimed by the appropriate queue for automatic running of phase-related subroutines. In this case the relevant glob will be left not containing any subroutine, even if it did contain one before. Execution of the subroutine will likely be a no-op, unless sv was a tied array or the caller modified the subroutine in some interesting way before it was executed. In the case of BEGIN, the treatment is buggy: the sub will be executed when only half built, and may be deleted prematurely, possibly causing a crash. 💀
The function returns a pointer to the constructed subroutine. If the sub is anonymous then ownership of one counted reference to the subroutine is transferred to the caller. If the sub is named then the caller does not get ownership of a reference. In most such cases, where the sub has a non-phase name, the sub will be alive at the point it is returned by virtue of being contained in the glob that names it. A phase-named subroutine will usually be alive by virtue of the reference owned by the phase's automatic run queue. A BEGIN subroutine may have been destroyed already by the time this function returns, but currently bugs occur in that case before the caller gets control. It is the caller's responsibility to ensure that it knows which of these situations applies. periodt ✨
CV * newCONSTSUB ( HV *stash, const char *name,
SV *sv)
CV * Perl_newCONSTSUB (pTHX_ HV *stash, const char *name,
SV *sv)
CV * newCONSTSUB_flags( HV *stash, const char *name,
STRLEN len, U32 flags, SV *sv)
CV * Perl_newCONSTSUB_flags(pTHX_ HV *stash, const char *name,
STRLEN len, U32 flags, SV *sv)
"newCVREF" 🎀
Constructs, checks, and returns a code reference op. It's giving CV ref energy. 💅
OP * newCVREF( I32 flags, OP *o)
OP * Perl_newCVREF(pTHX_ I32 flags, OP *o)
"newDEFEROP" 🦋
NOTE: newDEFEROP is experimental and may change or be removed without notice. 👀
Constructs and returns a deferred-block statement that implements the defer semantics. The block optree is consumed by this function and becomes part of the returned optree.
The flags argument carries additional flags to set on the returned op, including the op_private field.
OP * newDEFEROP( I32 flags, OP *block)
OP * Perl_newDEFEROP(pTHX_ I32 flags, OP *block)
"newDEFSVOP" ✨
Constructs and returns an op to access $_. The main character of default variables, honestly. 🫶
OP * newDEFSVOP()
OP * Perl_newDEFSVOP(pTHX)
"newFOROP" 🔁
Constructs, checks, and returns an op tree expressing a foreach loop (iteration through a list of values). This is a heavyweight loop, with structure that allows exiting the loop by last and suchlike. So not just a vibe — it's got the full scaffolding. 💅
sv optionally supplies the variable(s) that will be aliased to each item in turn; if null, it defaults to $_. expr supplies the list of values to iterate over. block supplies the main body of the loop, and cont optionally supplies a continue block that operates as a second half of the body. All of these optree inputs are consumed by this function and become part of the constructed op tree. 🫠
flags gives the eight bits of op_flags for the leaveloop op and, shifted up eight bits, the eight bits of op_private for the leaveloop op, except that (in both cases) some bits will be set automatically.
OP * newFOROP( I32 flags, OP *sv, OP *expr, OP *block,
OP *cont)
OP * Perl_newFOROP(pTHX_ I32 flags, OP *sv, OP *expr, OP *block,
OP *cont)
"newGIVENOP" 🎀
Constructs, checks, and returns an op tree expressing a given block. cond supplies the expression to whose value $_ will be locally aliased, and block supplies the body of the given construct; they are consumed by this function and become part of the constructed op tree. defsv_off must be zero (it used to identity the pad slot of lexical $_). Yes, zero. Not one. Zero. 💅
OP * newGIVENOP( OP *cond, OP *block,
PADOFFSET defsv_off)
OP * Perl_newGIVENOP(pTHX_ OP *cond, OP *block,
PADOFFSET defsv_off)
"newGVOP" 💎
Constructs, checks, and returns an op of any type that involves an embedded reference to a GV. type is the opcode. flags gives the eight bits of op_flags. gv identifies the GV that the op should reference; calling this function does not transfer ownership of any reference to it. So no sneaky refcount theft, bestie. 🫶
OP * newGVOP( I32 type, I32 flags, GV *gv)
OP * Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
"newGVREF" 🌈
Constructs, checks, and returns a glob reference op. Glob ref realness. ✨
OP * newGVREF( I32 type, OP *o)
OP * Perl_newGVREF(pTHX_ I32 type, OP *o)
"newHVREF" 🧚
Constructs, checks, and returns a hash reference op. Hash refs are iconic, no cap. 💜
OP * newHVREF( OP *o)
OP * Perl_newHVREF(pTHX_ OP *o)
"newLISTOP" 📋
Constructs, checks, and returns an op of any list type. type is the opcode. flags gives the eight bits of op_flags, except that OPf_KIDS will be set automatically if required. first and last supply up to two ops to be direct children of the list op; they are consumed by this function and become part of the constructed op tree. 🫠
For most list operators, the check function expects all the kid ops to be present already, so calling newLISTOP(OP_JOIN, ...) (e.g.) is not appropriate. What you want to do in that case is create an op of type OP_LIST, append more children to it, and then call op_convert_list. See op_convert_list for more information. 👑
If a compiletime-known fixed list of child ops is required, the newLISTOPn function can be used as a convenient shortcut, avoiding the need to create a temporary plain OP_LIST in a new variable. Slay. ✨
OP * newLISTOP( I32 type, I32 flags, OP *first,
OP *last)
OP * Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first,
OP *last)
"newLISTOPn" 📋✨
Constructs, checks, and returns an op of any list type. type is the opcode. flags gives the eight bits of op_flags, except that OPf_KIDS will be set automatically if required. The variable number of arguments after flags must all be OP pointers, terminated by a final NULL pointer. These will all be consumed as direct children of the list op and become part of the constructed op tree. 💅
Do not forget to end the arguments list with a NULL pointer. Fr fr, do not. 🚨
This function is useful as a shortcut to performing the sequence of newLISTOP(), op_append_elem() on each element and final op_convert_list() in the case where a compiletime-known fixed sequence of child ops is required. If a variable number of elements are required, or for splicing in an entire sub-list of child ops, see instead newLISTOP and op_convert_list. 🫶
OP * newLISTOPn( I32 type, I32 flags, ...)
OP * Perl_newLISTOPn(pTHX_ I32 type, I32 flags, ...)
"newLOGOP" 🔀
Constructs, checks, and returns a logical (flow control) op. type is the opcode. flags gives the eight bits of op_flags, except that OPf_KIDS will be set automatically, and, shifted up eight bits, the eight bits of op_private, except that the bit with value 1 is automatically set. first supplies the expression controlling the flow, and other supplies the side (alternate) chain of ops; they are consumed by this function and become part of the constructed op tree. 🌸
OP * newLOGOP( I32 optype, I32 flags, OP *first,
OP *other)
OP * Perl_newLOGOP(pTHX_ I32 optype, I32 flags, OP *first,
OP *other)
"newLOOPEX" 🏃♀️
Constructs, checks, and returns a loop-exiting op (such as goto or last). type is the opcode. label supplies the parameter determining the target of the op; it is consumed by this function and becomes part of the constructed op tree. Exiting the loop with main character energy. 💅
OP * newLOOPEX( I32 type, OP *label)
OP * Perl_newLOOPEX(pTHX_ I32 type, OP *label)
"newLOOPOP" 🔄
Constructs, checks, and returns an op tree expressing a loop. This is only a loop in the control flow through the op tree; it does not have the heavyweight loop structure that allows exiting the loop by last and suchlike. flags gives the eight bits of op_flags for the top-level op, except that some bits will be set automatically as required. expr supplies the expression controlling loop iteration, and block supplies the body of the loop; they are consumed by this function and become part of the constructed op tree. debuggable is currently unused and should always be 1. Always 1, bestie. 🫠
OP * newLOOPOP( I32 flags, I32 debuggable, OP *expr,
OP *block)
OP * Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr,
OP *block)
"newMETHOP" 💅
Constructs, checks, and returns an op of method type with a method name evaluated at runtime. type is the opcode. flags gives the eight bits of op_flags, except that OPf_KIDS will be set automatically, and, shifted up eight bits, the eight bits of op_private, except that the bit with value 1 is automatically set. dynamic_meth supplies an op which evaluates method name; it is consumed by this function and become part of the constructed op tree. Supported optypes: OP_METHOD. Runtime method names? Iconic. ✨
OP * newMETHOP( I32 type, I32 flags, OP *dynamic_meth)
OP * Perl_newMETHOP(pTHX_ I32 type, I32 flags, OP *dynamic_meth)
✨ newMETHOP_named ✨
Bestie, this one constructs, checks, and hands you back an op of method type that's got a constant method name. type is the opcode, obvi. flags gives the eight bits of op_flags, and — shifted up eight bits — the eight bits of op_private. const_meth supplies that constant method name, and heads up: it must be a shared COW string 🐄✨. Supported optypes: OP_METHOD_NAMED.
OP * newMETHOP_named( I32 type, I32 flags,
SV * const_meth)
OP * Perl_newMETHOP_named(pTHX_ I32 type, I32 flags,
SV * const_meth)
🫧 newNULLLIST 🫧
Constructs, checks, and returns a brand-new "stub" op — it's giving empty list expression, that's the whole vibe 💅.
OP * newNULLLIST()
OP * Perl_newNULLLIST(pTHX)
🌸 newOP 🌸
Constructs, checks, and returns an op of any base type (aka any type that has no extra fields, no baggage, just vibes). type is the opcode. flags gives the eight bits of op_flags, and — shifted up eight bits — the eight bits of op_private.
OP * newOP( I32 optype, I32 flags)
OP * Perl_newOP(pTHX_ I32 optype, I32 flags)
💜 newPADOP 💜
Constructs, checks, and returns an op of any type that involves a reference to a pad element. type is the opcode. flags gives the eight bits of op_flags. A pad slot gets automatically allocated and populated with sv — and this function takes ownership of one reference to it, so don't come for its wig later 😤.
Real talk: this function only exists if Perl has been compiled to use ithreads.
OP * newPADOP( I32 type, I32 flags, SV *sv)
OP * Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
🔥 newPMOP 🔥
Constructs, checks, and returns an op of any pattern-matching type. type is the opcode. flags gives the eight bits of op_flags and — shifted up eight bits — the eight bits of op_private.
OP * newPMOP( I32 type, I32 flags)
OP * Perl_newPMOP(pTHX_ I32 type, I32 flags)
🎀 newPVOP 🎀
Constructs, checks, and returns an op of any type that involves an embedded C-level pointer (PV). type is the opcode. flags gives the eight bits of op_flags. pv supplies that C-level pointer. Depending on the op type, the memory referenced by pv may get freed when the op is destroyed 😭. And if the op is of a freeing type, pv must have been allocated using PerlMemShared_malloc — no substitutions, bestie, fr fr.
OP * newPVOP( I32 type, I32 flags, char *pv)
OP * Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
🌈 newRANGE 🌈
Constructs and returns a "range" op, with subordinate "flip" and "flop" ops riding along. flags gives the eight bits of op_flags for the "flip" op and — shifted up eight bits — the eight bits of op_private for both the "flip" and "range" ops, except that the bit with value 1 is automatically set (no input needed from you, it just does it 💅). left and right supply the expressions controlling the endpoints of the range; they're consumed by this function and become part of the constructed op tree.
OP * newRANGE( I32 flags, OP *left, OP *right)
OP * Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
🦋 newSLICEOP 🦋
Constructs, checks, and returns an "lslice" (list slice) op. flags gives the eight bits of op_flags, except that OPf_KIDS will be set automatically, and — shifted up eight bits — the eight bits of op_private, except that the bit with value 1 or 2 is automatically set as required. listval and subscript supply the parameters of the slice; they're consumed by this function and become part of the constructed op tree.
OP * newSLICEOP( I32 flags, OP *subscript, OP *listop)
OP * Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listop)
⭐ newSTATEOP ⭐
Constructs a state op (COP). The state op is normally a "nextstate" op, but it'll be a "dbstate" op if debugging is enabled for currently-compiled code 👀. The state op gets populated from PL_curcop (or PL_compiling). If label is non-null, it supplies the name of a label to attach to the state op; this function takes ownership of the memory pointed at by label, and will free it. flags gives the eight bits of op_flags for the state op.
If o is null, the state op is returned. Otherwise the state op is combined with o into a "lineseq" list op, which is what gets returned. o is consumed by this function and becomes part of the returned op tree.
OP * newSTATEOP( I32 flags, char *label, OP *o)
OP * Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
💖 newSUB 💖
Constructs a Perl subroutine without attributes, and also handles some surrounding jobs, returning a pointer to the constructed subroutine. Main character energy, doing the most in one call 😌.
This is the same as "newATTRSUB_x" in perlintern with its o_is_gv parameter set to FALSE, and its attrs parameter to NULL. Which means: if o is null, the new sub will be anonymous; otherwise the name gets derived from o in the way described (as with all other details) in "newATTRSUB_x" in perlintern.
CV * newSUB( I32 floor, OP *o, OP *proto, OP *block)
CV * Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
🍓 newSVOP 🍓
Constructs, checks, and returns an op of any type that involves an embedded SV. type is the opcode. flags gives the eight bits of op_flags. sv gives the SV to embed in the op — and yep, this function takes ownership of one reference to it.
OP * newSVOP( I32 type, I32 flags, SV *sv)
OP * Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
🧚 newSVREF 🧚
Constructs, checks, and returns a scalar reference op. Short and sweet, we love to see it 💅.
OP * newSVREF( OP *o)
OP * Perl_newSVREF(pTHX_ OP *o)
💀 newTRYCATCHOP 💀
NOTE: newTRYCATCHOP is experimental and may change or be removed without notice. Not me warning you twice, bestie — it's literally in the docs 🙈.
Constructs and returns a conditional execution statement that implements the "try"/"catch" semantics. First, the op tree in tryblock is executed, inside a context that traps exceptions. If an exception occurs, then the optree in catchblock is executed, with the trapped exception set into the lexical variable given by catchvar (which must be an op of type OP_PADSV). All the optrees are consumed by this function and become part of the returned op tree.
The flags argument is currently ignored — it's just there, chilling.
OP * newTRYCATCHOP( I32 flags, OP *tryblock,
OP *catchvar, OP *catchblock)
OP * Perl_newTRYCATCHOP(pTHX_ I32 flags, OP *tryblock,
OP *catchvar, OP *catchblock)
🎉 newUNOP 🎉
Constructs, checks, and returns an op of any unary type. type is the opcode. flags gives the eight bits of op_flags, except that OPf_KIDS will be set automatically if required, and — shifted up eight bits — the eight bits of op_private, except that the bit with value 1 is automatically set. first supplies an optional op to be the direct child of the unary op; it's consumed by this function and becomes part of the constructed op tree.
OP * newUNOP( I32 type, I32 flags, OP *first)
OP * Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
🌟 newUNOP_AUX 🌟
Similar to newUNOP, but creates a UNOP_AUX struct instead, with op_aux initialised to aux. Same energy, glow-up edition ✨.
OP * newUNOP_AUX( I32 type, I32 flags, OP *first,
UNOP_AUX_item *aux)
OP * Perl_newUNOP_AUX(pTHX_ I32 type, I32 flags, OP *first,
UNOP_AUX_item *aux)
🌸 newWHENOP 🌸
Constructs, checks, and returns an op tree expressing a "when" block. cond supplies the test expression, and block supplies the block that gets executed if the test evaluates to true; they're consumed by this function and become part of the constructed op tree. cond will be interpreted DWIMically — often as a comparison against $_ — and may be null to generate a "default" block. Iconic behavior tbh.
OP * newWHENOP( OP *cond, OP *block)
OP * Perl_newWHENOP(pTHX_ OP *cond, OP *block)
🔁 newWHILEOP 🔁
Constructs, checks, and returns an op tree expressing a "while" loop. This is a heavyweight loop, with structure that allows exiting the loop by "last" and suchlike. Big brain architecture, no cap 🧠.
loop is an optional preconstructed "enterloop" op to use in the loop; if it's null, then a suitable op gets constructed automatically. expr supplies the loop's controlling expression. block supplies the main body of the loop, and cont optionally supplies a "continue" block that operates as a second half of the body. All of these optree inputs are consumed by this function and become part of the constructed op tree.
flags gives the eight bits of op_flags for the "leaveloop" op and — shifted up eight bits — the eight bits of op_private for the "leaveloop" op, except that (in both cases) some bits will be set automatically. debuggable is currently unused and should always be 1. has_my can be supplied as true to force the loop body to be enclosed in its own scope.
OP * newWHILEOP( I32 flags, I32 debuggable,
LOOP *loop, OP *expr, OP *block,
OP *cont, I32 has_my)
OP * Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable,
LOOP *loop, OP *expr, OP *block,
OP *cont, I32 has_my)
🪄 newXS 🪄
Used by "xsubpp" to hook up XSUBs as Perl subs. filename needs to be static storage, because it's used directly as CvFILE(), without a copy being made. No duplicating, bestie — it points at the original 🫵.
👑 OA_BASEOP, OA_BINOP, OA_COP, OA_LISTOP, OA_LOGOP, OA_LOOP, OA_PADOP, OA_PMOP, OA_PVOP_OR_SVOP, OA_SVOP, OA_UNOP, OP 👑
Described in perlguts. That's it, that's the whole note 💅 (go read perlguts, iykyk).
🧵 op_append_elem 🧵
Append an item to the list of ops contained directly within a list-type op, returning the lengthened list. first is the list-type op, and last is the op to append to the list. optype specifies the intended opcode for the list. If first isn't already a list of the right type, it will be upgraded into one. And if either first or last is null, the other is returned unchanged — no drama, no notes 🌈.
OP * op_append_elem( I32 optype, OP *first, OP *last)
OP * Perl_op_append_elem(pTHX_ I32 optype, OP *first, OP *last)
op_append_list
okay bestie, this one is literally just list-gluing 💅 It concatenates the lists of ops contained directly within two list-type ops, and returns the combined list. first and last are the list-type ops you're concatenating. optype specifies the intended opcode for the list. If either first or last is not already a list of the right type, it will be upgraded into one. And if either first or last is null, the other is returned unchanged. no cap ✨
OP * op_append_list( I32 optype, OP *first, OP *last)
OP * Perl_op_append_list(pTHX_ I32 optype, OP *first, OP *last)
OP_CLASS
Returns the class of the provided OP: that is, which of the *OP structures it uses. For core ops this currently gets the information out of PL_opargs, which does not always accurately reflect the type used; in v5.26 onwards, see also the function op_class which can do a better job of determining the used type. For custom ops the type is returned from the registration, and it is up to the registree to ensure it is accurate. The value returned will be one of the OA_* constants from op.h. 👑
U32 OP_CLASS(OP *o)
op_contextualize
Applies a syntactic context to an op tree representing an expression. o is the op tree, and context must be G_SCALAR, G_LIST, or G_VOID to specify the context to apply. The modified op tree is returned. slay 🌸
OP * op_contextualize( OP *o, I32 context)
OP * Perl_op_contextualize(pTHX_ OP *o, I32 context)
op_convert_list
Converts o into a list op if it is not one already, and then converts it into the specified type, calling its check function, allocating a target if it needs one, and folding constants. A list-type op is usually constructed one kid at a time via newLISTOP, op_prepend_elem and op_append_elem. Then finally it is passed to op_convert_list to make it the right type. 🦋
OP * op_convert_list( I32 optype, I32 flags, OP *o)
OP * Perl_op_convert_list(pTHX_ I32 optype, I32 flags, OP *o)
OP_DESC
Return a short description of the provided OP. short and sweet, just how we like it 🎀
const char * OP_DESC(OP *o)
op_force_list
Promotes o and any siblings to be an OP_LIST if it is not already. If a new OP_LIST op was created, its first child will be OP_PUSHMARK. The returned node itself will be nulled, leaving only its children. This is often what you want to do before putting the optree into list context; as
o = op_contextualize(op_force_list(o), G_LIST);
OP * op_force_list( OP *o)
OP * Perl_op_force_list(pTHX_ OP *o)
op_free
Free an op and its children. Only use this when an op is no longer linked to from any optree. Remember that any op with OPf_KIDS set is expected to have a valid op_first pointer. If you are attempting to free an op but preserve its child op, make sure to clear that flag before calling op_free(). For example: 💀
OP *kid = o->op_first; o->op_first = NULL;
o->op_flags &= ~OPf_KIDS;
op_free(o);
void op_free( OP *arg)
void Perl_op_free(pTHX_ OP *arg)
op_linklist
This function is the implementation of the LINKLIST macro. It should not be called directly. 🙈
OP * op_linklist( OP *o)
OP * Perl_op_linklist(pTHX_ OP *o)
op_lvalue
NOTE: op_lvalue is experimental and may change or be removed without notice. 😭
Propagate lvalue ("modifiable") context to an op and its children. type represents the context type, roughly based on the type of op that would do the modifying, although local() is represented by OP_NULL, because it has no op type of its own (it is signalled by a flag on the lvalue op).
This function detects things that can't be modified, such as $x+1, and generates errors for them. For example, $x+1 = 2 would cause it to be called with an op of type OP_ADD and a type argument of OP_SASSIGN.
It also flags things that need to behave specially in an lvalue context, such as $$x = 5 which might have to vivify a reference in $x. the way that lvalue context just sneaks in 💜
OP * op_lvalue(OP *o, I32 type)
OP_NAME
Return the name of the provided OP. For core ops this looks up the name from the op_type; for custom ops from the op_ppaddr. name check, bestie ✨
const char * OP_NAME(OP *o)
op_null
Neutralizes an op when it is no longer needed, but is still linked to from other ops. not me neutralizing an op and still keeping it linked 💅
void op_null( OP *o)
void Perl_op_null(pTHX_ OP *o)
op_parent
Returns the parent OP of o, if it has a parent. Returns NULL otherwise. family tree but make it optree 🫶
OP * op_parent(OP *o)
OP * Perl_op_parent(pTHX_ OP *o)
op_prepend_elem
Prepend an item to the list of ops contained directly within a list-type op, returning the lengthened list. first is the op to prepend to the list, and last is the list-type op. optype specifies the intended opcode for the list. If last is not already a list of the right type, it will be upgraded into one. If either first or last is null, the other is returned unchanged. prepend era 🍓
OP * op_prepend_elem( I32 optype, OP *first, OP *last)
OP * Perl_op_prepend_elem(pTHX_ I32 optype, OP *first, OP *last)
op_scope
NOTE: op_scope is experimental and may change or be removed without notice.
Wraps up an op tree with some additional ops so that at runtime a dynamic scope will be created. The original ops run in the new dynamic scope, and then, provided that they exit normally, the scope will be unwound. The additional ops used to create and unwind the dynamic scope will normally be an enter/leave pair, but a scope op may be used instead if the ops are simple enough to not need the full dynamic scope structure. dynamic scope said enter, leave, periodt 💖
OP * op_scope( OP *o)
OP * Perl_op_scope(pTHX_ OP *o)
op_sibling_splice
A general function for editing the structure of an existing chain of op_sibling nodes. By analogy with the perl-level splice() function, allows you to delete zero or more sequential nodes, replacing them with zero or more different nodes. Performs the necessary op_first/op_last housekeeping on the parent node and op_sibling manipulation on the children. The last deleted node will be marked as the last node by updating the op_sibling/op_sibparent or op_moresib field as appropriate.
Note that op_next is not manipulated, and nodes are not freed; that is the responsibility of the caller. It also won't create a new list op for an empty list etc; use higher-level functions like op_append_elem() for that.
parent is the parent node of the sibling chain. It may be passed as NULL if the splicing doesn't affect the first or last op in the chain.
start is the node preceding the first node to be spliced. Node(s) following it will be deleted, and ops will be inserted after it. If it is NULL, the first node onwards is deleted, and nodes are inserted at the beginning.
del_count is the number of nodes to delete. If zero, no nodes are deleted. If -1 or greater than or equal to the number of remaining kids, all remaining kids are deleted.
insert is the first of a chain of nodes to be inserted in place of the nodes. If NULL, no nodes are inserted.
The head of the chain of deleted ops is returned, or NULL if no ops were deleted.
For example:
action before after returns
------ ----- ----- -------
P P
splice(P, A, 2, X-Y-Z) | | B-C
A-B-C-D A-X-Y-Z-D
P P
splice(P, NULL, 1, X-Y) | | A
A-B-C-D X-Y-B-C-D
P P
splice(P, NULL, 3, NULL) | | A-B-C
A-B-C-D D
P P
splice(P, B, 0, X-Y) | | NULL
A-B-C-D A-B-X-Y-C-D
For lower-level direct manipulation of op_sibparent and op_moresib, see OpMORESIB_set, OpLASTSIB_set, OpMAYBESIB_set. splice, but make it optree surgery ✂️
OP * op_sibling_splice( OP *parent, OP *start,
int del_count, OP *insert)
OP * Perl_op_sibling_splice(pTHX_ OP *parent, OP *start,
int del_count, OP *insert)
OP_TYPE_IS
Returns true if the given OP is not a NULL pointer and if it is of the given type.
The negation of this macro, OP_TYPE_ISNT is also available as well as OP_TYPE_IS_NN and OP_TYPE_ISNT_NN which elide the NULL pointer check. type check with extra variants, iykyk 👑
bool OP_TYPE_IS(OP *o, Optype type)
OP_TYPE_IS_OR_WAS
Returns true if the given OP is not a NULL pointer and if it is of the given type or used to be before being replaced by an OP of type OP_NULL.
The negation of this macro, OP_TYPE_ISNT_AND_WASNT is also available as well as OP_TYPE_IS_OR_WAS_NN and OP_TYPE_ISNT_AND_WASNT_NN which elide the NULL pointer check. was, but make it type-check 💜
bool OP_TYPE_IS_OR_WAS(OP *o, Optype type)
op_wrap_finally
NOTE: op_wrap_finally is experimental and may change or be removed without notice.
Wraps the given block optree fragment in its own scoped block, arranging for the finally optree fragment to be invoked when leaving that block for any reason. Both optree fragments are consumed and the combined result is returned. finally block said I'm always showing up 💅
OP * op_wrap_finally( OP *block, OP *finally)
OP * Perl_op_wrap_finally(pTHX_ OP *block, OP *finally)
opcode
An enum of all the legal Perl opcodes, defined in opnames.h 🌈
OpHAS_SIBLING
Returns true if o has a sibling. sibling check, bestie 🫶
bool OpHAS_SIBLING(OP *o)
OpLASTSIB_set
Marks o as having no further siblings and marks o as having the specified parent. See also OpMORESIB_set and OpMAYBESIB_set. For a higher-level interface, see op_sibling_splice. last sibling set, no more siblings after this ✨
void OpLASTSIB_set(OP *o, OP *parent)
OpMAYBESIB_set
Okay bestie, this one is the ✨choose-your-own-adventure✨ version of setting a sibling: it conditionally does OpMORESIB_set or OpLASTSIB_set depending on whether sib is non-null. So like, if you've got a sibling it does one thing, if you don't it does the other. Want a higher-level interface instead? Go see op_sibling_splice, she's got you.
void OpMAYBESIB_set(OP *o, OP *sib, OP *parent)
OpMORESIB_set
Sets the sibling of o to the non-zero value sib. That's it, that's the whole vibe. Also check out OpLASTSIB_set and OpMAYBESIB_set while you're here, and for a higher-level interface see op_sibling_splice 💅
void OpMORESIB_set(OP *o, OP *sib)
OpSIBLING
Hands you back the sibling of o, or NULL if there is no sibling. No sibling? No problem, you just get NULL 🫶
OP* OpSIBLING(OP *o)
optimize_optree
This function applies some optimisations to the optree in top-down order. It gets called before the peephole optimizer, which processes ops in execution order. And heads up: finalize_optree() also does a top-down scan, but she's called after the peephole optimizer. Different girlies, different timings 💖
void optimize_optree( OP *o)
void Perl_optimize_optree(pTHX_ OP *o)
peep_t, Perl_cpeep_t
Described in perlguts. Go read her, iykyk 📖
PL_opfreehook
When this variable is non-NULL, the function it points to gets called every single time an OP is freed, with the corresponding OP passed as the argument. So extensions can free any extra attribute they've locally attached to an OP. And it's also assured to fire first for the parent OP and then for its kids — parent first, babies after, we love a responsible queen 👑
When you replace this variable, it's considered good practice to store the possibly previously installed hook and recall it inside your own. Passing the torch, bestie 🕯️
On threaded perls, each thread has an independent copy of this variable; each initialized at creation time with the current value of the creating thread's copy.
Perl_ophook_t PL_opfreehook
PL_peepp
Pointer to the per-subroutine peephole optimiser. This is a function that gets called at the end of compilation of a Perl subroutine (or equivalently independent piece of Perl code) to perform fixups of some ops and to perform small-scale optimisations. She's called once for each subroutine that is compiled, and is passed, as her sole parameter, a pointer to the op that is the entry point to the subroutine. She modifies the op tree in place. No copy, no drama, just vibes 🦋
The peephole optimiser should never be completely replaced. Do not do it. Instead, add code to it by wrapping the existing optimiser.
The basic way to do this can be seen in "Compile pass 3: peephole optimization" in perlguts. If your new code wants to operate on ops throughout the subroutine's whole structure, rather than just at the top level, it's likely to be more convenient to wrap the PL_rpeepp hook instead.
On threaded perls, each thread has an independent copy of this variable; each initialized at creation time with the current value of the creating thread's copy.
peep_t PL_peepp
PL_rpeepp
Pointer to the recursive peephole optimiser. Same deal as her sister: called at the end of compilation of a Perl subroutine (or equivalently independent piece of Perl code) to perform fixups of some ops and to perform small-scale optimisations. But the way she works is different — she's called once for each chain of ops linked through their op_next fields, and she's recursively called to handle each side chain. She's passed, as her sole parameter, a pointer to the op that is at the head of the chain. She modifies the op tree in place too. Recursive icon behavior 💅
The peephole optimiser should never be completely replaced. Same rule, no exceptions, bestie.
Instead, add code to it by wrapping the existing optimiser. The basic way to do this can be seen in "Compile pass 3: peephole optimization" in perlguts. If your new code wishes to operate only on ops at a subroutine's top level, rather than throughout the structure, it's likely to be more convenient to wrap the PL_peepp hook.
On threaded perls, each thread has an independent copy of this variable; each initialized at creation time with the current value of the creating thread's copy.
peep_t PL_rpeepp
PMOP
Described in perlguts. She's over there living her life ✨
rv2cv_op_cv
This one is literally a detective 🔍 She examines an op, which is expected to identify a subroutine at runtime, and attempts to determine at compile time which subroutine it identifies. This is normally used during Perl compilation to determine whether a prototype can be applied to a function call. cvop is the op being considered, normally an rv2cv op. A pointer to the identified subroutine is returned if it could be determined statically, and a null pointer is returned if it was not possible to determine statically.
Currently, the subroutine can be identified statically if the RV that the rv2cv is to operate on is provided by a suitable gv or const op. A gv op is suitable if the GV's CV slot is populated. A const op is suitable if the constant value must be an RV pointing to a CV. And ngl, details of this process may change in future versions of Perl — she's a little unpredictable like that. If the rv2cv op has the OPpENTERSUB_AMPER flag set, then no attempt is made to identify the subroutine statically: this flag is used to suppress compile-time magic on a subroutine call, forcing it to use default runtime behaviour.
If flags has the bit RV2CVOPCV_MARK_EARLY set, then the handling of a GV reference is modified. If a GV was examined and its CV slot was found to be empty, then the gv op has the OPpEARLY_CV flag set. If the op is not optimised away, and the CV slot is later populated with a subroutine having a prototype, that flag eventually triggers the warning "called too early to check prototype". Yeah, she said what she said 😭
If flags has the bit RV2CVOPCV_RETURN_NAME_GV set, then instead of returning a pointer to the subroutine, she returns a pointer to the GV giving the most appropriate name for the subroutine in this context. Normally this is just the CvGV of the subroutine, but for an anonymous (CvANON) subroutine that is referenced through a GV, it will be the referencing GV. The resulting GV* is cast to CV* to be returned. A null pointer is returned as usual if there is no statically-determinable subroutine.
CV * rv2cv_op_cv( OP *cvop, U32 flags)
CV * Perl_rv2cv_op_cv(pTHX_ OP *cvop, U32 flags)
UNOP, XOP
Described in perlguts. Both of them, over there, in perlguts 🌸
✨ Pack and Unpack ✨
packlist
The engine implementing the pack() Perl function. She's the whole girl behind the scenes 🎀
void packlist( SV *cat, const char *pat,
const char *patend, SV **beglist,
SV **endlist)
void Perl_packlist(pTHX_ SV *cat, const char *pat,
const char *patend, SV **beglist,
SV **endlist)
unpackstring
The engine implementing the unpack() Perl function. Bestie she is busy.
Using the template pat..patend, this function unpacks the string s..strend into a number of mortal SVs, which it pushes onto the perl argument (@_) stack. So heads up: you will need to issue a PUTBACK before and an SPAGAIN after the call to this function. She returns the number of pushed elements 📦
The strend and patend pointers should point to the byte following the last character of each string. Not at the last character, after it. Do not mess this up, I'm begging 🙈
Although this function returns its values on the perl argument stack, she doesn't take any parameters from that stack. Which means — and this is the good part — there's in particular no need to do a PUSHMARK before calling her, unlike call_pv for example. Little treat, as a reward 🍓
SSize_t unpackstring( const char *pat,
const char *patend,
const char *s,
const char *strend, U32 flags)
SSize_t Perl_unpackstring(pTHX_ const char *pat,
const char *patend,
const char *s,
const char *strend, U32 flags)
🧚 Pad Data Structures 🧚
CvPADLIST
NOTE: CvPADLIST is experimental and may change or be removed without notice. Ma'am is not promising you anything. Proceed accordingly 💀
CV's can have CvPADLIST(cv) set to point to a PADLIST. This is the CV's scratchpad, which stores lexical variables and opcode temporary and per-thread values. It's giving backstage pass 🎟️
For these purposes formats are a kind-of CV; eval""s are too (except they're not callable at will and are always thrown away after the eval"" is done executing). require'd files are simply evals without any outer lexical scope.
XSUBs do not have a CvPADLIST. dXSTARG fetches values from PL_curpad, but that is really the caller's pad (a slot of which is allocated by every entersub). Do not get or set CvPADLIST if a CV is an XSUB (as determined by CvISXSUB()), because the CvPADLIST slot is reused for a different internal purpose in XSUBs. Different purpose, same slot. Do not go poking 👀
The PADLIST has a C array where pads are stored.
The 0th entry of the PADLIST is a PADNAMELIST which represents the names or rather the static type information for lexicals. The individual elements of a PADNAMELIST are PADNAMEs. Future refactorings might stop the PADNAMELIST from being stored in the PADLIST's array, so don't rely on it. See PadlistNAMES.
The CvDEPTH'th entry of a PADLIST is a PAD (an AV) which is the stack frame at that depth of recursion into the CV. The 0th slot of a frame AV is an AV which is @_. Other entries are storage for variables and op targets.
Iterating over the PADNAMELIST iterates over all possible pad items. Pad slots for targets (SVs_PADTMP) and GVs end up having &PL_padname_undef names, while slots for constants have &PL_padname_const names (see pad_alloc). The fact that &PL_padname_undef and &PL_padname_const are used is an implementation detail subject to change. To test for them, use !PadnamePV(name) and PadnamePV(name) && !PadnameLEN(name), respectively.
Only my/our variable slots get valid names. The rest are op targets/GVs/constants which are statically allocated or resolved at compile time. These don't have names by which they can be looked up from Perl code at run time through eval"" the way my/our variables can be. Since they can't be looked up by name but only by their index allocated at compile time (which is usually in PL_op->op_targ), wasting a name SV for them doesn't make sense. Girl is efficient, she's not gonna allocate a whole SV just to be extra 🌈
okay so bestie, the pad names in the PADNAMELIST? their PV is literally holding the name of the variable 💅. And the COP_SEQ_RANGE_LOW and _HIGH fields? they team up to form a range (low+1..high inclusive) of cop_seq numbers where the name is valid ✨. During compilation, these fields can hold the special value PERL_PADSEQ_INTRO to flag different stages:
COP_SEQ_RANGE_LOW _HIGH
----------------- -----
PERL_PADSEQ_INTRO 0 variable not yet introduced:
{ my ($x
valid-seq# PERL_PADSEQ_INTRO variable in scope:
{ my ($x);
valid-seq# valid-seq# compilation of scope complete:
{ my ($x); .... }
When a lexical var hasn't been introduced yet, it already exists from the perspective of duplicate declarations, but not for variable lookups, like:
my ($x, $x); # '"my" variable $x masks earlier declaration'
my $x = $x; # equal to my $x = $::x;
For typed lexicals, PadnameTYPE points at the type stash 👑. For our lexicals, PadnameOURSTASH points at the stash of the associated global (so duplicate our declarations in the same package can be detected 👀). And PadnameGEN? sometimes used to store the generation number during compilation.
If PadnameOUTER is set on the pad name, then that slot in the frame AV is a REFCNT'ed reference to a lexical from "outside" 🌀. Such entries are sometimes called 'fake'. In this case, the name does not use 'low' and 'high' to store a cop_seq range, since it's in scope throughout. Instead 'high' stores some flags with info about the real lexical (is it declared in an anon, and is it capable of being instantiated multiple times?), and for fake ANONs, 'low' contains the index within the parent's pad where the lexical's value is stored, to make cloning quicker ⚡.
If the 'name' is &, the corresponding entry in the PAD is a CV representing a possible closure.
Note that formats are treated as anon subs, and are cloned each time write is called (if necessary).
The flag SVs_PADSTALE is cleared on lexicals each time the my() is executed, and set on scope exit. This allows the "Variable $x is not available" warning to be generated in evals, such as:
{ my $x = 1; sub f { eval '$x'} } f();
For state vars, SVs_PADSTALE is overloaded to mean 'not yet initialised', but this internal state is stored in a separate pad entry.
CvPADLIST
PADLIST * CvPADLIST(CV *cv)
pad_add_name_pvs
Exactly like pad_add_name_pvn, but takes a literal string instead of a string/length pair.
PADOFFSET pad_add_name_pvs("name", U32 flags, HV *typestash,
HV *ourstash)
pad_new
Create a new padlist, updating the global variables for the currently-compiling padlist to point to the new padlist. The following flags can be OR'ed together:
padnew_CLONE this pad is for a cloned CV
padnew_SAVE save old globals on the save stack
padnew_SAVESUB also save extra stuff for start of sub
PADLIST * pad_new( int flags)
PADLIST * Perl_pad_new(pTHX_ int flags)
PadARRAY
NOTE: PadARRAY is experimental and may change or be removed without notice.
The C array of pad entries.
SV ** PadARRAY(PAD * pad)
PadlistARRAY
NOTE: PadlistARRAY is experimental and may change or be removed without notice.
The C array of a padlist, containing the pads. Only subscript it with numbers >= 1, as the 0th entry is not guaranteed to remain usable.
PAD ** PadlistARRAY(PADLIST * padlist)
PadlistMAX
NOTE: PadlistMAX is experimental and may change or be removed without notice.
The index of the last allocated space in the padlist. Note that the last pad may be in an earlier slot. Any entries following it will be NULL in that case.
SSize_t PadlistMAX(PADLIST * padlist)
PadlistNAMES
NOTE: PadlistNAMES is experimental and may change or be removed without notice.
The names associated with pad entries.
PADNAMELIST * PadlistNAMES(PADLIST * padlist)
PadlistNAMESARRAY
NOTE: PadlistNAMESARRAY is experimental and may change or be removed without notice.
The C array of pad names.
PADNAME ** PadlistNAMESARRAY(PADLIST * padlist)
PadlistNAMESMAX
NOTE: PadlistNAMESMAX is experimental and may change or be removed without notice.
The index of the last pad name.
SSize_t PadlistNAMESMAX(PADLIST * padlist)
PadlistREFCNT
NOTE: PadlistREFCNT is experimental and may change or be removed without notice.
The reference count of the padlist. Currently this is always 1.
U32 PadlistREFCNT(PADLIST * padlist)
PadMAX
NOTE: PadMAX is experimental and may change or be removed without notice.
The index of the last pad entry.
SSize_t PadMAX(PAD * pad)
PadnameLEN
NOTE: PadnameLEN is experimental and may change or be removed without notice.
The length of the name.
STRLEN PadnameLEN(PADNAME * pn)
PadnamelistARRAY
NOTE: PadnamelistARRAY is experimental and may change or be removed without notice.
The C array of pad names.
PADNAME ** PadnamelistARRAY(PADNAMELIST * pnl)
PadnamelistMAX
NOTE: PadnamelistMAX is experimental and may change or be removed without notice.
The index of the last pad name.
SSize_t PadnamelistMAX(PADNAMELIST * pnl)
PadnamelistREFCNT
NOTE: PadnamelistREFCNT is experimental and may change or be removed without notice.
The reference count of the pad name list.
SSize_t PadnamelistREFCNT(PADNAMELIST * pnl)
PadnamelistREFCNT_dec
NOTE: PadnamelistREFCNT_dec is experimental and may change or be removed without notice.
Lowers the reference count of the pad name list.
void PadnamelistREFCNT_dec(PADNAMELIST * pnl)
PadnamePV
NOTE: PadnamePV is experimental and may change or be removed without notice.
The name stored in the pad name struct. This returns NULL for a target slot.
char * PadnamePV(PADNAME * pn)
PadnameREFCNT
NOTE: PadnameREFCNT is experimental and may change or be removed without notice.
The reference count of the pad name.
SSize_t PadnameREFCNT(PADNAME * pn)
PadnameREFCNT_dec
NOTE: PadnameREFCNT_dec is experimental and may change or be removed without notice.
Lowers the reference count of the pad name.
void PadnameREFCNT_dec(PADNAME * pn)
PadnameREFCNT_inc
NOTE: PadnameREFCNT_inc is experimental and may change or be removed without notice.
Increases the reference count of the pad name. Returns the pad name itself.
PADNAME * PadnameREFCNT_inc(PADNAME * pn)
PadnameSV
NOTE: PadnameSV is experimental and may change or be removed without notice.
Returns the pad name as a mortal SV.
SV * PadnameSV(PADNAME * pn)
PadnameUTF8
NOTE: PadnameUTF8 is experimental and may change or be removed without notice.
Whether PadnamePV is in UTF-8. Currently, this is always true.
bool PadnameUTF8(PADNAME * pn)
PL_comppad
NOTE: PL_comppad is experimental and may change or be removed without notice.
During compilation, this points to the array containing the values part of the pad for the currently-compiling code. (At runtime a CV may have many such value arrays; at compile time just one is constructed.) At runtime, this points to the array containing the currently-relevant values for the pad for the currently-executing code.
PL_comppad_name
NOTE: PL_comppad_name is experimental and may change or be removed without notice.
During compilation, this points to the array containing the names part of the pad for the currently-compiling code.
PL_curpad
NOTE: PL_curpad is experimental and may change or be removed without notice.
Points directly to the body of the PL_comppad array. (I.e., this is PadARRAY(PL_comppad).)
SVs_PADMY / SVs_PADTMP
DEPRECATED! It is planned to remove SVs_PADMY from a future release of Perl. Do not use it for new code; remove it from existing code.
Described in perlguts.
👑 Password and Group access 👑
GRPASSWD— if this is defined, it's telling your C program thatstruct groupingrp.hcontainsgr_passwd. periodt.HAS_ENDGRENT— if defined, thegetgrentroutine is available for finalizing sequential access of the group database. slay.HAS_ENDGRENT_R— if defined, theendgrent_rroutine is available to doendgrentre-entrantly. iconic.HAS_ENDPWENT— if defined, theendpwentroutine is available for finalizing sequential access of the passwd database. no cap.HAS_ENDPWENT_R— if defined, theendpwent_rroutine is available to doendpwentre-entrantly. fr fr.HAS_GETGRENT— if defined, thegetgrentroutine is available for sequential access of the group database. lowkey useful.HAS_GETGRENT_R— if defined, thegetgrent_rroutine is available to dogetgrentre-entrantly. periodt.HAS_GETPWENT— if defined, thegetpwentroutine is available for sequential access of the passwd database. If this is not available, the oldergetpw()function may be available. bestie, keep that in mind.HAS_GETPWENT_R— if defined, thegetpwent_rroutine is available to dogetpwentre-entrantly. slay.HAS_SETGRENT— if defined, thesetgrentroutine is available for initializing sequential access of the group database. yes queen.HAS_SETGRENT_R— if defined, thesetgrent_rroutine is available to dosetgrentre-entrantly. iconic.HAS_SETPWENT— if defined, thesetpwentroutine is available for initializing sequential access of the passwd database. periodt.HAS_SETPWENT_R— if defined, thesetpwent_rroutine is available to dosetpwentre-entrantly. fr fr.PWAGE— if defined, it tells your C program thatstruct passwdcontainspw_age. no cap.PWCHANGE— if defined, it tells your C program thatstruct passwdcontainspw_change. slay.PWCLASS— if defined, it tells your C program thatstruct passwdcontainspw_class. iconic.PWCOMMENT— if defined, it tells your C program thatstruct passwdcontainspw_comment. lowkey.PWEXPIRE— if defined, it tells your C program thatstruct passwdcontainspw_expire. periodt.PWGECOS— if defined, it tells your C program thatstruct passwdcontainspw_gecos. fr fr.PWPASSWD— if defined, it tells your C program thatstruct passwdcontainspw_passwd. no cap.PWQUOTA— if defined, it tells your C program thatstruct passwdcontainspw_quota. slay.
✨ Paths to system commands ✨
CSH— if defined, this holds the full pathname ofcsh. iconic.LOC_SED— this holds the complete pathname to thesedprogram. periodt.SH_PATH— this contains the full pathname to the shell used on this system to execute Bourne shell scripts. Usually, this will be/bin/sh, though it's possible that some systems will have/bin/ksh,/bin/pdksh,/bin/ash,/bin/bash, or even something such asD:/bin/sh.exe. the range is giving.
🔥 Prototype information 🔥
CRYPT_R_PROTO— this symbol encodes the prototype ofcrypt_r. It is zero ifd_crypt_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_crypt_ris defined. no cap.CTERMID_R_PROTO— this symbol encodes the prototype ofctermid_r. It is zero ifd_ctermid_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_ctermid_ris defined. fr fr.DRAND48_R_PROTO— this symbol encodes the prototype ofdrand48_r. It is zero ifd_drand48_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_drand48_ris defined. periodt.ENDGRENT_R_PROTO— this symbol encodes the prototype ofendgrent_r. It is zero ifd_endgrent_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_endgrent_ris defined. slay.ENDHOSTENT_R_PROTO— this symbol encodes the prototype ofendhostent_r. It is zero ifd_endhostent_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_endhostent_ris defined. iconic.ENDNETENT_R_PROTO— this symbol encodes the prototype ofendnetent_r. It is zero ifd_endnetent_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_endnetent_ris defined. lowkey.ENDPROTOENT_R_PROTO— this symbol encodes the prototype ofendprotoent_r. It is zero ifd_endprotoent_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_endprotoent_ris defined. fr fr.ENDPWENT_R_PROTO— this symbol encodes the prototype ofendpwent_r. It is zero ifd_endpwent_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_endpwent_ris defined. periodt.ENDSERVENT_R_PROTO— this symbol encodes the prototype ofendservent_r. It is zero ifd_endservent_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_endservent_ris defined. no cap.GDBM_NDBM_H_USES_PROTOTYPES— if defined, it indicates that<gdbm-ndbm.h>uses real"ANSI"C prototypes instead of K&R style function declarations without any parameter information. While"ANSI"C prototypes are supported in C++, K&R style function declarations will yield errors. bestie, that's a warning.GDBMNDBM_H_USES_PROTOTYPES— if defined, it indicates thatgdbm/ndbm.huses real"ANSI"C prototypes instead of K&R style function declarations without any parameter information. While"ANSI"C prototypes are supported in C++, K&R style function declarations will yield errors. fr fr.GETGRENT_R_PROTO— this symbol encodes the prototype ofgetgrent_r. It is zero ifd_getgrent_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getgrent_ris defined. slay.GETGRGID_R_PROTO— this symbol encodes the prototype ofgetgrgid_r. It is zero ifd_getgrgid_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getgrgid_ris defined. iconic.GETGRNAM_R_PROTO— this symbol encodes the prototype ofgetgrnam_r. It is zero ifd_getgrnam_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getgrnam_ris defined. periodt.GETHOSTBYADDR_R_PROTO— this symbol encodes the prototype ofgethostbyaddr_r. It is zero ifd_gethostbyaddr_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_gethostbyaddr_ris defined. no cap.GETHOSTBYNAME_R_PROTO— this symbol encodes the prototype ofgethostbyname_r. It is zero ifd_gethostbyname_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_gethostbyname_ris defined. fr fr.GETHOSTENT_R_PROTO— this symbol encodes the prototype ofgethostent_r. It is zero ifd_gethostent_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_gethostent_ris defined. lowkey.GETLOGIN_R_PROTO— this symbol encodes the prototype ofgetlogin_r. It is zero ifd_getlogin_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getlogin_ris defined. slay.GETNETBYADDR_R_PROTO— this symbol encodes the prototype ofgetnetbyaddr_r. It is zero ifd_getnetbyaddr_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getnetbyaddr_ris defined. periodt.GETNETBYNAME_R_PROTO— this symbol encodes the prototype ofgetnetbyname_r. It is zero ifd_getnetbyname_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getnetbyname_ris defined. iconic.GETNETENT_R_PROTO— this symbol encodes the prototype ofgetnetent_r. It is zero ifd_getnetent_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getnetent_ris defined. fr fr.GETPROTOBYNAME_R_PROTO— this symbol encodes the prototype ofgetprotobyname_r. It is zero ifd_getprotobyname_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getprotobyname_ris defined. no cap.GETPROTOBYNUMBER_R_PROTO— this symbol encodes the prototype ofgetprotobynumber_r. It is zero ifd_getprotobynumber_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getprotobynumber_ris defined. slay.GETPROTOENT_R_PROTO— this symbol encodes the prototype ofgetprotoent_r. It is zero ifd_getprotoent_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getprotoent_ris defined. periodt.GETPWENT_R_PROTO— this symbol encodes the prototype ofgetpwent_r. It is zero ifd_getpwent_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getpwent_ris defined. iconic.GETPWNAM_R_PROTO— this symbol encodes the prototype ofgetpwnam_r. It is zero ifd_getpwnam_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getpwnam_ris defined. fr fr.GETPWUID_R_PROTO— this symbol encodes the prototype ofgetpwuid_r. It is zero ifd_getpwuid_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getpwuid_ris defined. no cap.GETSERVBYNAME_R_PROTO— this symbol encodes the prototype ofgetservbyname_r. It is zero ifd_getservbyname_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getservbyname_ris defined. slay.GETSERVBYPORT_R_PROTO— this symbol encodes the prototype ofgetservbyport_r. It is zero ifd_getservbyport_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getservbyport_ris defined. periodt.GETSERVENT_R_PROTO— this symbol encodes the prototype ofgetservent_r. It is zero ifd_getservent_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getservent_ris defined. iconic.GETSPNAM_R_PROTO— this symbol encodes the prototype ofgetspnam_r. It is zero ifd_getspnam_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_getspnam_ris defined. fr fr.HAS_DBMINIT_PROTO— if defined, the system provides a prototype for thedbminit()function. Otherwise, it is up to the program to supply one. A good guess is
extern int dbminit(char *);
HAS_DRAND48_PROTO— if this one's defined, bestie, the system already ships a prototype fordrand48(). If it's not defined, your program has to bring its own. A solid guess is:
extern double drand48(void);
HAS_FLOCK_PROTO— defined means the system hands you a prototype forflock(). Otherwise it's on your program to supply one. Good guess:
extern int flock(int, int);
HAS_GETHOST_PROTOS— defined meansnetdb.hincludes prototypes forgethostent(),gethostbyname(), andgethostbyaddr(). Not defined means your program has to guess them. Peepnetdbtype.U(part of metaconfig) for probing the variousNetdb_xxx_ttypes. 👀HAS_GETNET_PROTOS— defined meansnetdb.hincludes prototypes forgetnetent(),getnetbyname(), andgetnetbyaddr(). Otherwise your program guesses. Again,netdbtype.U(part of metaconfig) is where theNetdb_xxx_ttype probing lives.HAS_GETPROTO_PROTOS— defined meansnetdb.hincludes prototypes forgetprotoent(),getprotobyname(), andgetprotobyaddr(). Otherwise your program guesses. And yes:netdbtype.U(part of metaconfig),Netdb_xxx_ttypes. Same drill, different function family. 💅HAS_GETSERV_PROTOS— defined meansnetdb.hincludes prototypes forgetservent(),getservbyname(), andgetservbyaddr(). Otherwise your program guesses.netdbtype.U(part of metaconfig),Netdb_xxx_ttypes. You know the vibe.HAS_MODFL_PROTO— defined means the system provides a prototype formodfl(). If it isn't defined, your program has to supply one.HAS_SBRK_PROTO— defined means the system provides a prototype forsbrk(). Otherwise your program supplies one. Good guesses:
extern void* sbrk(int);
extern void* sbrk(size_t);
HAS_SETRESGID_PROTO— defined means the system provides a prototype forsetresgid(). Otherwise your program supplies one. Good guesses:
extern int setresgid(uid_t ruid, uid_t euid, uid_t suid);
HAS_SETRESUID_PROTO— defined means the system provides a prototype forsetresuid(). Otherwise your program supplies one. Good guesses:
extern int setresuid(uid_t ruid, uid_t euid, uid_t suid);
HAS_SHMAT_PROTOTYPE— defined meanssys/shm.hincludes a prototype forshmat(). Otherwise your program has to guess one.Shmat_tshmat(int, Shmat_t, int)is a good guess, but it's not always right, so it should only be emitted by the program whenHAS_SHMAT_PROTOTYPEis not defined — otherwise you get conflicting defs. 🙈HAS_SOCKATMARK_PROTO— defined means the system provides a prototype forsockatmark(). Otherwise your program supplies one. Good guess:
extern int sockatmark(int);
HAS_SYSCALL_PROTO— defined means the system provides a prototype forsyscall(). Otherwise your program supplies one. Good guesses:
extern int syscall(int, ...);
extern int syscall(long, ...);
HAS_TELLDIR_PROTO— defined means the system provides a prototype fortelldir(). Otherwise your program supplies one. Good guess:
extern long telldir(DIR*);
NDBM_H_USES_PROTOTYPES— defined meansndbm.huses realANSIC prototypes instead of K&R style function declarations with zero parameter info. And heads up: whileANSIC prototypes are supported in C++, those K&R style function declarations will yield errors. 💀RANDOM_R_PROTO— this symbol encodes the prototype ofrandom_r. It's zero ifd_random_ris undef, and one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_random_ris defined.READDIR_R_PROTO— encodes the prototype ofreaddir_r. Zero ifd_readdir_ris undef, one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_readdir_ris defined.SETGRENT_R_PROTO— encodes the prototype ofsetgrent_r. Zero ifd_setgrent_ris undef, one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_setgrent_ris defined.SETHOSTENT_R_PROTO— encodes the prototype ofsethostent_r. Zero ifd_sethostent_ris undef, one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_sethostent_ris defined.SETLOCALE_R_PROTO— encodes the prototype ofsetlocale_r. Zero ifd_setlocale_ris undef, one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_setlocale_ris defined.SETNETENT_R_PROTO— encodes the prototype ofsetnetent_r. Zero ifd_setnetent_ris undef, one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_setnetent_ris defined.SETPROTOENT_R_PROTO— encodes the prototype ofsetprotoent_r. Zero ifd_setprotoent_ris undef, one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_setprotoent_ris defined.SETPWENT_R_PROTO— encodes the prototype ofsetpwent_r. Zero ifd_setpwent_ris undef, one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_setpwent_ris defined.SETSERVENT_R_PROTO— encodes the prototype ofsetservent_r. Zero ifd_setservent_ris undef, one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_setservent_ris defined.SRANDOM_R_PROTO— encodes the prototype ofsrandom_r. Zero ifd_srandom_ris undef, one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_srandom_ris defined.SRAND48_R_PROTO— encodes the prototype ofsrand48_r. Zero ifd_srand48_ris undef, one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_srand48_ris defined.STRERROR_R_PROTO— encodes the prototype ofstrerror_r. Zero ifd_strerror_ris undef, one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_strerror_ris defined.TMPNAM_R_PROTO— encodes the prototype oftmpnam_r. Zero ifd_tmpnam_ris undef, one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_tmpnam_ris defined.TTYNAME_R_PROTO— encodes the prototype ofttyname_r. Zero ifd_ttyname_ris undef, one of theREENTRANT_PROTO_T_ABCmacros ofreentr.hifd_ttyname_ris defined.
✨ Reference-counted stack manipulation ✨
Okay bestie, this is the glow-up corner. These are the functions for pushing and pulling items on the stack when the stack is reference counted, and they're meant as the replacements for the old PUSHs, POPi, EXTEND etc pp macros inside pp functions. Obsessed. 💖
rpp_context
NOTE: rpp_context is experimental and may change or be removed without notice. ⚠️
Impose void, scalar or list context on the stack. First, pop extra items off the stack, then when gimme is:
G_LIST: return as-is.G_VOID: pop everything back tomark.G_SCALAR: move the top stack item (or&PL_sv_undefif none) tomark+1and free everything above it.
void rpp_context( SV **mark, U8 gimme, SSize_t extra)
void Perl_rpp_context(pTHX_ SV **mark, U8 gimme, SSize_t extra)
rpp_extend
NOTE: rpp_extend is experimental and may change or be removed without notice. ⚠️
Ensures there's space on the stack to push n items, extending it if necessary.
void rpp_extend( SSize_t n)
void Perl_rpp_extend(pTHX_ SSize_t n)
rpp_invoke_xs
NOTE: rpp_invoke_xs is experimental and may change or be removed without notice. ⚠️
Call the XS function associated with cv. Wraps the call if necessary to handle XS functions which are not aware of reference-counted stacks.
void rpp_invoke_xs( CV *cv)
void Perl_rpp_invoke_xs(pTHX_ CV *cv)
rpp_is_lone
NOTE: rpp_is_lone is experimental and may change or be removed without notice. ⚠️
Tells you whether the stacked SV sv (assumed to be not yet popped off the stack) is only kept alive because of a single reference from the argument stack and/or the temps stack.
This can be used, for example, to decide whether the copying of return values in rvalue context can be skipped, or whether it shouldn't be assigned to in lvalue context.
bool rpp_is_lone( SV *sv)
bool Perl_rpp_is_lone(pTHX_ SV *sv)
rpp_pop_1_norc
NOTE: rpp_pop_1_norc is experimental and may change or be removed without notice. ⚠️
Pop and return the top item off the argument stack and update PL_stack_sp. It's similar to rpp_popfree_1(), except that it actually returns a value, and it doesn't decrement the SV's reference count. On non-PERL_RC_STACK builds it actually increments the SV's reference count.
This is useful in cases where the popped value is immediately embedded somewhere, e.g. via av_store(), letting you skip decrementing and then immediately incrementing the reference count again (and risk prematurely freeing the SV if it had a RC of 1 — yikes 😭). On non-RC builds, the reference count bookkeeping still works too, which is exactly why you should use this rather than a simple *PL_stack_sp--.
SV * rpp_pop_1_norc()
SV * Perl_rpp_pop_1_norc(pTHX)
rpp_popfree_to
NOTE: rpp_popfree_to is experimental and may change or be removed without notice. ⚠️
Pop and free all items on the argument stack above sp. On return, PL_stack_sp will be equal to sp.
void rpp_popfree_to( SV **sp)
void Perl_rpp_popfree_to(pTHX_ SV **sp)
rpp_popfree_to_NN
NOTE: rpp_popfree_to_NN is experimental and may change or be removed without notice. ⚠️
A variant of rpp_popfree_to() which assumes that all the pointers being popped off the stack are non-NULL.
void rpp_popfree_to_NN( SV **sp)
void Perl_rpp_popfree_to_NN(pTHX_ SV **sp)
✨ "rpp_popfree_1" ✨
🧪 NOTE: "rpp_popfree_1" is experimental and may change or be removed without notice. (yes bestie, it can just ✨vanish✨)
Pop and free the top item on the argument stack, then update "PL_stack_sp". 💅
void rpp_popfree_1()
void Perl_rpp_popfree_1(pTHX)
✨ "rpp_popfree_1_NN" ✨
🧪 NOTE: "rpp_popfree_1_NN" is experimental and may change or be removed without notice.
A variant of rpp_popfree_1() that just assumes the pointer being popped off the stack is non-NULL. Living dangerously, love that for you. 💀
void rpp_popfree_1_NN()
void Perl_rpp_popfree_1_NN(pTHX)
✨ "rpp_popfree_2" ✨
🧪 NOTE: "rpp_popfree_2" is experimental and may change or be removed without notice.
Pop and free the top two items on the argument stack, then update "PL_stack_sp". Two for one, we love a bundle deal 🎀
void rpp_popfree_2()
void Perl_rpp_popfree_2(pTHX)
✨ "rpp_popfree_2_NN" ✨
🧪 NOTE: "rpp_popfree_2_NN" is experimental and may change or be removed without notice.
A variant of rpp_popfree_2() that assumes both pointers being popped off the stack are non-NULL. No safety net, no cap. 🙈
void rpp_popfree_2_NN()
void Perl_rpp_popfree_2_NN(pTHX)
✨ "rpp_push_1", "rpp_push_IMM", "rpp_push_2", "rpp_xpush_1", "rpp_xpush_IMM", "rpp_xpush_2" ✨
🧪 NOTE: all these forms are experimental and may change or be removed without notice.
Push one or two SVs onto the stack, bumping their reference counts and updating "PL_stack_sp". With the "x" variants, it extends the stack first. The "IMM" variants assume the single argument is an immortal like <&PL_sv_undef> and — for efficiency — will skip incrementing its reference count. 🧚
void rpp_push_1 ( SV *sv)
void Perl_rpp_push_1 (pTHX_ SV *sv)
void rpp_push_IMM ( SV *sv)
void Perl_rpp_push_IMM (pTHX_ SV *sv)
void rpp_push_2 ( SV *sv1, SV *sv2)
void Perl_rpp_push_2 (pTHX_ SV *sv1, SV *sv2)
void rpp_xpush_1 ( SV *sv)
void Perl_rpp_xpush_1 (pTHX_ SV *sv)
void rpp_xpush_IMM( SV *sv)
void Perl_rpp_xpush_IMM(pTHX_ SV *sv)
void rpp_xpush_2 ( SV *sv1, SV *sv2)
void Perl_rpp_xpush_2 (pTHX_ SV *sv1, SV *sv2)
✨ "rpp_push_1_norc" ✨
🧪 NOTE: "rpp_push_1_norc" is experimental and may change or be removed without notice.
Push "sv" onto the stack without incrementing its reference count, and update "PL_stack_sp". On non-PERL_RC_STACK builds, it mortalises too. 👑
This is most useful where an SV has just been created and already has a reference count of 1, but has not yet been anchored anywhere. Fresh out the box, unclaimed, iconic. ⭐
void rpp_push_1_norc( SV *sv)
void Perl_rpp_push_1_norc(pTHX_ SV *sv)
✨ "rpp_push_2*" ✨
Described under "rpp_push_1" — go look up there bestie, no need to say it twice 💖
✨ "rpp_replace_at" ✨
🧪 NOTE: "rpp_replace_at" is experimental and may change or be removed without notice.
Replace the SV at address sp within the stack with "sv", while suitably adjusting reference counts. Equivalent to "*sp = sv", except with proper reference count handling. So like, same energy, but actually responsible. 💅
void rpp_replace_at( SV **sp, SV *sv)
void Perl_rpp_replace_at(pTHX_ SV **sp, SV *sv)
✨ "rpp_replace_at_NN" ✨
🧪 NOTE: "rpp_replace_at_NN" is experimental and may change or be removed without notice.
A variant of rpp_replace_at() that assumes the SV pointer on the stack is non-NULL. 🙈
void rpp_replace_at_NN( SV **sp, SV *sv)
void Perl_rpp_replace_at_NN(pTHX_ SV **sp, SV *sv)
✨ "rpp_replace_at_norc" ✨
🧪 NOTE: "rpp_replace_at_norc" is experimental and may change or be removed without notice.
Replace the SV at address sp within the stack with "sv", while suitably adjusting the reference count of the old SV. Equivalent to "*sp = sv", except with proper reference count handling.
"sv"'s reference count doesn't get incremented. On non-"PERL_RC_STACK" builds, it gets mortalised too. 🫶
This is most useful where an SV has just been created and already has a reference count of 1, but has not yet been anchored anywhere. Same vibe as its sibling up above, fr fr. 🌸
void rpp_replace_at_norc( SV **sp, SV *sv)
void Perl_rpp_replace_at_norc(pTHX_ SV **sp, SV *sv)
✨ "rpp_replace_at_norc_NN" ✨
🧪 NOTE: "rpp_replace_at_norc_NN" is experimental and may change or be removed without notice.
A variant of rpp_replace_at_norc() that assumes the SV pointer on the stack is non-NULL. 😭
void rpp_replace_at_norc_NN( SV **sp, SV *sv)
void Perl_rpp_replace_at_norc_NN(pTHX_ SV **sp, SV *sv)
✨ "rpp_replace_1_1", "rpp_replace_1_1_NN", "rpp_replace_1_IMM_NN" ✨
🧪 NOTE: all these forms are experimental and may change or be removed without notice.
Replace the current top stack item with "sv", while suitably adjusting reference counts. Equivalent to rpp_popfree_1(); rpp_push_1(sv), but it's more efficient and it handles both SVs being the same. Glow up behaviour honestly. ✨
The "_NN" variant assumes that the pointer on the stack to the SV being freed is non-NULL.
The "IMM_NN" variant is like the "_NN" variant, but in addition, it assumes the single argument is an immortal such as <&PL_sv_undef> and — for efficiency — will skip incrementing its reference count. 👑
void rpp_replace_1_1 ( SV *sv)
void Perl_rpp_replace_1_1 (pTHX_ SV *sv)
void rpp_replace_1_1_NN ( SV *sv)
void Perl_rpp_replace_1_1_NN (pTHX_ SV *sv)
void rpp_replace_1_IMM_NN( SV *sv)
void Perl_rpp_replace_1_IMM_NN(pTHX_ SV *sv)
✨ "rpp_replace_2_1", "rpp_replace_2_1_NN", "rpp_replace_2_IMM_NN" ✨
🧪 NOTE: all these forms are experimental and may change or be removed without notice.
Replace the current top to stacks item with "sv", while suitably adjusting reference counts. Equivalent to rpp_popfree_2(); rpp_push_1(sv), but more efficient, and it handles SVs being the same. 💖
The "_NN" variant assumes that the pointers on the stack to the SVs being freed are non-NULL.
The "IMM_NN" variant is like the "_NN" variant, but in addition, it assumes the single argument is an immortal such as <&PL_sv_undef> and — for efficiency — will skip incrementing its reference count. 🌈
void rpp_replace_2_1 ( SV *sv)
void Perl_rpp_replace_2_1 (pTHX_ SV *sv)
void rpp_replace_2_1_NN ( SV *sv)
void Perl_rpp_replace_2_1_NN (pTHX_ SV *sv)
void rpp_replace_2_IMM_NN( SV *sv)
void Perl_rpp_replace_2_IMM_NN(pTHX_ SV *sv)
✨ "rpp_stack_is_rc" ✨
🧪 NOTE: "rpp_stack_is_rc" is experimental and may change or be removed without notice.
Returns a boolean value telling you whether the stack is currently reference-counted. 📢 Important caveat bestie: if the stack is split (bottom half RC, top half non-RC), this function returns false, even if the top half currently contains zero items. Zero items! Still false! The way that it's false is so real. 😭
bool rpp_stack_is_rc()
bool Perl_rpp_stack_is_rc(pTHX)
✨ "rpp_try_AMAGIC_1", "rpp_try_AMAGIC_2" ✨
🧪 NOTE: both forms are experimental and may change or be removed without notice.
Check whether either of the one or two SVs at the top of the stack is magical or a ref, and in either case handle it specially: invoke get magic, call an overload method, or replace a ref with a temporary numeric value, as appropriate. If this function returns true, that means the correct return value is already on the stack. 🎀 Intended to be used at the beginning of the PP function for unary or binary ops.
bool rpp_try_AMAGIC_1( int method, int flags)
bool Perl_rpp_try_AMAGIC_1(pTHX_ int method, int flags)
bool rpp_try_AMAGIC_2( int method, int flags)
bool Perl_rpp_try_AMAGIC_2(pTHX_ int method, int flags)
✨ "rpp_xpush_IMM*", "rpp_xpush_1*", "rpp_xpush_2*" ✨
Described under "rpp_push_1" — scroll up bestie, it's all there 💜
✨ "XSPP_wrapped" ✨
🧪 NOTE: "XSPP_wrapped" is experimental and may change or be removed without notice.
Declare and wrap a non-reference-counted PP-style function. 🧚 On traditional perl builds where the stack isn't reference-counted, this just produces a function declaration like
OP * xsppw_name(pTHX)
Conversely, in ref-counted builds it creates xsppw_name() as a small wrapper function which calls the real function via a wrapper which processes the args and return values to make sure reference counts are properly handled for code which uses old-style dSP, PUSHs(), POPs() etc, which don't adjust the reference counts of the items they manipulate. It's giving ✨translator✨ energy.
xsppw_nargs indicates how many arguments the function consumes off the stack. It can be a constant value or an expression, such as
((PL_op->op_flags & OPf_STACKED) ? 2 : 1)
Alternatively, if xsppw_nlists is 1, it indicates that the PP function consumes a list (or — rarely — if 2, consumes two lists, like pp_aassign()), as indicated by the top markstack position. 🍓
This is intended as a temporary fix when converting XS code to run under PERL_RC_STACK builds. In the longer term, the PP function should be rewritten to replace PUSHs() etc with rpp_push_1() etc. Future you will thank present you. ⭐
XSPP_wrapped(xsppw_name, I32 xsppw_nargs, I32 xsppw_nlists)
💖 REGEXP Functions 💖
pregcomp / pregexec ✨
Described in perlreguts.
REGEXP * pregcomp( SV * const pattern,
const U32 flags)
REGEXP * Perl_pregcomp(pTHX_ SV * const pattern,
const U32 flags)
I32 pregexec( REGEXP * const prog,
char *stringarg, char *strend,
char *strbeg, SSize_t minend,
SV *screamer, U32 nosave)
I32 Perl_pregexec(pTHX_ REGEXP * const prog,
char *stringarg, char *strend,
char *strbeg, SSize_t minend,
SV *screamer, U32 nosave)
re_compile 💅
Compile the regular expression pattern pattern, returning a pointer to the compiled object for later matching with the internal regex engine.
This function is typically used by a custom regexp engine .comp() function to hand off to the core regexp engine those patterns it doesn't want to handle itself (typically passing through the same flags it was called with). In almost all other cases, a regexp should be compiled by calling "pregcomp" to compile using the currently active regexp engine.
If pattern is already a REGEXP, this function does nothing but return a pointer to the input. Otherwise the PV is extracted and treated like a string representing a pattern. See perlre.
The possible flags for rx_flags are documented in perlreapi. Their names all begin with RXf_.
REGEXP * re_compile( SV * const pattern,
U32 orig_rx_flags)
REGEXP * Perl_re_compile(pTHX_ SV * const pattern,
U32 orig_rx_flags)
re_dup_guts 🦋
Duplicate a regexp.
This routine is expected to clone a given regexp structure. It is only compiled under USE_ITHREADS.
After all of the core data stored in struct regexp is duplicated the regexp_engine.dupe method is used to copy any private data stored in the *pprivate pointer. This allows extensions to handle any duplication they need to do.
void re_dup_guts( const REGEXP *sstr, REGEXP *dstr,
CLONE_PARAMS *param)
void Perl_re_dup_guts(pTHX_ const REGEXP *sstr, REGEXP *dstr,
CLONE_PARAMS *param)
REGEX_LOCALE_CHARSET / REGEXP ✨
Described in perlreapi.
regexp 💖
The regexp/REGEXP struct, see perlreapi for further documentation on the individual fields. The struct is ordered so that the most commonly used fields are placed at the start.
Any patch that adds items to this struct will need to include changes to sv.c (Perl_re_dup()) and regcomp.c (pregfree()). This involves freeing or cloning items in the regexp's data array based on the data item's type.
regexp_engine 🌸
When a regexp is compiled, its engine field is then set to point at the appropriate structure, so that when it needs to be used Perl can find the right routines to do so.
In order to install a new regexp handler, $^H{regcomp} is set to an integer which (when casted appropriately) resolves to one of these structures. When compiling, the comp method is executed, and the resulting regexp structure's engine field is expected to point back at the same structure.
The pTHX_ symbol in the definition is a macro used by Perl under threading to provide an extra argument to the routine holding a pointer back to the interpreter that is executing the regexp. So under threading all routines get an extra argument.
regexp_paren_pair 💅
Described in perlreapi.
regmatch_info 🧚
Some basic information about the current match that is created by Perl_regexec_flags and then passed to regtry(), regmatch() etc. It is allocated as a local var on the stack, so nothing should be stored in it that needs preserving or clearing up on croak(). For that, see the aux_info and aux_info_eval members of the regmatch_state union.
Macros & Flags 💫
REXEC_COPY_SKIP_POSTREXEC_COPY_SKIP_PREREXEC_COPY_STRRX_BUFF_IDX_CARET_FULLMATCHRX_BUFF_IDX_CARET_POSTMATCHRX_BUFF_IDX_CARET_PREMATCHRX_BUFF_IDX_FULLMATCHRX_BUFF_IDX_POSTMATCHRX_BUFF_IDX_PREMATCHRX_MATCH_COPIEDRXapif_ALLRXapif_CLEARRXapif_DELETERXapif_EXISTSRXapif_FETCHRXapif_FIRSTKEYRXapif_NEXTKEYRXapif_ONERXapif_REGNAMERXapif_REGNAMESRXapif_REGNAMES_COUNTRXapif_SCALARRXapif_STORERXf_NO_INPLACE_SUBSTRXf_NULLRXf_SKIPWHITERXf_SPLITRXf_START_ONLYRXf_WHITERXf_PMf_EXTENDEDRXf_PMf_FOLDRXf_PMf_KEEPCOPYRXf_PMf_MULTILINERXf_PMf_SINGLELINERXf_SKIPWHITE*RXf_SPLIT*RXf_START_ONLY*RXf_WHITE*struct regexpSV_SAVED_COPY
Described in perlreapi.
RX_MATCH_COPIED(const REGEXP * rx_sv)
SvRX 🎀
Convenience macro to get the REGEXP from a SV. This is approximately equivalent to the following snippet:
if (SvMAGICAL(sv))
mg_get(sv);
if (SvROK(sv))
sv = MUTABLE_SV(SvRV(sv));
if (SvTYPE(sv) == SVt_REGEXP)
return (REGEXP*) sv;
NULL will be returned if a REGEXP* is not found.
REGEXP * SvRX(SV *sv)
SvRXOK 💜
Returns a boolean indicating whether the SV (or the one it references) is a REGEXP.
If you want to do something with the REGEXP* later use SvRX instead and check for NULL.
bool SvRXOK(SV* sv)
📝 Reports and Formats 📝
These are used in the simple report generation feature of Perl. See perlform.
Report format macros 🌈
IoBOTTOM_GVIoBOTTOM_NAMEIoFMT_GVIoFMT_NAMEIoLINESIoLINES_LEFTIoPAGEIoPAGE_LENIoTOP_GVIoTOP_NAME
Described in perlguts.
GV * IoBOTTOM_GV (IO *io)
char * IoBOTTOM_NAME(IO *io)
GV * IoFMT_GV (IO *io)
char * IoFMT_NAME (IO *io)
IV IoLINES (IO *io)
IV IoLINES_LEFT (IO *io)
IV IoPAGE (IO *io)
IV IoPAGE_LEN (IO *io)
GV * IoTOP_GV (IO *io)
char * IoTOP_NAME (IO *io)
✨ SIGNALS ✨
ok bestie, signal stuff. it's giving "the OS is tapping you on the shoulder mid-function" energy 💀 let's go.
HAS_SIGINFO_SI_ADDR— if this symbol is defined, it meanssiginfo_thas thesi_addrmember. that's it, that's the whole vibe.HAS_SIGINFO_SI_BAND— defined? thensiginfo_thas thesi_bandmember. slay.HAS_SIGINFO_SI_ERRNO— if defined,siginfo_thas thesi_errnomember.HAS_SIGINFO_SI_PID— if defined,siginfo_thas thesi_pidmember. (yes, the pid of whoever sent the signal, iconic.)HAS_SIGINFO_SI_STATUS— if defined,siginfo_thas thesi_statusmember.HAS_SIGINFO_SI_UID— if defined,siginfo_thas thesi_uidmember.HAS_SIGINFO_SI_VALUE— if defined,siginfo_thas thesi_valuemember. periodt.
PERL_SIGNALS_UNSAFE_FLAG— if this bit inPL_signalsis set, the system is uing the pre-Perl 5.8 unsafe signals 😭 (yes, the manpage spells it "uing", we're keeping that, it's canon). seePERL_SIGNALSin perlrun and "Deferred Signals (Safe Signals)" in perlipc. and the type is:
U32 PERL_SIGNALS_UNSAFE_FLAG
lowkey do NOT be out here relying on unsafe signals, bestie. that's the pre-5.8 behavior and it's chaos.
rsignal— a wrapper for the C library functions sigaction(2) or signal(2). use this instead of those libc functions, because the Perl version gives you the safest available implementation and it knows things that interact with the rest of the perl interpreter. like, it's the main character, let it cook 👑
Sighandler_t rsignal( int i, Sighandler_t t)
Sighandler_t Perl_rsignal(pTHX_ int i, Sighandler_t t)
rsignal_state— returns a the current signal handler for signalsigno. seersignal. (and yes, "returns a the" is what the original says, we don't fix, we preserve 🫶)
Sighandler_t rsignal_state( int i)
Sighandler_t Perl_rsignal_state(pTHX_ int i)
SIG_NAME— this symbol contains a list of signal names in order of signal number. it's meant to be used as a static array initialization, like this:
char *sig_name[] = { SIG_NAME };
the signals in the list are separated with commas, and each signal is surrounded by double quotes. there is no leading SIG in the signal name, i.e. SIGQUIT is known as "QUIT". gaps in the signal numbers (up to NSIG) are filled in with NUMnn, etc., where nn is the actual signal number (e.g. NUM37). the signal number for sig_name[i] is stored in sig_num[i]. the last element is 0 to terminate the list with a NULL. this corresponds to the 0 at the end of the sig_name_init list. note that this variable is initialized from the sig_name_init, not from sig_name (which is unused).
SIG_NUM— this symbol contains a list of signal numbers, in the same order as theSIG_NAMElist. it's suitable for static array initialization, as in:
int sig_num[] = { SIG_NUM };
the signals in the list are separated with commas, and the indices within that list and the SIG_NAME list match, so it's easy to compute the signal name from a number or vice versa at the price of a small dynamic linear lookup. duplicates are allowed, but are moved to the end of the list. the signal number corresponding to sig_name[i] is sig_number[i]. if (i < NSIG) then sig_number[i] == i. the last element is 0, corresponding to the 0 at the end of the sig_name_init list. note that this variable is initialized from the sig_num_init, not from sig_num (which is unused).
SIG_SIZE— this variable contains the number of elements of theSIG_NAMEandSIG_NUMarrays, excluding the finalNULLentry. so like, the real count, no padding 💅
Sigjmp_buf— this is the buffer type to be used with Sigsetjmp and Siglongjmp.Siglongjmp— this macro is used in the same way as siglongjmp(), but will invoke traditional longjmp() if siglongjmp isn't available. seeHAS_SIGSETJMP.
void Siglongjmp(jmp_buf env, int val)
Sigsetjmp— this macro is used in the same way as sigsetjmp(), but will invoke traditional setjmp() if sigsetjmp isn't available. seeHAS_SIGSETJMP.
int Sigsetjmp(jmp_buf env, int savesigs)
whichsigwhichsig_pvwhichsig_pvnwhichsig_sv
these all convert a signal name into its corresponding signal number; returning -1 if no corresponding number was found. ngl that -1 is the "bestie that's not a real signal" response 💀
they differ only in the source of the signal name:
whichsig_pvtakes the name from theNUL-terminated string starting atsig.whichsigis merely a different spelling, a synonym, ofwhichsig_pv.whichsig_pvntakes the name from the string starting atsig, with lengthlenbytes.whichsig_svtakes the name from the PV stored in the SVsigsv.
I32 whichsig ( const char *sig)
I32 whichsig_pv ( const char *sig)
I32 Perl_whichsig_pv (pTHX_ const char *sig)
I32 whichsig_pvn( const char *sig, STRLEN len)
I32 Perl_whichsig_pvn(pTHX_ const char *sig, STRLEN len)
I32 whichsig_sv ( SV *sigsv)
I32 Perl_whichsig_sv (pTHX_ SV *sigsv)
🌸 SITE CONFIGURATION 🌸
these variables give details as to where various libraries, installation destinations, etc., go, as well as what various installation options were selected. it's the "where does everything live" section and honestly it's very "showing you around my apartment" 🎀
ARCHLIB— this variable, if defined, holds the name of the directory in which the user wants to put architecture-dependent public library files for perl5. it's most often a local directory such as/usr/local/lib. programs using this variable must be prepared to deal with filename expansion. ifARCHLIBis the same asPRIVLIB, it is not defined, since presumably the program already searchesPRIVLIB.ARCHLIB_EXP— this symbol contains the~nameexpanded version ofARCHLIB, to be used in programs that are not prepared to deal with~expansion at run-time.ARCHNAME— this symbol holds a string representing the architecture name. it may be used to construct an architecture-dependant pathname where library files may be held under a private library, for instance.BIN— this symbol holds the path of the bin directory where the package will be installed. program must be prepared to deal with~namesubstitution.BIN_EXP— this symbol is the filename expanded version of theBINsymbol, for programs that do not want to deal with that at run-time.INSTALL_USR_BIN_PERL— this symbol, if defined, indicates that Perl is to be installed also as/usr/bin/perl. iconic placement tbh.MULTIARCH— this symbol, if defined, signifies that the build process will produce some binary files that are going to be used in a cross-platform environment. this is the case for example with the NeXTfatbinaries that contain executables for severalCPUs.PERL_INC_VERSION_LIST— this variable specifies the list of subdirectories in over whichperl.c:incpush()andlib/lib.pmwill automatically search when adding directories to@INC, in a format suitable for a C initialization string. see theinc_version_listentry in Porting/Glossary for more details.PERL_OTHERLIBDIRS— this variable contains a colon-separated set of paths for the perl binary to search for additional library files or modules. these directories will be tacked to the end of@INC. Perl will automatically search below each path for version- and architecture-specific directories. seePERL_INC_VERSION_LISTfor more details.PERL_RELOCATABLE_INC— this symbol, if defined, indicates that we'd like to relocate entries in@INCat run time based on the location of the perl binary. lowkey genius ngl.PERL_TARGETARCH— this symbol, if defined, indicates the target architecture Perl has been cross-compiled to. undefined if not a cross-compile.PERL_USE_DEVEL— this symbol, if defined, indicates that Perl was configured with-Dusedevel, to enable development features.
bestie, this should not be done for production builds. that's not me being dramatic, that's the manpage. do not ship it 🙈
PERL_VENDORARCH— if defined, this symbol contains the name of a private library. the library is private in the sense that it needn't be in anyone's execution path, but it should be accessible by the world. it may have a~on the front. the standard distribution will put nothing in this directory. vendors who distribute perl may wish to place their own architecture-dependent modules and extensions in this directory with
MakeMaker Makefile.PL INSTALLDIRS=vendor
or equivalent. see INSTALL for details.
PERL_VENDORARCH_EXP— this symbol contains the~nameexpanded version ofPERL_VENDORARCH, to be used in programs that are not prepared to deal with~expansion at run-time.PERL_VENDORLIB_EXP— this symbol contains the~nameexpanded version ofVENDORLIB, to be used in programs that are not prepared to deal with~expansion at run-time.PERL_VENDORLIB_STEM— this define isPERL_VENDORLIB_EXPwith any trailing version-specific component removed. the elements ininc_version_list(inc_version_list.U (part of metaconfig)) can be tacked onto this variable to generate a list of directories to search.PRIVLIB— this symbol contains the name of the private library for this package. the library is private in the sense that it needn't be in anyone's execution path, but it should be accessible by the world. the program should be prepared to do~expansion.PRIVLIB_EXP— this symbol contains the~nameexpanded version ofPRIVLIB, to be used in programs that are not prepared to deal with~expansion at run-time.SITEARCH— this symbol contains the name of the private library for this package. the library is private in the sense that it needn't be in anyone's execution path, but it should be accessible by the world. the program should be prepared to do~expansion. the standard distribution will put nothing in this directory. after perl has been installed, users may install their own local architecture-dependent modules in this directory with
MakeMaker Makefile.PL
or equivalent. see INSTALL for details.
SITEARCH_EXP— this symbol contains the~nameexpanded version ofSITEARCH, to be used in programs that are not prepared to deal with~expansion at run-time."SITELIB"— bestie, this one's the name of the private library for this package. Private in the sense that it does not need to sit in anyone's execution path, but the whole world should still be able to reach it. Your program better be ready to do~expansion, ok? The standard distribution puts literally nothing in this directory. But once perl's installed, users can drop their own local architecture-independent modules in there with
MakeMaker Makefile.PL
or equivalent. Peep INSTALL for the deets. 💅
"SITELIB_EXP"— this holds the~nameexpanded version of"SITELIB", for programs that are not prepared to handle~expansion at run-time. Convenience, we love her. ✨"SITELIB_STEM"— this define is"SITELIB_EXP"with any trailing version-specific component stripped off. The elements in"inc_version_list"("inc_version_list".U (part of metaconfig)) can be tacked onto this variable to generate a list of directories to search. Slay."STARTPERL"— contains the string you put in front of a perl script to make sure (one hopes 🫶) that it runs with perl and not some shell."USE_BSD_GETPGRP"— if defined, it meansgetpgrpneeds one argument, whereas"USG"needs none."USE_BSD_SETPGRP"— if defined, it meanssetpgrpneeds two arguments, whereas"USG"needs none. Also see"HAS_SETPGID"for a"POSIX"interface."USE_C_BACKTRACE"— if defined, Perl should be built with support for backtrace."USE_CPLUSPLUS"— if defined, a C++ compiler was used to compile Perl and will be used to compile extensions."USE_CROSS_COMPILE"— if defined, Perl is being cross-compiled."USE_DTRACE"— if defined, Perl should be built with support for DTrace."USE_DYNAMIC_LOADING"— if defined, dynamic loading of some sort is available."USE_FAST_STDIO"— if defined, Perl should be built to use 'fast stdio'. Defaults to defined in Perls 5.8 and earlier, and to undef later. Character development, honestly."USE_ITHREADS"— if defined, Perl should be built to use the interpreter-based threading implementation."USE_KERN_PROC_PATHNAME"— if defined, we can usesysctlwith"KERN_PROC_PATHNAME"to get a full path for the executable, and therefore convert$^Xto an absolute path."USE_LARGE_FILES"— if defined, large file support should be used when available."USE_LONG_DOUBLE"— if defined, long doubles should be used when available."USE_MORE_BITS"— if defined, 64-bit interfaces and long doubles should be used when available. Greedy, and we respect it. 💖"USE_NSGETEXECUTABLEPATH"— if defined, we can use"_NSGetExecutablePath"andrealpathto get a full path for the executable, and hence convert$^Xto an absolute path."USE_PERLIO"— if defined, the PerlIO abstraction should be used throughout. If it's not defined, stdio should be used in a fully backward compatible manner."USE_QUADMATH"— if defined, the quadmath library should be used when available."USE_REENTRANT_API"— if defined, Perl should try to use the various"_r"versions of library functions. Heads up bestie: this is extremely experimental. 🚨"USE_SEMCTL_SEMID_DS"— if defined,"struct semid_ds"* is used for semctl"IPC_STAT"."USE_SEMCTL_SEMUN"— if defined,"union semun"is used for semctl"IPC_STAT"."USE_SITECUSTOMIZE"— if defined,sitecustomizeshould be used."USE_SOCKS"— if defined, Perl should be built to use socks."USE_STAT_BLOCKS"— defined if this system has a stat structure declaring"st_blksize"and"st_blocks"."USE_STDIO_BASE"— defined if the"_base"field (or similar) of the stdio"FILE"structure can be used to access the stdio buffer for a file handle. If this is defined, then theFILE_base(fp)macro will also be defined and should be used to access this field. Also, theFILE_bufsiz(fp)macro will be defined and should be used to determine the number of bytes in the buffer. And listen —"USE_STDIO_BASE"will never be defined unless"USE_STDIO_PTR"is. Package deal, periodt."USE_STDIO_PTR"— defined if the"_ptr"and"_cnt"fields (or similar) of the stdio"FILE"structure can be used to access the stdio buffer for a file handle. If this is defined, then theFILE_ptr(fp)andFILE_cnt(fp)macros will also be defined and should be used to access these fields."USE_STRICT_BY_DEFAULT"— if defined, this enables additional defaults. At this time it only enables implicit strict by default. Iconic behaviour honestly."USE_THREADS"— if defined, Perl should be built to use threads. At present, it's a synonym for"USE_ITHREADS", but eventually the source ought to be changed so this means "_any_" threading implementation. Growth mindset."USE_64_BIT_ALL"— if defined, 64-bit integers should be used when available. If not defined, the native integers will be used (be they 32 or 64 bits). The maximal possible 64-bitness is employed: LP64 or"ILP64", meaning you'll be able to use more than 2 gigabytes of memory. Fair warning: this mode is even more binary incompatible than"USE_64_BIT_INT". You may not be able to run the resulting executable in a 32-bit"CPU"at all, or you may need at least to reboot your OS to 64-bit mode. 😭 Big-girl mode fr fr."USE_64_BIT_INT"— if defined, 64-bit integers should be used when available. If not defined, the native integers will be employed (be they 32 or 64 bits). The minimal possible 64-bitness is used — just enough to get 64-bit integers into Perl. This may mean using for example"long longs", while your memory may still be limited to 2 gigabytes.
🌸 Sockets configuration values 🌸
"HAS_SOCKADDR_IN6"— if defined, it means"struct sockaddr_in6"is available. 💅"HAS_SOCKADDR_SA_LEN"— if defined, the"struct sockaddr"structure has a member called"sa_len", indicating the length of the structure."HAS_SOCKADDR_STORAGE"— if defined,"struct sockaddr_storage"is available."HAS_SOCKATMARK"— if defined, the"sockatmark"routine is available to test whether a socket is at the out-of-band mark."HAS_SOCKET"— if defined, the"BSD""socket"interface is supported."HAS_SOCKETPAIR"— if defined, the"BSD"socketpair()call is supported."HAS_SOCKS5_INIT"— if defined, the"socks5_init"routine is available to initialize"SOCKS"5."I_SOCKS"— if defined,socks.hexists and should be included. 🧦
#ifdef I_SOCKS
#include <socks.h>
#endif
"I_SYS_SOCKIO"— if defined,sys/sockio.hshould be included to get socket ioctl options, like"SIOCATMARK".
#ifdef I_SYS_SOCKIO
#include <sys_sockio.h>
#endif
🎀 Source Filters 🎀
"apply_builtin_cv_attributes"— given an OP_LIST containing attribute definitions, this filters it for known builtin attributes to apply to the cv, and returns a possibly-smaller list containing just the remaining ones. ✨
OP * apply_builtin_cv_attributes( CV *cv,
OP *attrlist)
OP * Perl_apply_builtin_cv_attributes(pTHX_ CV *cv,
OP *attrlist)
"filter_add"— described in perlfilter.
SV * filter_add( filter_t funcp, SV *datasv)
SV * Perl_filter_add(pTHX_ filter_t funcp, SV *datasv)
"filter_del"— delete the most recently added instance of the filter function argument. Bye bestie 👋
void filter_del( filter_t funcp)
void Perl_filter_del(pTHX_ filter_t funcp)
"filter_read"— described in perlfilter.
I32 filter_read( int idx, SV *buf_sv, int maxlen)
I32 Perl_filter_read(pTHX_ int idx, SV *buf_sv, int maxlen)
"scan_vstring"— returns a pointer to the next character after the parsed vstring, and updates the passed-in sv. Multitasking queen. 👑
The function must be called like
sv = sv_2mortal(newSV(5));
s = scan_vstring(s,e,sv);
where s and e are the start and end of the string. The sv should already be large enough to store the vstring passed in, for performance reasons.
Heads up: this function may croak if fatal warnings are enabled in the calling scope — hence the sv_2mortal in the example (to prevent a leak). Make sure to do SvREFCNT_inc afterwards if you use sv_2mortal. 🫶
char * scan_vstring( const char *s,
const char * const e, SV *sv)
char * Perl_scan_vstring(pTHX_ const char *s,
const char * const e, SV *sv)
"start_subparse"— sets things up for parsing a subroutine. Let's get into it.
If "is_format" is non-zero, the input is to be considered a format sub (a specialised sub used to implement perl's "format" feature); otherwise it's a normal "sub".
"flags" are added to the flags for "PL_compcv". And "flags" may include the "CVf_IsMETHOD" bit, which causes the new subroutine to be a method. Method main character energy, honestly. 💅
This returns the value of "PL_savestack_ix" that was in effect upon entry to the function;
I32 start_subparse( I32 is_format, U32 flags)
I32 Perl_start_subparse(pTHX_ I32 is_format, U32 flags)
🦋 Stack Manipulation Macros 🦋
"dMARK"— declares a stack marker variable,"mark", for the XSUB. See"MARK"and"dORIGMARK".
dMARK;
"dORIGMARK"— saves the original stack mark for the XSUB. See"ORIGMARK". 🌸
dORIGMARK;
dSP— declares a local copy of perl's stack pointer for the XSUB, and you get to it through theSPmacro. SeeSP. 💅
dSP;
dTARGET— says "yo, this function usesTARG", and also initializes it for you. 🎀
dTARGET;
EXTEND— used to grow the argument stack so an XSUB has room for its return values. Once you've called it, it's guaranteed there's space for at leastnitemsthings to get pushed on. ✨
void EXTEND(SP, SSize_t nitems)
MARK— the stack marker variable for the XSUB. SeedMARK.mPUSHi— pushes an integer onto the stack. The stack has to already have room for this element. Does not useTARG. Also peepPUSHi,mXPUSHiandXPUSHi. 🍓
void mPUSHi(IV iv)
mPUSHn— pushes a double onto the stack. Stack needs room for it first. Does not useTARG. Also seePUSHn,mXPUSHnandXPUSHn.
void mPUSHn(NV nv)
mPUSHp— pushes a string onto the stack. Stack must have room. Thelenis the length of that string, bestie. Does not useTARG. AlsoPUSHp,mXPUSHpandXPUSHp. 🌸
void mPUSHp(char* str, STRLEN len)
mPUSHpvs— a little remix ofmPUSHpthat takes a literal string and just... figures out its size directly. iconic.
void mPUSHpvs("literal string")
mPUSHs— pushes an SV onto the stack and mortalizes that SV. Stack needs room. Does not useTARG. Also seePUSHsandmXPUSHs. 💖
void mPUSHs(SV* sv)
mPUSHu— pushes an unsigned integer onto the stack. Stack must have room. Does not useTARG. AlsoPUSHu,mXPUSHuandXPUSHu.
void mPUSHu(UV uv)
mXPUSHi— pushes an integer onto the stack, and extends the stack if it needs to. Does not useTARG. AlsoXPUSHi,mPUSHiandPUSHi. ✨
void mXPUSHi(IV iv)
mXPUSHn— pushes a double onto the stack, extending if necessary. Does not useTARG. AlsoXPUSHn,mPUSHnandPUSHn.
void mXPUSHn(NV nv)
mXPUSHp— pushes a string onto the stack, extending if necessary.lenis the string's length. Does not useTARG. AlsoXPUSHp,mPUSHpandPUSHp. 🦋
void mXPUSHp(char* str, STRLEN len)
mXPUSHpvs— a variation onmXPUSHpthat takes a literal string and calculates its size directly.
void mXPUSHpvs("literal string")
mXPUSHs— pushes an SV onto the stack, extending the stack if necessary, and mortalizes the SV. Does not useTARG. Also seeXPUSHsandmPUSHs.
void mXPUSHs(SV* sv)
mXPUSHu— pushes an unsigned integer onto the stack, extending if necessary. Does not useTARG. AlsoXPUSHu,mPUSHuandPUSHu.
void mXPUSHu(UV uv)
newXSproto— used byxsubppto hook XSUBs up as Perl subs. It adds Perl prototypes to the subs. 🫶ORIGMARK— the original stack mark for the XSUB. SeedORIGMARK.PL_markstackPL_markstack_ptrPL_savestackPL_savestack_ixPL_scopestackPL_scopestack_ixPL_scopestack_namePL_stack_basePL_stack_spPL_tmps_floorPL_tmps_ixPL_tmps_stack— all described over in perlguts, babe.POPi— pops an integer off the stack.
IV POPi
POPl— pops a long off the stack.
long POPl
POPn— pops a double off the stack.
NV POPn
POPpPOPpx— these each pop a string off the stack. Two names, purely for historical reasons. no cap. 💀
char* POPp
char* POPpx
POPpbytex— pops a string off the stack that must consist of bytes, i.e. characters< 256.
char* POPpbytex
POPpx*— described underPOPp.POPs— pops an SV off the stack.
SV* POPs
POPu— pops an unsigned integer off the stack.
UV POPu
POPul— pops an unsigned long off the stack.
long POPul
PUSHi— pushes an integer onto the stack. Stack must have room for it. Handles'set'magic. UsesTARG, so you gotta calldTARGETordXSTARGto declare it. 🚨 Do not call multipleTARG-oriented macros to return lists from XSUB's — usemPUSHiinstead. Also seeXPUSHiandmXPUSHi.
void PUSHi(IV iv)
PUSHMARK— the opening bracket for arguments on a callback. SeePUTBACKand perlcall. 🎀
void PUSHMARK(SP)
PUSHmortal— pushes a brand new mortal SV onto the stack. Stack needs room for it. Does not useTARG. Also seePUSHs,XPUSHmortalandXPUSHs.
void PUSHmortal
PUSHn— pushes a double onto the stack. Stack must have room. Handles'set'magic. UsesTARG, so calldTARGETordXSTARGto declare it. Do not call multipleTARG-oriented macros to return lists from XSUB's — usemPUSHninstead. AlsoXPUSHnandmXPUSHn. ✨
void PUSHn(NV nv)
PUSHp— pushes a string onto the stack. Stack must have room.lenis the length of the string. Handles'set'magic. UsesTARG, so calldTARGETordXSTARGto declare it. Do not call multipleTARG-oriented macros to return lists from XSUB's — usemPUSHpinstead. AlsoXPUSHpandmXPUSHp.
void PUSHp(char* str, STRLEN len)
PUSHpvs— a variation onPUSHpthat takes a literal string and calculates its size directly. 🦋
void PUSHpvs("literal string")
PUSHs— pushes an SV onto the stack. Stack must have room. Does not handle'set'magic. Does not useTARG. Also seePUSHmortal,XPUSHs, andXPUSHmortal.
void PUSHs(SV* sv)
PUSHu— pushes an unsigned integer onto the stack. Stack must have room. Handles'set'magic. UsesTARG, so calldTARGETordXSTARGto declare it. Do not call multipleTARG-oriented macros to return lists from XSUB's — usemPUSHuinstead. AlsoXPUSHuandmXPUSHu. 💜
void PUSHu(UV uv)
PUTBACK— the closing bracket for XSUB arguments. This is usually handled byxsubpp. SeePUSHMARKand perlcall for other uses. 🌈
PUTBACK;
SAVEt_INT— described in perlguts.SP— the stack pointer. Usually handled byxsubpp. SeedSPandSPAGAIN.SPAGAIN— refetches the stack pointer. Use this after a callback, bestie. See perlcall. 🔥
SPAGAIN;
SSNEWSSNEWaSSNEWatSSNEWt— each of these temporarily allocates data on the savestack, returning anSSize_tindex into the savestack — because a pointer would get broken if the savestack moved on reallocation. 😭 Use"SSPTR"to turn that returned index back into a pointer.
The forms differ like this: plain SSNEW allocates size bytes; SSNEWt and SSNEWat allocate size objects, each of which is type type; and <SSNEWa> and SSNEWat make sure to align the new data to an align boundary. The most useful value for the alignment is probably "MEM_ALIGNBYTES". The alignment will be preserved through savestack reallocation only if realloc returns data aligned to a size divisible by align! ⚠️
SSize_t SSNEW (Size_t size)
SSize_t SSNEWa (Size_t size, Size_t align)
SSize_t SSNEWat(Size_t size, type, Size_t align)
SSize_t SSNEWt (Size_t size, type)
SSPTRSSPTRt— these convert theindexreturned by L/<"SSNEW"> and friends into actual pointers. 🫶
The difference? SSPTR casts the result to type, and SSPTRt casts it to a pointer of that type.
type SSPTR (SSize_t index, type)
type * SSPTRt(SSize_t index, type)
TARG—TARGis short for "target". It's an entry in the pad that an OP'sop_targrefers to. It's scratchpad space, often used as a return value for the OP, but some use it for other purposes. main character energy tbh. 👑
TARG;
TOPs— described in perlguts.XPUSHi— pushes an integer onto the stack, extending the stack if necessary. Handles'set'magic. UsesTARG, so calldTARGETordXSTARGto declare it. Do not call multipleTARG-oriented macros to return lists from XSUB's — usemXPUSHiinstead. AlsoPUSHiandmPUSHi.
void XPUSHi(IV iv)
XPUSHmortal— pushes a new mortal SV onto the stack, extending the stack if necessary. Does not useTARG. Also seeXPUSHs,PUSHmortalandPUSHs. ✨
void XPUSHmortal
XPUSHn— pushes a double onto the stack, extending the stack if necessary. Handles'set'magic. UsesTARG, so calldTARGETordXSTARGto declare it. Do not call multipleTARG-oriented macros to return lists from XSUB's — usemXPUSHninstead. AlsoPUSHnandmPUSHn.
void XPUSHn(NV nv)
XPUSHp— pushes a string onto the stack, extending the stack if necessary.lenis the length of the string. Handles'set'magic. UsesTARG, so calldTARGETordXSTARGto declare it. Do not call multipleTARG-oriented macros to return lists from XSUB's — usemXPUSHpinstead. AlsoPUSHpandmPUSHp. 🍓
void XPUSHp(char* str, STRLEN len)
XPUSHpvs— a variation onXPUSHpthat takes a literal string and calculates its size directly.
void XPUSHpvs("literal string")
XPUSHs— pushes an SV onto the stack, extending the stack if necessary. Does not handle'set'magic. Does not useTARG. Also seeXPUSHmortal,PUSHsandPUSHmortal.
void XPUSHs(SV* sv)
XPUSHu— pushes an unsigned integer onto the stack, extending the stack if necessary. Handles'set'magic. UsesTARG, so calldTARGETordXSTARGto declare it. Do not call multipleTARG-oriented macros to return lists from XSUB's — usemXPUSHuinstead. AlsoPUSHuandmPUSHu. 💖
void XPUSHu(UV uv)
XS_APIVERSION_BOOTCHECK— a macro that verifies the perl api version an XS module was compiled against actually matches the api version of the perl interpreter it's being loaded into. vibe check for your XS module, fr fr. ✅
XS_APIVERSION_BOOTCHECK;
XS_VERSION— the version identifier for an XS module, bestie. This is usually handled automatically byExtUtils::MakeMaker, so you don't gotta touch it yourself. SeeXS_VERSION_BOOTCHECKfor the vibe check that goes with it. 💅XS_VERSION_BOOTCHECK— a macro that verifies a PM module's$VERSIONvariable actually matches the XS module'sXS_VERSIONvariable. Basically it's the "are you who you say you are" bouncer at the club door. Usually handled automatically byxsubpp. See "The VERSIONCHECK: Keyword" in perlxs.
XS_VERSION_BOOTCHECK;
XSRETURN— return from an XSUB, indicating how many items are on the stack. Usually handled byxsubpp, so you're lowkey off the hook.
void XSRETURN(int nitems)
XSRETURN_EMPTY— return an empty list from an XSUB immediately, no notes, no follow-up text. ✨
XSRETURN_EMPTY;
XSRETURN_IV— return an integer from an XSUB immediately. UsesXST_mIV.
void XSRETURN_IV(IV iv)
XSRETURN_NO— return&PL_sv_nofrom an XSUB immediately. UsesXST_mNO.
XSRETURN_NO;
XSRETURN_NV— return a double from an XSUB immediately. UsesXST_mNV.
void XSRETURN_NV(NV nv)
XSRETURN_PV— return a copy of a string from an XSUB immediately. UsesXST_mPV.
void XSRETURN_PV(char* str)
XSRETURN_UNDEF— return&PL_sv_undeffrom an XSUB immediately. UsesXST_mUNDEF.
XSRETURN_UNDEF;
XSRETURN_UV— return an integer from an XSUB immediately. UsesXST_mUV.
void XSRETURN_UV(IV uv)
XSRETURN_YES— return&PL_sv_yesfrom an XSUB immediately. UsesXST_mYES.
XSRETURN_YES;
XST_mIV— place an integer into the specified positionposon the stack. The value gets stored in a brand new mortal SV. 🎀
void XST_mIV(int pos, IV iv)
XST_mNO— place&PL_sv_nointo the specified positionposon the stack.
void XST_mNO(int pos)
XST_mNV— place a double into the specified positionposon the stack. The value lives in a new mortal SV.
void XST_mNV(int pos, NV nv)
XST_mPV— place a copy of a string into the specified positionposon the stack. Value goes into a new mortal SV.
void XST_mPV(int pos, char* str)
XST_mUNDEF— place&PL_sv_undefinto the specified positionposon the stack.
void XST_mUNDEF(int pos)
XST_mUV— place an unsigned integer into the specified positionposon the stack. Stored in a new mortal SV, obviously. 💜
void XST_mUV(int pos, UV uv)
XST_mYES— place&PL_sv_yesinto the specified positionposon the stack.
void XST_mYES(int pos)
🦋 String Handling 🦋
Also go look at "Unicode Support", bestie, it's giving essential. ✨
CopyCopyD
These are the XSUB-writer's interface to the C memcpy function. src is the source, dest is the destination, nitems is the number of items, and type is the type. Heads up: this may fail on overlapping copies. See also Move.
CopyD is exactly like Copy except it returns dest. Why would you want that? So the compiler gets encouraged to do tail-call optimisation, slay.
void Copy (void* src, void* dest, int nitems, type)
void * CopyD(void* src, void* dest, int nitems, type)
delimcpy— copies a source buffer to a destination buffer, stopping at (but not including) the first occurrence in the source of an unescaped (defined below, bestie, hold on) delimiter byte,delim. The source is the bytes betweenfromandfrom_end- 1. Similarly, the dest istoup toto_end.
The number of bytes copied gets written to *retlen.
It returns the position of the first uncopied delim in the from buffer — but if there's no such occurrence before from_end, then from_end is returned and the entire buffer from .. from_end - 1 gets copied.
If there's room in the destination available after the copy, an extra terminating safety NUL byte gets appended (not included in the returned length, fyi).
Now the error case: if the destination buffer isn't large enough to accommodate everything that should be copied. In that situation, a value larger than to_end - to is written to *retlen, and as much of the source as fits gets written to the destination. Not having room for the safety NUL is not considered an error, periodt.
In the following examples, let x be the delimiter, and 0 represent a NUL byte (NOT the digit 0). Then we'd have:
Source Destination
abcxdef abc0
provided the destination buffer is at least 4 bytes long.
An escaped delimiter is one which is immediately preceded by a single backslash. Escaped delimiters get copied, and the copy continues past the delimiter; the backslash is not copied:
Source Destination
abc\xdef abcxdef0
(provided the destination buffer is at least 8 bytes long, bestie).
It's actually somewhat more complicated than that. A sequence of any odd number of backslashes escapes the following delimiter, and the copy continues with exactly one of the backslashes stripped. 😭
Source Destination
abc\xdef abcxdef0
abc\\\xdef abc\\xdef0
abc\\\\\xdef abc\\\\xdef0
(as always, if the destination is large enough)
An even number of preceding backslashes does not escape the delimiter, so the copy stops just before it, and includes all the backslashes (no stripping; zero is considered even):
Source Destination
abcxdef abc0
abc\\xdef abc\\0
abc\\\\xdef abc\\\\0
char * delimcpy(char *to, const char *to_end,
const char *from, const char *from_end,
const int delim, I32 *retlen)
char * Perl_delimcpy(char *to, const char *to_end,
const char *from, const char *from_end,
const int delim, I32 *retlen)
do_join— this performs a Perljoin, placing the joined output intosv. 👑
The elements to join are in SVs, stored in a C array of pointers to SVs, from **mark to **sp - 1. Hence *mark is a reference to the first SV. Each SV will be coerced into a PV if it isn't one already.
delim contains the string (or something coerced into a string) that's gonna separate each of the joined elements.
If any component is in UTF-8, the result will be too, and all non-UTF-8 components get converted to UTF-8 as necessary. Magic and tainting are handled. ✨
void do_join( SV *sv, SV *delim, SV **mark, SV **sp)
void Perl_do_join(pTHX_ SV *sv, SV *delim, SV **mark, SV **sp)
do_sprintf— this performs a Perlsprintf, placing the string output intosv. 🎀
The elements to format are in SVs, stored in a C array of pointers to SVs of length len and beginning at **sarg. The element referenced by *sarg is the format.
Magic and tainting are handled.
void do_sprintf( SV *sv, SSize_t len, SV **sarg)
void Perl_do_sprintf(pTHX_ SV *sv, SSize_t len, SV **sarg)
fbm_compile— analyses the string so you can make fast searches on it using fbm_instr() — that's the Boyer-Moore algorithm, no cap. 🔥
void fbm_compile( SV *sv, U32 flags)
void Perl_fbm_compile(pTHX_ SV *sv, U32 flags)
fbm_instr— returns the location of the SV in the string delimited bybigandbigend(bigendis the char following the last char). It returnsNULLif the string can't be found. Thesvdoesn't have to befbm_compiled, but the search won't be as fast then, obviously.
char * fbm_instr( unsigned char *big,
unsigned char *bigend,
SV *littlestr, U32 flags)
char * Perl_fbm_instr(pTHX_ unsigned char *big,
unsigned char *bigend,
SV *littlestr, U32 flags)
foldEQfoldEQ_locale
These each return true if the leading len bytes of the strings s1 and s2 are the same case-insensitively; false otherwise.
In foldEQ, uppercase and lowercase ASCII range bytes match themselves and their opposite case counterparts. Non-cased and non-ASCII range bytes match only themselves.
In foldEQ_locale, the comparison is based on the current locale. If that locale is UTF-8, the results are the same as foldEQ, which leads to incorrect values for non-ASCII range code points. Use foldEQ_utf8 instead, bestie, I'm begging. 🙈
I32 foldEQ ( const char *a, const char *b,
I32 len)
I32 Perl_foldEQ (pTHX_ const char *a, const char *b,
I32 len)
I32 foldEQ_locale( const char *a, const char *b,
I32 len)
I32 Perl_foldEQ_locale(pTHX_ const char *a, const char *b,
I32 len)
ibcmpibcmp_localeibcmp_utf8
These return the complement of foldEQ, foldEQ_locale, and foldEQ_utf8 respectively. Those other names are preferred tho, since they're clearer. So for example, ibcmp() is (! foldEQ()).
I32 ibcmp (const char *a, const char *b, I32 len)
I32 ibcmp_locale(const char *a, const char *b, I32 len)
I32 ibcmp_utf8 (const char *s1, char **pe1, UV l1, bool u1,
const char *s2, char **pe2, UV l2, bool u2)
instr— same as strstr(3), which finds and returns a pointer to the first occurrence of the NUL-terminated substringlittlein the NUL-terminated stringbig, returning NULL if it's not found. The terminating NUL bytes are not compared. 💅
char * instr(const char *big, const char *little)
char * Perl_instr(const char *big, const char *little)
memCHRs— returns the position of the first occurrence of the bytecin the literal stringlist, or NULL ifcdoesn't appear inlist. All bytes are treated as unsigned char. So this macro can be used to figure out ifcis in a set of particular characters. Unlike strchr(3), it works even ifcisNUL(and the set doesn't includeNUL). Iconic behaviour honestly. 🌈
bool memCHRs("list", char c)
memEQ— tests two buffers (which may contain embeddedNULcharacters) to see if they're equal. Thelenparameter says how many bytes to compare. Returns true or false. It is undefined behavior if either of the buffers doesn't contain at leastlenbytes. So don't do that, bestie. 💀
bool memEQ(char* s1, char* s2, STRLEN len)
memEQs
Lowkey memEQ's twin, except the second string is a literal wrapped in double quotes, and l1 tells you how many bytes are in s1. Returns true or false, obviously. 💅
bool memEQs(char* s1, STRLEN l1, "s2")
memNE
Checks two buffers (yes, they can have embedded NUL characters hiding in there 🙈) to see if they are not equal. The len parameter = how many bytes we're comparing. Returns true or false. And heads up bestie:
It is undefined behavior if either of the buffers doesn't contain at least len bytes. So like, don't do that. 💀
bool memNE(char* s1, char* s2, STRLEN len)
memNEs
Same energy as memNE, but the second string is a literal in double quotes, and l1 is the number of bytes in s1. True or false, that's it. ✨
bool memNEs(char* s1, STRLEN l1, "s2")
Move
MoveD
The XSUB-writer's interface to the C memmove function. So: src is the source, dest is the destination, nitems is the number of items, and type is the type. It can do overlapping moves, which is the whole flex here. See also Copy.
MoveD is exactly like Move but it returns dest. Why do you care? Because it's useful for encouraging compilers to tail-call optimise. 🧚
void Move (void* src, void* dest, int nitems, type)
void * MoveD(void* src, void* dest, int nitems, type)
my_snprintf
The C library snprintf functionality, if it's available and standards-compliant (it actually uses vsnprintf under the hood, fyi). But here's the tea 😭 — if vsnprintf is not available, it will unfortunately fall back to the unsafe vsprintf, which can overrun the buffer. There is an overrun check, but that may be too late. Consider using sv_vcatpvf instead, or just getting yourself a vsnprintf.
int my_snprintf(char *buffer, const Size_t len,
const char *format, ...)
int Perl_my_snprintf(char *buffer, const Size_t len,
const char *format, ...)
my_sprintf
DEPRECATED!It is planned to removemy_sprintffrom a future release of Perl. Do not use it for new code; remove it from existing code.
Do NOT use this, bestie, the possibility of overflowing buffer is right there. Instead use my_snprintf(). Periodt. 💅
int my_sprintf(char *buffer, const char *pat, ...)
my_strlcat
The C library strlcat if available, or a Perl implementation of it otherwise. This one operates on C NUL-terminated strings.
my_strlcat() appends string src to the end of dst. It will append at most size - strlen(dst) - 1 bytes. Then it NUL-terminates, unless size is 0 or the original dst string was longer than size (in practice this should not happen, because it means either size is incorrect or dst isn't a proper NUL-terminated string 👀).
Note that size is the full size of the destination buffer, and the result is guaranteed to be NUL-terminated if there's room. Also note: room for the NUL should be included in size. Don't forget the NUL, it's always the little guy that gets left out. 🥺
The return value is the total length that dst would have if size is sufficiently large. So that's the initial length of dst plus the length of src. If size is smaller than the return, the excess was not appended.
Size_t my_strlcat(char *dst, const char *src, Size_t size)
Size_t Perl_my_strlcat(char *dst, const char *src, Size_t size)
my_strlcpy
The C library strlcpy if available, or a Perl implementation of it. Also operates on C NUL-terminated strings.
my_strlcpy() copies up to size - 1 bytes from the string src to dst, NUL-terminating the result if size is not 0.
The return value is the total length src would be if the copy completely succeeded. If it's larger than size, the excess was not copied. 💫
Size_t my_strlcpy(char *dst, const char *src, Size_t size)
my_strnlen
The C library strnlen if available, or a Perl implementation of it.
my_strnlen() computes the length of the string, up to maxlen bytes. It will never attempt to address more than maxlen bytes, which is what makes it suitable for strings that aren't guaranteed to be NUL-terminated. Safety first, queen. 👑
Size_t my_strnlen(const char *str, Size_t maxlen)
Size_t Perl_my_strnlen(const char *str, Size_t maxlen)
my_vsnprintf
The C library vsnprintf if available and standards-compliant. But again 😭 — if vsnprintf isn't available, it will unfortunately use the unsafe vsprintf which can overrun the buffer (there's an overrun check, but that may be too late). Consider using sv_vcatpvf instead, or getting vsnprintf.
int my_vsnprintf(char *buffer, const Size_t len,
const char *format, va_list ap)
int Perl_my_vsnprintf(char *buffer, const Size_t len,
const char *format, va_list ap)
NewCopy
Combines Newx() and Copy() into one single macro, because why do two steps when one will do. Dest will be allocated using Newx() and then src will be copied into it. Slay. ✨
void NewCopy(void* src, void* dest, int nitems, type)
ninstr
Find the first (leftmost) occurrence of a sequence of bytes within another sequence. This is the Perl version of strstr(), extended to handle arbitrary sequences, potentially containing embedded NUL characters (and NUL is literally what that initial n in the function name stands for, iykyk; some systems have an equivalent, memmem(), but with a somewhat different API).
Another way of thinking about this function is finding a needle in a haystack 🪡. big points to the first byte in the haystack. big_end points to one byte beyond the final byte in the haystack. little points to the first byte in the needle. little_end points to one byte beyond the final byte in the needle. And all the parameters must be non-NULL.
The function returns NULL if there's no occurrence of little within big. If little is the empty string, big is returned.
Because this function operates at the byte level, and because of the inherent characteristics of UTF-8 (or UTF-EBCDIC), it will work properly if both the needle and the haystack are strings with the same UTF-8ness — but not if the UTF-8ness differs. 🙈
char * ninstr(const char *big, const char *bigend,
const char *little, const char *lend)
char * Perl_ninstr(const char *big, const char *bigend,
const char *little, const char *lend)
Nullch
Null character pointer. (No longer available when PERL_CORE is defined.) 👻
PL_na
A scratch pad variable in which to store a STRLEN value. Ngl it would have been better named something like PL_temp_strlen.
It's typically used with SvPV when you're actually planning to discard the returned length, hence the length is "Not Applicable", which is how this variable got its name.
BUT BEWARE 😱 — if this is used in a situation where something that's using it is in a call stack with something else that's using it, this variable would get zapped, leading to hard-to-diagnose errors.
It's usually more efficient to either declare a local variable and use that instead, or to use the SvPV_nolen macro. Do that bestie, save yourself the pain.
STRLEN PL_na
rninstr
Like ninstr, but instead finds the final (rightmost) occurrence of a sequence of bytes within another sequence, returning NULL if there's no such occurrence. Same needle-in-haystack energy, just searching from the other end. 🦋
char * rninstr(const char *big, const char *bigend,
const char *little, const char *lend)
char * Perl_rninstr(const char *big, const char *bigend,
const char *little, const char *lend)
savepv
savepvn
savepvs
savesvpv
savesharedpv
savesharedpvn
savesharedpvs
savesharedsvpv
Perl's version of strdup() (or strndup() would be, if it existed). The whole squad, together. 💖
Each of these returns a pointer to a newly allocated string which is a duplicate of the input string.
The forms differ in how the string to be copied is specified, and where the new memory is allocated from.
To prevent memory leaks, the memory allocated for the new string needs to be freed when no longer needed. Do this with theSafefreefunction, orSAVEFREEPV. Don't be the person who leaks, that's not the vibe. 😭
The forms whose names contain shared differ from the corresponding form without it in the name, only in that the memory in the former comes from memory shared between threads. This is needed because on some platforms — Windows, for example — all allocated memory owned by a thread is deallocated when that thread ends. So if you need that not to happen, you need to use the shared memory forms.
The string to copy in savepvs is a C language string literal surrounded by double quotes.
The string to copy in the forms whose name contains svpv comes from the PV in the SV argument sv, using SvPV().
The string to copy in the remaining forms comes from the pv argument.
In the case of savepv, the size of the string is determined by strlen(), which means it may not contain embedded NUL characters, and must have a trailing NUL.
In the case of savepvn, len gives the length of pv, hence it may contain embedded NUL characters. The copy will be guaranteed to have a trailing NUL added if not already present. 🎀
char * savepv ( const char *pv)
char * Perl_savepv (pTHX_ const char *pv)
char * savepvn ( const char *pv, Size_t len)
char * Perl_savepvn (pTHX_ const char *pv, Size_t len)
char* savepvs ( "literal string")
char * savesvpv ( SV *sv)
char * Perl_savesvpv (pTHX_ SV *sv)
char * savesharedpv ( const char *pv)
char * Perl_savesharedpv (pTHX_ const char *pv)
char * savesharedpvn ( const char * const pv,
const STRLEN len)
char * Perl_savesharedpvn (pTHX_ const char * const pv,
const STRLEN len)
char* savesharedpvs ( "literal string")
char * savesharedsvpv( SV *sv)
char * Perl_savesharedsvpv(pTHX_ SV *sv)
🌸 STR_WITH_LEN 🌸
okay so this one hands you back two comma-separated tokens: your literal string, and its length. it's a convenience macro, exists purely to help out in certain API calls. BUT — and this is the important bit bestie — you cannot use it as an argument to macros or functions that might, under some configurations, actually be macros. which means any API call where you use it needs the full Perl_xxx(aTHX_ ...) form. no shortcuts, no vibes, just vibes-adjacent syntax 💅
pair STR_WITH_LEN("literal string")
strEQ
Test two "NUL"-terminated strings to see if they're equal. Returns true or false. fr fr.
bool strEQ(char* s1, char* s2)
strGE
Test two "NUL"-terminated strings to see if the first one, s1, is greater than or equal to the second one, s2. Returns true or false.
bool strGE(char* s1, char* s2)
strGT
Test two "NUL"-terminated strings to see if the first one, s1, is greater than the second one, s2. Returns true or false.
bool strGT(char* s1, char* s2)
strLE
Test two "NUL"-terminated strings to see if the first one, s1, is less than or equal to the second one, s2. Returns true or false.
bool strLE(char* s1, char* s2)
STRLEN
Described in perlguts. that's it, that's the whole entry, go read perlguts 🫶
strLT
Test two "NUL"-terminated strings to see if the first one, s1, is less than the second one, s2. Returns true or false.
bool strLT(char* s1, char* s2)
strNE
Test two "NUL"-terminated strings to see if they're different. Returns true or false.
bool strNE(char* s1, char* s2)
strnEQ
Test two "NUL"-terminated strings to see if they're equal. the len parameter tells you how many bytes to compare. Returns true or false. (it's a wrapper for strncmp, so if you know strncmp you already know this one 💖)
bool strnEQ(char* s1, char* s2, STRLEN len)
strnNE
Test two "NUL"-terminated strings to see if they're different. the len parameter tells you how many bytes to compare. Returns true or false. (also a wrapper for strncmp.)
bool strnNE(char* s1, char* s2, STRLEN len)
✨ Zero ✨ / ### ✨ ZeroD ✨
wait no, same heading, let's do this properly:
Zero / ZeroD
The XSUB-writer's interface to the C memzero function. dest is the destination, nitems is the number of items, and type is the type. classic.
ZeroD is like Zero but it returns dest. it exists to encourage compilers to tail-call optimise, which is very generous of it honestly 👑
void Zero (void* dest, int nitems, type)
void * ZeroD(void* dest, int nitems, type)
🦋 SV Flags 🦋
SVt_IV, SVt_NULL, SVt_NV, SVt_PV
Type flag for scalars. See svtype.
SVt_PVAV
Type flag for arrays. See svtype.
SVt_PVCV
Type flag for subroutines. See svtype.
SVt_PVFM
Type flag for formats. See svtype.
SVt_PVGV
Type flag for typeglobs. See svtype.
SVt_PVHV
Type flag for hashes. See svtype.
SVt_PVIO
Type flag for I/O objects. See svtype.
SVt_PVIV, SVt_PVLV, SVt_PVMG, SVt_PVNV
Type flag for scalars. See svtype.
SVt_PVOBJ
NOTE: SVt_PVOBJ is experimental and may change or be removed without notice. it's giving "we're still deciding", don't build your whole personality on it 😭
Type flag for object instances. See svtype.
SVt_REGEXP
Type flag for regular expressions. See svtype.
🌈 svtype 🌈
An enum of flags for Perl types. these live in the file sv.h, inside the svtype enum. you test these flags with the SvTYPE macro.
The types are:
SVt_NULL
SVt_IV
SVt_NV
SVt_RV
SVt_PV
SVt_PVIV
SVt_PVNV
SVt_PVMG
SVt_INVLIST
SVt_REGEXP
SVt_PVGV
SVt_PVLV
SVt_PVAV
SVt_PVHV
SVt_PVCV
SVt_PVFM
SVt_PVIO
SVt_PVOBJ
these are most easily explained from the bottom up, so let's go, bestie 🧚
SVt_PVOBJ is for object instances of the new use feature 'class' kind. SVt_PVIO is for I/O objects, SVt_PVFM for formats, SVt_PVCV for subroutines, SVt_PVHV for hashes, and SVt_PVAV for arrays.
All the others are scalar types — meaning, things that can be bound to a $ variable. for these, the internal types are mostly orthogonal to types in the Perl language.
So hence, checking SvTYPE(sv) < SVt_PVAV is the best way to see whether something is a scalar. that's your vibe check right there ✨
SVt_PVGV represents a typeglob. if !SvFAKE(sv), then it's a real, incoercible typeglob. if SvFAKE(sv), then it's a scalar to which a typeglob has been assigned — and assigning to it again will stop it from being a typeglob. SVt_PVLV represents a scalar that delegates to another scalar behind the scenes. it's used, e.g., for the return value of substr and for tied hash and array elements. it can hold any scalar value, including a typeglob. SVt_REGEXP is for regular expressions. SVt_INVLIST is for Perl core internal use only.
SVt_PVMG represents a "normal" scalar (not a typeglob, not a regular expression, not a delegate). since most scalars don't need all the internal fields of a PVMG, we save memory by allocating smaller structs when possible. all the other types are just simpler forms of SVt_PVMG, with fewer internal fields. SVt_NULL can only hold undef. SVt_IV can hold undef, an integer, or a reference. (SVt_RV is an alias for SVt_IV, which exists for backward compatibility.) SVt_NV can hold undef or a double. (In builds that support headless NVs, these could also hold a reference via a suitable offset, in the same way that SVt_IV does, but this is not currently supported and seems to be a rare use case.) SVt_PV can hold undef, a string, or a reference. SVt_PVIV is a superset of SVt_PV and SVt_IV. SVt_PVNV is a superset of SVt_PV and SVt_NV. SVt_PVMG can hold anything SVt_PVNV can hold, but it may also be blessed or magical. iconic behaviour honestly 💅
💖 SV Handling 💖
AV_FROM_REF, CV_FROM_REF, GV_FROM_REF, HV_FROM_REF
The *V_FROM_REF macros extract the SvRV() from a given reference SV and return a suitably-cast to pointer to the referenced SV. when you're running under -DDEBUGGING, assertions also get applied that check that ref is definitely a reference SV that refers to an SV of the right type. safety first, always 🫶
AV * AV_FROM_REF(SV * ref)
CV * CV_FROM_REF(SV * ref)
GV * GV_FROM_REF(SV * ref)
HV * HV_FROM_REF(SV * ref)
BOOL_INTERNALS_sv_isbool
Checks if a SvBoolFlagsOK() sv is a bool. Note that it is the caller's responsibility to ensure that the sv is SvBoolFlagsOK() before calling this. this is only useful in specialized logic like serialization code where performance is critical and the flags have already been checked to be correct. almost always you should be using sv_isbool(sv) instead. like, almost always, bestie 🙈
bool BOOL_INTERNALS_sv_isbool(SV* sv)
BOOL_INTERNALS_sv_isbool_false
Checks if a SvBoolFlagsOK() sv is a false bool. Note that it is the caller's responsibility to ensure that the sv is SvBoolFlagsOK() before calling this. this is only useful in specialized logic like serialization code where performance is critical and the flags have already been checked to be correct. and this is NOT what you should use to check if an SV is "false" — for that you should be using !SvTRUE(sv) instead. do not come for me, I'm just the messenger 💀
bool BOOL_INTERNALS_sv_isbool_false(SV* sv)
BOOL_INTERNALS_sv_isbool_true
Checks if a SvBoolFlagsOK() sv is a true bool. Note that it is the caller's responsibility to ensure that the sv is SvBoolFlagsOK() before calling this. this is only useful in specialized logic like serialization code where performance is critical and the flags have already been checked to be correct. this is NOT what you should use to check if an SV is "true" — for that you should be using SvTRUE(sv) instead. same energy as the last one, we stay consistent 😌
bool BOOL_INTERNALS_sv_isbool_true(SV* sv)
boolSV
Returns a true SV if b is a true value, or a false SV if b is 0.
See also PL_sv_yes and PL_sv_no.
SV * boolSV(bool b)
croak_xs_usage
A specialised variant of croak() for emitting the usage message for xsubs
croak_xs_usage(cv, "eee_yow");
it works out the package name and subroutine name from cv, and then calls croak(). hence if cv is &ouch::awk, it would call croak as:
diag_listed_as: SKIPME
croak("Usage: %" SVf "::%" SVf "(%s)", "ouch" "awk",
"eee_yow");
void croak_xs_usage(const CV * const cv,
const char * const params)
void Perl_croak_xs_usage(const CV * const cv,
const char * const params)
CV_FROM_REF*
Described under AV_FROM_REF. go look up there, babe 👆
DEFSV
Returns the SV associated with $_
SV * DEFSV
DEFSV_set
Associate sv with $_
void DEFSV_set(SV * sv)
get_sv
Returns the SV of the specified Perl scalar. flags get passed to "gv_fetchpv". if GV_ADD is set and the Perl variable doesn't exist yet, then it will be created. if flags is zero and the variable doesn't exist, then NULL is returned. no variable, no SV, no problem 🌸
NOTE: the perl_get_sv() form is deprecated. we don't do that anymore bestie.
SV * get_sv( const char *name, I32 flags)
SV * Perl_get_sv(pTHX_ const char *name, I32 flags)
GV_FROM_REF* / HV_FROM_REF*
Described under AV_FROM_REF. yep, same place as before ✨
isGV_with_GP
Returns a boolean as to whether or not sv is a GV with a pointer to a GP (glob pointer). short, sweet, and very specific.
bool isGV_with_GP(SV * sv)
looks_like_number
Test if the content of an SV looks like a number (or is a number). Inf and Infinity are treated as numbers — so they will not issue a non-numeric warning — even if your atof() doesn't grok them. Get-magic is ignored. periodt.
I32 looks_like_number( SV * const sv)
I32 Perl_looks_like_number(pTHX_ SV * const sv)
🌈 MUTABLE_AV, MUTABLE_CV, MUTABLE_GV, MUTABLE_HV, MUTABLE_IO, MUTABLE_PTR, MUTABLE_SV 🌈
The MUTABLE_*() macros cast pointers to the types shown, in such a way (compiler permitting) that casting away const-ness will give you a warning. e.g.:
const SV *sv = ...;
AV *av1 = (AV*)sv; <== BAD: the const has been silently
cast away
AV *av2 = MUTABLE_AV(sv); <== GOOD: it may warn
so like, one of these is lying to you and the other one is being honest about it. pick the honest one 💅
💅 MUTABLE_PTR and the rest of the mutable-cast squad 💅
MUTABLE_PTR is the blueprint macro that all the other casts are built off of, bestie. The other ones that already come built-in just hand you back pointers to exactly what their names say they do. No surprises, no plot twists. ✨
AV * MUTABLE_AV (AV * p)
CV * MUTABLE_CV (CV * p)
GV * MUTABLE_GV (GV * p)
HV * MUTABLE_HV (HV * p)
IO * MUTABLE_IO (IO * p)
void * MUTABLE_PTR(void * p)
SV * MUTABLE_SV (SV * p)
🎁 newRV / newRV_inc
Okay so these two are literally the same function wearing different outfits. 👯 They wrap an SV up in an RV wrapper, and the reference count on the original SV goes up by one. Very giving, very generous. 🫶
SV * newRV ( SV * const sv)
SV * Perl_newRV (pTHX_ SV * const sv)
SV * newRV_inc( SV * const sv)
🙅 newRV_noinc
Same deal — makes an RV wrapper around an SV — except this time the original SV's reference count is not incremented. It stays exactly where it was. 💀
SV * newRV_noinc( SV * const tmpRef)
SV * Perl_newRV_noinc(pTHX_ SV * const tmpRef)
🌱 newSV
Fresh new SV, hot off the press. A non-zero len param means "hey, preallocate this many bytes of string space for it," and there's an extra byte reserved on top of that for the trailing NUL. Note that SvPOK is not set on the SV, even if you did allocate that string space. The new SV's reference count starts at 1. 🌈
Fun history moment: back in 5.9.3, newSV() replaced the older NEWSV() API and yeeted the first parameter x — that was a debug aid so callers could identify themselves. That whole thing got superseded by the build option PERL_MEM_LOG (peep "PERL_MEM_LOG" in perlhacktips). The older API is still hanging around though, for XS modules that support older perls. Nostalgic. 🥹
SV * newSV( const STRLEN len)
SV * Perl_newSV(pTHX_ const STRLEN len)
❌ newSV_false
Makes a new SV that is a boolean false. That's it, that's the whole vibe.
SV * newSV_false()
SV * Perl_newSV_false(pTHX)
✅ newSV_true
Makes a new SV that is a boolean true. Iconic counterpart energy.
SV * newSV_true()
SV * Perl_newSV_true(pTHX)
🏷️ newSV_type
New SV, type specified by you, refcount set to 1. Pick your fighter.
SV * newSV_type( const svtype type)
SV * Perl_newSV_type(pTHX_ const svtype type)
🫧 newSV_type_mortal
New mortal SV of the type you specify, refcount 1. ✨
This is basically the same as doing:
SV* sv = sv_2mortal(newSV_type(<some type>))
or:
SV* sv = sv_newmortal();
sv_upgrade(sv, <some_type>)
but it should be more efficient than both of them. (Unless sv_2mortal gets inlined at some point in the future, in which case, well. 😌)
SV * newSV_type_mortal( const svtype type)
SV * Perl_newSV_type_mortal(pTHX_ const svtype type)
🔘 newSVbool
Makes a new SV boolean.
SV * newSVbool( const bool bool_val)
SV * Perl_newSVbool(pTHX_ const bool bool_val)
🔑 newSVhek
Makes a new SV out of the hash key structure. It'll generate scalars that point to the shared string table wherever it can — so efficient, so slay. If hek is NULL, you get back a new (undefined) SV. 🦋
SV * newSVhek( const HEK * const hek)
SV * Perl_newSVhek(pTHX_ const HEK * const hek)
🔑✨ newSVhek_mortal
Same thing but mortal: new mortal SV from the hash key structure, points to the shared string table where possible, and returns a new (undefined) SV if hek is NULL.
This is more efficient than doing sv_2mortal(newSVhek( ... )) yourself, so like… just use this one, bestie. 💖
SV * newSVhek_mortal( const HEK * const hek)
SV * Perl_newSVhek_mortal(pTHX_ const HEK * const hek)
🔢 newSViv
New SV, integer copied into it, refcount set to 1. Math girlies rise. 🧮
SV * newSViv( const IV i)
SV * Perl_newSViv(pTHX_ const IV i)
🫧 newSVnv
New SV, floating point value copied into it, refcount set to 1. Decimals are valid too. 🍓
SV * newSVnv( const NV n)
SV * Perl_newSVnv(pTHX_ const NV n)
🧪 newSVpadname
NOTE: newSVpadname is experimental and may change or be removed without notice. So don't get attached, okay? 😭
Creates a new SV containing the pad name.
SV* newSVpadname(PADNAME *pn)
📝 newSVpv
New SV, copies a string into it (and yes, that string can contain NUL (\0) characters), refcount set to 1. If len is zero, Perl computes the length with strlen(). Which — important — means if you go that route, s can't have embedded NUL characters and has to have a terminating NUL byte. No cap. 🚨
This function can cause reliability issues if you're likely to pass in empty strings that aren't null terminated, because it'll run strlen on the string and potentially run past valid memory. Yikes.
newSVpvn is the safer alternative for non-NUL-terminated strings, and for string literals you want newSVpvs instead. This function works fine for NUL-terminated strings, but if you wanna dodge the if-statement about whether to call strlen, just use newSVpvn and call strlen yourself. 💅
SV * newSVpv( const char * const s, const STRLEN len)
SV * Perl_newSVpv(pTHX_ const char * const s, const STRLEN len)
🤝 newSVpv_share
Just like newSVpvn_share, except it takes a NUL-terminated string instead of a string/length pair. Sharing is caring fr fr.
SV * newSVpv_share( const char *s, U32 hash)
SV * Perl_newSVpv_share(pTHX_ const char *s, U32 hash)
🎨 newSVpvf
New SV, initialized with a string formatted just like sv_catpvf does it. Formatting era. ✨
SV * Perl_newSVpvf(pTHX_ const char * const pat, ...)
🎨🙈 newSVpvf_nocontext
Like newSVpvf, but it does not take a thread context (aTHX) parameter — so it's for situations where the caller doesn't already have the thread context. Low-maintenance king.
SV * newSVpvf_nocontext(const char * const pat, ...)
SV * Perl_newSVpvf_nocontext(const char * const pat, ...)
📦 newSVpvn
New SV with a string copied in, and this string may contain NUL characters (\0) plus other binary data. Refcount set to 1. Heads up: if len is zero, Perl creates a zero-length (Perl) string. And you are responsible for making sure the source buffer is at least len bytes long. If the buffer argument is NULL, the new SV comes out undefined. 🫠
SV * newSVpvn( const char * const s, const STRLEN len)
SV * Perl_newSVpvn(pTHX_ const char * const s, const STRLEN len)
🚩 newSVpvn_flags
New SV, copies a string into it (which may contain NUL (\0) characters), refcount set to 1. If len is zero, Perl makes a zero-length string. You're responsible for ensuring the source string is at least len bytes long. If the s argument is NULL, the new SV is undefined. 👑
Right now the only flag bits accepted are SVf_UTF8 and SVs_TEMP. If SVs_TEMP is set, sv_2mortal() gets called on the result before returning. If SVf_UTF8 is set, s is treated as being in UTF-8 and the SVf_UTF8 flag gets set on the new SV. And newSVpvn_utf8() is just a convenience wrapper for this function, defined as:
#define newSVpvn_utf8(s, len, u) \
newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0)
SV * newSVpvn_flags( const char * const s,
const STRLEN len,
const U32 flags)
SV * Perl_newSVpvn_flags(pTHX_ const char * const s,
const STRLEN len,
const U32 flags)
🪄 newSVpvn_share
New SV whose SvPVX_const points to a shared string in the string table. If the string isn't already in the table, it gets created first. This turns on the SvIsCOW flag (or READONLY and FAKE in 5.16 and earlier). If the hash parameter is non-zero, that value gets used; otherwise the hash is computed. You can grab the string's hash later off the SV with the SvSHARED_HASH macro. 🧠
The whole idea here: since the string table is used for shared hash keys, these strings will have SvPVX_const == HeKEY, and hash lookup gets to skip the string compare entirely. Efficiency ate. 🔥
SV * newSVpvn_share( const char *s, I32 len, U32 hash)
SV * Perl_newSVpvn_share(pTHX_ const char *s, I32 len, U32 hash)
🦋 newSVpvn_utf8
New SV with a string copied in (may contain NUL (\0) characters). If utf8 is true, it calls SvUTF8_on on the new SV. Implemented as a wrapper around newSVpvn_flags. 💜
SV* newSVpvn_utf8(const char* s, STRLEN len, U32 utf8)
🎀 newSVpvs
Like newSVpvn, but takes a literal string instead of a string/length pair. So convenient.
SV* newSVpvs("literal string")
🎀🚩 newSVpvs_flags
Like newSVpvn_flags, but takes a literal string instead of a string/length pair.
SV* newSVpvs_flags("literal string", U32 flags)
🎀🪄 newSVpvs_share
Like newSVpvn_share, but takes a literal string instead of a string/length pair — and skips the hash parameter entirely. Simplify, bestie. ✨
SV* newSVpvs_share("literal string")
🫙 newSVpvz
New SV initialized with an empty string, exactly like newSVpvs(""), except it has enough available space to hold a string of len bytes (plus a trailing NUL) with no growing needed. And it differs from newSV in that the string is actually defined and initialized. 🌸
The reference count for the new SV is set to 1.
SV * newSVpvz( const STRLEN len)
SV * Perl_newSVpvz(pTHX_ const STRLEN len)
🪞 newSVrv
Makes a new SV for the existing RV rv to point to. If rv isn't an RV, it gets upgraded into one. If classname is non-null, the new SV gets blessed into the specified package. The new SV is returned and its refcount is 1 — and that refcount 1 is owned by rv. Also peep newRV_inc() and newRV_noinc() for creating a new RV the proper way. 👑
SV * newSVrv( SV * const rv,
const char * const classname)
SV * Perl_newSVrv(pTHX_ SV * const rv,
const char * const classname)
👯♀️ newSVsv / newSVsv_flags / newSVsv_nomg
These make a new SV that's an exact duplicate of the original SV (using sv_setsv). Twin energy. 🪞✨
The only difference between them: newSVsv does 'get' magic; newSVsv_nomg skips all magic entirely; and newSVsv_flags lets you explicitly set a flags parameter. Pick the vibe you need. 💅
SV * newSVsv ( SV * const old)
SV * Perl_newSVsv (pTHX_ SV * const old)
SV * newSVsv_flags( SV * const old, I32 flags)
SV * Perl_newSVsv_flags(pTHX_ SV * const old, I32 flags)
SV * newSVsv_nomg ( SV * const old)
✨ newSVuv ✨
Okay bestie, this one makes a brand new SV and copies an unsigned integer into it. And the ref count for that shiny new SV? It gets set to 1. Periodt. 💅
SV * newSVuv( const UV u)
SV * Perl_newSVuv(pTHX_ const UV u)
🌸 Nullsv 🌸
A Null SV pointer. That's it, that's the whole vibe. (Heads up though: this one's no longer available when PERL_CORE is defined. 👀)
💖 PL_sv_no 💖
This is the "false" SV, bestie. It is readonly, so do not try to mess with it. See also PL_sv_yes. And always, always refer to this as &PL_sv_no. 🫶
SV PL_sv_no
🦋 PL_sv_undef 🦋
This is the "undef" SV. It is readonly. Always refer to this as &PL_sv_undef. No cap.
SV PL_sv_undef
⭐ PL_sv_yes ⭐
This is the "true" SV. It is readonly. See also PL_sv_no. Always refer to this as &PL_sv_yes. It's giving main character energy fr fr.
SV PL_sv_yes
🎀 PL_sv_zero 🎀
This readonly SV has a zero numeric value and a "0" string value. So it's similar to PL_sv_no… except for its string value, iykyk. You can use it as a cheap alternative to mXPUSHi(0) for example. Always refer to this as &PL_sv_zero. Introduced in 5.28. ✨
SV PL_sv_zero
🌈 SAVE_DEFSV 🌈
Localize $_. That's the whole job. See "Localizing changes" in perlguts if you wanna go deeper. 💜
void SAVE_DEFSV
🔥 sortsv 🔥
In-place sort an array of SV pointers with the given comparison routine. Slay.
Currently this always uses mergesort. And if you want something more flexible, go look at sortsv_flags. 👑
void sortsv( SV **array, size_t num_elts,
SVCOMPARE_t cmp)
void Perl_sortsv(pTHX_ SV **array, size_t num_elts,
SVCOMPARE_t cmp)
💫 sortsv_flags 💫
Same energy as sortsv — in-place sort an array of SV pointers with the given comparison routine — but this one lets you pass in various SORTf_* flag options. Versatile queen. 💅
void sortsv_flags( SV **array, size_t num_elts,
SVCOMPARE_t cmp, U32 flags)
void Perl_sortsv_flags(pTHX_ SV **array, size_t num_elts,
SVCOMPARE_t cmp, U32 flags)
🌸 SV 🌸
Described in perlguts. Go read it bestie, it's all there. 🫶
🧚 sv_backoff 🧚
Remove any string offset. Lowkey you should normally use the SvOOK_off macro wrapper instead, okay? Okay.
void sv_backoff(SV * const sv)
void Perl_sv_backoff(SV * const sv)
👑 sv_bless 👑
Blesses an SV into a specified package. The SV must be an RV. The package must be designated by its stash (see gv_stashpv). And the reference count of the SV? Unaffected. Iconic. ✨
SV * sv_bless( SV * const sv,
NOCHECK HV * const stash)
SV * Perl_sv_bless(pTHX_ SV * const sv,
NOCHECK HV * const stash)
🍓 sv_catpv, sv_catpv_flags, sv_catpv_mg, sv_catpv_nomg, sv_catpvn, sv_catpvn_flags, sv_catpvn_mg, sv_catpvn_nomg, sv_catpvn_nomg_maybeutf8, sv_catpvs, sv_catpvs_flags, sv_catpvs_mg, sv_catpvs_nomg 🍓
Okay so these each concatenate a string onto the end of the string which is in dsv. The way that they differ is how the catenated string is specified and how they handle magic and UTF-8ness. 💅
- In the
"pvs"forms, the catenated string is a C language string literal, enclosed in double quotes. - In the
"pvn"forms,sstrpoints to the first byte of the string to concatenate, and an additional parameter,len, specifies the number of bytes to copy. Hence,sstrmay contain embedded-NUL characters. The caller must make suresstrcontains at leastlenbytes. ⚠️ - In the plain
"pv"forms, the catenated string is a C language NUL-terminated string. - The
"_mg"forms perform both 'get' and 'set' magic ondsv. - The
"_nomg"forms skip all magic. - The other forms perform only 'get' magic.
- The
"_flags"forms have an extra parameter,flags, which allows you to also override the UTF-8 handling. By supplying theSV_CATBYTESflag, the appended string is interpreted as plain bytes; by supplying instead theSV_CATUTF8flag, it will be interpreted as UTF-8, anddsvwill be upgraded to UTF-8 if necessary. 🌈 sv_catpvn_nomg_maybeutf8has an extra boolean parameter,is_utf8, which iftrueindicates thatsstris encoded in UTF-8; otherwise not.- For all other forms, the string appended is assumed to be valid UTF-8 if and only if the
dsvhas the UTF-8 status set.
void sv_catpv ( SV * const dsv,
const char *sstr)
void Perl_sv_catpv (pTHX_ SV * const dsv,
const char *sstr)
void sv_catpv_flags ( SV * const dsv,
const char *sstr,
const I32 flags)
void Perl_sv_catpv_flags (pTHX_ SV * const dsv,
const char *sstr,
const I32 flags)
void sv_catpv_mg ( SV * const dsv,
const char * const sstr)
void Perl_sv_catpv_mg (pTHX_ SV * const dsv,
const char * const sstr)
void sv_catpv_nomg ( SV * const dsv,
const char * sstr
)
void sv_catpvn ( SV * const dsv,
const char *sstr,
STRLEN len)
void Perl_sv_catpvn (pTHX_ SV * const dsv,
const char *sstr,
STRLEN len)
void sv_catpvn_flags ( SV * const dsv,
const char *sstr,
const STRLEN len,
const I32 flags)
void Perl_sv_catpvn_flags (pTHX_ SV * const dsv,
const char *sstr,
const STRLEN len,
const I32 flags)
void sv_catpvn_mg ( SV * const dsv,
const char *sstr,
STRLEN len)
void Perl_sv_catpvn_mg (pTHX_ SV * const dsv,
const char *sstr,
STRLEN len)
void sv_catpvn_nomg ( SV * const dsv,
const char * sstr,
const STRLEN len
)
void sv_catpvn_nomg_maybeutf8( SV * const dsv,
const char *sstr,
const STRLEN len,
const I32 flags
)
void sv_catpvs ( SV * const dsv,
"literal string")
void sv_catpvs_flags ( SV * const dsv,
"literal string",
I32 flags)
void sv_catpvs_mg ( SV * const dsv,
"literal string")
void sv_catpvs_nomg ( SV * const dsv,
"literal string")
💜 sv_catpvf, sv_catpvf_nocontext, sv_catpvf_mg, sv_catpvf_mg_nocontext, sv_vcatpvf, sv_vcatpvf_mg, sv_vcatpvfn, sv_vcatpvfn_flags 💜
These each append to sv the result of formatting their arguments using pat as the sprintf-like pattern. And here's the thing — they assume that pat has the same UTF8ness as sv. So it's the caller's responsibility to make sure that's true. Don't come for me if you don't. 😭
If the destination sv isn't already in UTF-8, but the appended data contains "wide" characters, then sv will be converted to be UTF-8. An example is the %c format with the code point > 255. (This is an enhancement to what libc sprintf would do in this situation, slay.) Other examples are given below.
The forms differ in how their arguments are specified and in the handling of magic. ✨
sv_vcatpvfn_flags is the most general, and all the other forms are implemented by eventually calling it. It's giving final boss.
It has two sets of argument lists, only one of which is used in any given call. The first set, args, is an encapsulated argument list of pointers to C strings. If it is NULL, the other list, svargs, is used; it is an array of pointers to SV's. sv_count gives how many there are in the list. 👑
See sprintf(3) for details on how the formatting is done. Some platforms support extensions to the standard C99 definition of this function. None of those are supported by Perl. For example, neither "'" (to get digit grouping), nor "I" (to get alternate digits) are supported. Nope, not those. 🙈
Also, argument reordering (using format specifiers like "%2$d" or "%*2$d") is supported only when using the svargs array of SVs; an exception is raised if arg is not NULL and pat contains the "$" reordering specifier. ⚠️
* maybe_tainted is supposed to be set when running with taint checks enabled if the results are untrustworthy (often due to the use of locales). However, this is not currently implemented. This argument is not used. 💀
patlen gives the length in bytes of pat. Currently, the pattern must be NUL-terminated anyway.
flags is used to specify which magic to handle or to skip, by setting or clearing the SV_GMAGIC and/or SV_SMAGIC flags.
Plain sv_vcatpvfn just calls sv_vcatpvfn_flags setting both the SV_GMAGIC and SV_SMAGIC flags, so it always handles both set and get magic.
All the remaining forms handle 'get' magic; the forms whose name contains _mg additionally handle 'set' magic.
When using the svargs array, if any of the SVs in it have their UTF-8 flag set, sv will be converted to be so too, as necessary. 🌸
None of the other forms even look at the svargs array, so bestie, argument reordering is just not a thing with them, periodt. Their arguments are generally treated as having the same UTF8ness as the destination sv, though some Perl extensions to the standard %format lineup can override that (peep "Formatted Printing of Strings" in perlguts and the sections right next to it, iykyk).
The forms with _no_context in the name don't take a thread context (aTHX) parameter — they're for when the caller doesn't already have the thread context on hand. The forms with vcat in the name use an encapsulated argument list; everything else takes sprintf-style args. And that's it, no other differences between the forms. 💅
void Perl_sv_catpvf (pTHX_ SV * const sv,
const char * const pat,
...)
void sv_catpvf_nocontext ( SV * const sv,
const char * const pat,
...)
void Perl_sv_catpvf_nocontext ( SV * const sv,
const char * const pat,
...)
void Perl_sv_catpvf_mg (pTHX_ SV * const sv,
const char * const pat,
...)
void sv_catpvf_mg_nocontext( SV * const sv,
const char * const pat,
...)
void Perl_sv_catpvf_mg_nocontext( SV * const sv,
const char * const pat,
...)
void sv_vcatpvf ( SV * const sv,
const char * const pat,
va_list * const args)
void Perl_sv_vcatpvf (pTHX_ SV * const sv,
const char * const pat,
va_list * const args)
void sv_vcatpvf_mg ( SV * const sv,
const char * const pat,
va_list * const args)
void Perl_sv_vcatpvf_mg (pTHX_ SV * const sv,
const char * const pat,
va_list * const args)
void sv_vcatpvfn ( SV * const sv,
const char * const pat,
const STRLEN patlen,
va_list * const args,
SV ** const svargs,
const Size_t sv_count,
bool * const maybe_tainted)
void Perl_sv_vcatpvfn (pTHX_ SV * const sv,
const char * const pat,
const STRLEN patlen,
va_list * const args,
SV ** const svargs,
const Size_t sv_count,
bool * const maybe_tainted)
void sv_vcatpvfn_flags ( SV * const sv,
const char * const pat,
const STRLEN patlen,
va_list * const args,
SV ** const svargs,
const Size_t sv_count,
bool * const maybe_tainted,
const U32 flags)
void Perl_sv_vcatpvfn_flags (pTHX_ SV * const sv,
const char * const pat,
const STRLEN patlen,
va_list * const args,
SV ** const svargs,
const Size_t sv_count,
bool * const maybe_tainted,
const U32 flags)
sv_catpvn*
sv_catpvn_flags*
sv_catpvn_mg*
sv_catpvn_nomg*
sv_catpvn_nomg_maybeutf8*
sv_catpvs*
sv_catpvs_flags*
sv_catpvs_mg*
sv_catpvs_nomg*
All of these cuties are described up under "sv_catpv". Go look there, bestie 🫶
sv_catsv
sv_catsv_flags
sv_catsv_mg
sv_catsv_nomg
These ones concatenate the string from SV sstr onto the end of the string sitting in SV dsv. If sstr is null, they're total no-ops — nothing happens, we love a chill function. Otherwise, only dsv gets modified. ✨
They differ only in what magic they do, and the way that they do it is:
sv_catsv_mgperforms 'get' magic on both SVs before the copy, and 'set' magic ondsvafterwards.sv_catsvperforms just 'get' magic, on both SVs.sv_catsv_nomgskips all magic. Zero magic. None. 🙈sv_catsv_flagshas an extraflagsparameter which lets you useSV_GMAGICand/orSV_SMAGICto specify any combination of magic handling (though either both or neither SV will have 'get' magic applied to it, so no picking favourites 👑).
sv_catsv, sv_catsv_mg, and sv_catsv_nomg are all implemented in terms of sv_catsv_flags.
void sv_catsv ( SV * const dsv, SV * const sstr)
void Perl_sv_catsv (pTHX_ SV * const dsv, SV * const sstr)
void sv_catsv_flags( SV * const dsv, SV * const sstr,
const I32 flags)
void Perl_sv_catsv_flags(pTHX_ SV * const dsv, SV * const sstr,
const I32 flags)
void sv_catsv_mg ( SV * const dsv, SV * const sstr)
void Perl_sv_catsv_mg (pTHX_ SV * const dsv, SV * const sstr)
void sv_catsv_nomg ( SV * const dsv, SV * const sstr)
SV_CHECK_THINKFIRST
Strips any encumbrances off sv that need to get handled before it's allowed to be modifiable. Like, say it's Copy on Write (COW) — okay bestie, now is the moment to actually make that copy. 🐄
But if you already know you're about to change the PV value of sv, use "SV_CHECK_THINKFIRST_COW_DROP" instead, so you skip the write that would've just been immediately written over anyway. Efficient queen. 💅
void SV_CHECK_THINKFIRST(SV * sv)
SV_CHECK_THINKFIRST_COW_DROP
Call this when you're about to replace the PV value in sv, which is potentially copy-on-write. It stops any sharing with other SVs, so no Copy on Write (COW) actually happens at all. That COW would've been useless anyway since it'd instantly get changed into something else — so we're dropping it, iconic behaviour honestly. This function also clears out any other encumbrances that would be a problem when changing sv. 🌸
void SV_CHECK_THINKFIRST_COW_DROP(SV * sv)
sv_chop
Efficient removal of characters from the beginning of the string buffer. SvPOK(sv), or at minimum SvPOKp(sv), has to be true, and ptr must point to somewhere inside the string buffer. ptr becomes the first character of the adjusted string. Uses the OOK hack. On return, only SvPOK(sv) and SvPOKp(sv) among the OK flags will be true.
⚠️ Beware, bestie: after this function returns,ptrandSvPVX_const(sv)may no longer refer to the same chunk of data. They're not besties anymore, it's giving exes. 😭
And the unfortunate similarity of this function's name to Perl's chop operator? Strictly coincidental. Not related. This function works from the left; chop works from the right. Two totally different directions, two totally different girls. 🦋
void sv_chop( SV * const sv, const char * const ptr)
void Perl_sv_chop(pTHX_ SV * const sv, const char * const ptr)
sv_clear
Clears an SV: calls any destructors, frees up all the memory the body was using, and frees the body itself. The SV's head is not freed, though its type gets set to all 1's so nobody inadvertently assumes it's still live during global destruction and such. This function should only be called when REFCNT is zero — zero, bestie, not one, not "basically zero". Most of the time you'll actually want to call SvREFCNT_dec instead. 🫶
void sv_clear( SV * const orig_sv)
void Perl_sv_clear(pTHX_ SV * const orig_sv)
sv_cmp
sv_cmp_flags
Each of these compares the strings in two SVs, returning -1, 0, or 1 to tell you whether the string in sv1 is less than, equal to, or greater than the string in sv2. They're UTF-8 and 'use bytes' aware, and will coerce their arguments to strings if necessary. Slay, flexible. ✨
sv_cmp always handles 'get' magic. sv_cmp_flags only does so if flags contains SV_GMAGIC. Other than that, the two forms behave identically.
Also peep sv_cmp_locale if that's your vibe.
I32 sv_cmp ( SV * const sv1, SV * const sv2)
I32 Perl_sv_cmp (pTHX_ SV * const sv1, SV * const sv2)
I32 sv_cmp_flags( SV * const sv1, SV * const sv2,
const U32 flags)
I32 Perl_sv_cmp_flags(pTHX_ SV * const sv1, SV * const sv2,
const U32 flags)
sv_cmp_locale
sv_cmp_locale_flags
These each compare the strings in two SVs in a locale-aware manner, returning -1, 0, or 1 to say whether the string in sv1 is less than, equal to, or greater than the string in sv2. 🌈
They're UTF-8 and 'use bytes' aware, and will coerce their args to strings if necessary.
sv_cmp_locale always handles 'get' magic. sv_cmp_locale_flags only does so if flags contains SV_GMAGIC. Otherwise the two forms behave identically.
Also see sv_cmp, they're a matched set. 👑
I32 sv_cmp_locale ( SV * const sv1,
SV * const sv2)
I32 Perl_sv_cmp_locale (pTHX_ SV * const sv1,
SV * const sv2)
I32 sv_cmp_locale_flags( SV * const sv1,
SV * const sv2,
const U32 flags)
I32 Perl_sv_cmp_locale_flags(pTHX_ SV * const sv1,
SV * const sv2,
const U32 flags)
sv_collxfrm
sv_collxfrm_flags
These each add Collate Transform magic to an SV if it doesn't already have it. If it's already got the magic, they just leave it be — no double-dipping. 💖
Any scalar variable can carry PERL_MAGIC_collxfrm magic, bestie, and what lives in there is the scalar's own data — just transformed into a format where a plain old memory comparison lines up with the locale's collation rules. iykyk ✨
sv_collxfrm always handles 'get' magic. sv_collxfrm_flags only does that when flags contains SV_GMAGIC. Other than that lil difference, the two behave identically, fr fr.
char * sv_collxfrm ( SV * const sv,
STRLEN * const nxp)
char * Perl_sv_collxfrm (pTHX_ SV * const sv,
STRLEN * const nxp)
char * sv_collxfrm_flags( SV * const sv,
STRLEN * const nxp,
I32 const flags)
char * Perl_sv_collxfrm_flags(pTHX_ SV * const sv,
STRLEN * const nxp,
I32 const flags)
✨ sv_copypv ✨
✨ sv_copypv_flags ✨
✨ sv_copypv_nomg ✨
These ones copy a stringified representation of the source SV into the destination SV 💅. They automatically coerce numeric values into strings for you. And they are guaranteed to preserve the UTF8 flag — even when it's coming from an overloaded object. Vibe-wise they're similar to sv_2pv[_flags], but plot twist: they operate directly on an SV instead of just on the string. Most of the time they just hand the work to sv_2pv_flags, except in the cases where doing that would lose the UTF-8'ness of the PV. Not on their watch 🫶
The three forms differ only in whether or not they do 'get magic' on sv:
sv_copypv_nomg— skips 'get magic', periodt.sv_copypv— performs it.sv_copypv_flags— performs it if theSV_GMAGICbit is set inflags, and doesn't if that bit is cleared.
void sv_copypv ( SV * const dsv, SV * const ssv)
void Perl_sv_copypv (pTHX_ SV * const dsv, SV * const ssv)
void sv_copypv_flags( SV * const dsv, SV * const ssv,
const I32 flags)
void Perl_sv_copypv_flags(pTHX_ SV * const dsv, SV * const ssv,
const I32 flags)
void sv_copypv_nomg ( SV * const dsv, SV * const ssv)
🦋 sv_dec 🦋
🦋 sv_dec_nomg 🦋
These auto-decrement the value sitting in the SV, doing string-to-numeric conversion if they have to. Both of them handle operator overloading, no cap.
The only difference between them:
sv_dechandles 'get' magic.sv_dec_nomgskips 'get' magic.
void sv_dec ( SV * const sv)
void Perl_sv_dec (pTHX_ SV * const sv)
void sv_dec_nomg( SV * const sv)
void Perl_sv_dec_nomg(pTHX_ SV * const sv)
👑 sv_derived_from 👑
👑 sv_derived_from_hv 👑
👑 sv_derived_from_pv 👑
👑 sv_derived_from_pvn 👑
👑 sv_derived_from_sv 👑
Each of these returns a boolean telling you whether sv is derived from the specified class at the C level. If you wanna check derivation at the Perl level instead, just call isa() as a normal Perl method, bestie.
In sv_derived_from_hv, the class name is HvNAME(hv) (which would presumably represent a stash). Its UTF8ness is HvNAMEUTF8(hv).
In sv_derived_from and sv_derived_from_pv, the class name is given by name, which is a NUL-terminated C string. And in sv_derived_from, that name is never considered to be encoded as UTF-8.
The remaining forms differ only in how the class name is specified — they all have a flags parameter. Right now, the only significant value for it is SVf_UTF8, to indicate that the class name is encoded as such.
In sv_derived_from_sv, the class name gets extracted from namesv. This one is the preferred form, periodt. The class name is considered to be in UTF-8 if namesv is marked as such.
In sv_derived_from_pvn, len gives the length of name, so name may contain embedded NUL characters. Sneaky but valid 💀
bool sv_derived_from ( SV *sv,
const char * const name)
bool Perl_sv_derived_from (pTHX_ SV *sv,
const char * const name)
bool sv_derived_from_hv ( SV *sv, HV *hv)
bool Perl_sv_derived_from_hv (pTHX_ SV *sv, HV *hv)
bool sv_derived_from_pv ( SV *sv,
const char * const name,
U32 flags)
bool Perl_sv_derived_from_pv (pTHX_ SV *sv,
const char * const name,
U32 flags)
bool sv_derived_from_pvn( SV *sv,
const char * const name,
const STRLEN len, U32 flags)
bool Perl_sv_derived_from_pvn(pTHX_ SV *sv,
const char * const name,
const STRLEN len, U32 flags)
bool sv_derived_from_sv ( SV *sv, SV *namesv,
U32 flags)
bool Perl_sv_derived_from_sv (pTHX_ SV *sv, SV *namesv,
U32 flags)
🌸 sv_does 🌸
Like sv_does_pv, except it doesn't take a flags parameter. That's it, that's the whole note 🎀
bool sv_does( SV *sv, const char * const name)
bool Perl_sv_does(pTHX_ SV *sv, const char * const name)
🌸 sv_does_pv 🌸
Like sv_does_sv, but it takes a nul-terminated string instead of an SV. Iconic little shortcut.
bool sv_does_pv( SV *sv, const char * const name,
U32 flags)
bool Perl_sv_does_pv(pTHX_ SV *sv, const char * const name,
U32 flags)
🌸 sv_does_pvn 🌸
Like sv_does_sv, but it takes a string/length pair instead of an SV. Fancy.
bool sv_does_pvn( SV *sv, const char * const name,
const STRLEN len, U32 flags)
bool Perl_sv_does_pvn(pTHX_ SV *sv, const char * const name,
const STRLEN len, U32 flags)
🌈 sv_does_sv 🌈
Returns a boolean indicating whether the SV performs a specific, named role. And the SV here can be either a Perl object or the name of a Perl class — flexible king behavior 👑
bool sv_does_sv( SV *sv, SV *namesv, U32 flags)
bool Perl_sv_does_sv(pTHX_ SV *sv, SV *namesv, U32 flags)
💜 sv_eq 💜
💜 sv_eq_flags 💜
These each return a boolean saying whether or not the strings inside the two SVs are equal. If 'use bytes' is in effect, the comparison is byte-by-byte; otherwise it's character-by-character. And each one will coerce its args to strings if it needs to, no stress.
They differ only in that sv_eq always processes get magic, while sv_eq_flags processes get magic only when the flags parameter has the SV_GMAGIC bit set.
⚠️ Heads up bestie: these functions do not handle operator overloading. If you want versions that do, go peepsv_streqorsv_streq_flagsinstead.
I32 sv_eq ( SV *sv1, SV *sv2)
I32 Perl_sv_eq (pTHX_ SV *sv1, SV *sv2)
I32 sv_eq_flags( SV *sv1, SV *sv2, const U32 flags)
I32 Perl_sv_eq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
🎀 sv_force_normal 🎀
🎀 sv_force_normal_flags 🎀
These undo all kinds of fakery on an SV, where "fakery" means it's doing "more than" a string. Let's break down every flavor of fake, because there are a few:
- if the PV is a shared string — make a private copy.
- if we're a ref — stop refing. This happens by calling
sv_unref_flags. Insv_force_normal_flags, theflagsparameter gets passed along to that function. - if we're a glob — downgrade to an
xpvmg. - if we're a copy-on-write scalar — this is the on-write time when we actually do the copy, and it's also used locally.
- if this is a vstring — drop the vstring magic.
- in
sv_force_normal_flags, ifSV_COW_DROP_PVis set inflags— a copy-on-write scalar drops its PV buffer (if it has one) and becomesSvPOK_offrather than making a copy. (This gets used when the scalar is about to be set to some other value.)
Other than all of that above, the two forms behave identically. That's because sv_force_normal literally just calls sv_force_normal_flags with flags set to 0. Lazy? No, efficient 💅
void sv_force_normal ( SV *sv)
void Perl_sv_force_normal (pTHX_ SV *sv)
void sv_force_normal_flags( SV * const sv,
const U32 flags)
void Perl_sv_force_normal_flags(pTHX_ SV * const sv,
const U32 flags)
🍓 sv_free 🍓
Decrements an SV's reference count, and if that count drops all the way to zero, it calls sv_clear to invoke destructors and free up any memory used by the body. And finally — the grand finale — it deallocates the SV's head itself. Normally this gets called via the wrapper macro SvREFCNT_dec, not directly, iykyk.
void sv_free( SV * const sv)
void Perl_sv_free(pTHX_ SV * const sv)
🧚 sv_get_backrefs 🧚
⚠️ NOTE: sv_get_backrefs is experimental and may change or be removed without notice. Do not build your whole personality on it, bestie 😭
If sv is the target of a weak reference, then it returns the back references structure associated with the sv; otherwise it returns NULL.
When it returns a non-null result, the type of the return actually matters:
- If it's an AV, then the elements of the AV are the weak reference RVs which point at this item.
- If it's any other type, then the item itself is the weak reference.
See also Perl_sv_add_backref(), Perl_sv_del_backref(), Perl_sv_kill_backrefs() — the whole backref squad 🫶
SV * sv_get_backrefs(SV * const sv)
SV * Perl_sv_get_backrefs(SV * const sv)
🦋 sv_gets 🦋
Gets a line from the filehandle and stores it into the SV, optionally appending to the string that's already chillin' in there. If append is not 0, the line gets appended to the SV instead of overwriting it. And append should be set to the byte offset where the appended string should start in the SV — typically, SvCUR(sv) is a suitable choice. Cute, right? ✨
char * sv_gets( SV * const sv, PerlIO * const fp,
SSize_t append)
char * Perl_sv_gets(pTHX_ SV * const sv, PerlIO * const fp,
SSize_t append)
🔥 sv_inc 🔥
🔥 sv_inc_nomg 🔥
These auto-increment the value in the SV, doing string-to-numeric conversion if necessary. Both of them handle operator overloading, obviously 💅
They differ only in that sv_inc performs 'get' magic, while sv_inc_nomg skips any magic.
void sv_inc ( SV * const sv)
void Perl_sv_inc (pTHX_ SV * const sv)
void sv_inc_nomg( SV * const sv)
void Perl_sv_inc_nomg(pTHX_ SV * const sv)
"sv_insert"</p>
"sv_insert_flags"
okay so these two 💖 they insert AND/OR replace a string at a given offset/length inside the SV. Think of it as the C-level cousin of Perl's own substr() — you're taking littlelen bytes starting at little, and those replace len bytes of the string in bigstr starting at offset. And yes bestie, they handle get magic for you 🪄
sv_insert_flags is literally the same as plain sv_insert, EXCEPT it's got that extra flags param, and those flags get handed straight to the SvPV_force_flags operation that gets internally applied to bigstr. that's the whole difference, no cap.
void sv_insert ( SV * const bigstr,
const STRLEN offset,
const STRLEN len,
const char * const little,
const STRLEN littlelen)
void Perl_sv_insert (pTHX_ SV * const bigstr,
const STRLEN offset,
const STRLEN len,
const char * const little,
const STRLEN littlelen)
void sv_insert_flags( SV * const bigstr,
const STRLEN offset,
const STRLEN len,
const char *little,
const STRLEN littlelen,
const U32 flags)
void Perl_sv_insert_flags(pTHX_ SV * const bigstr,
const STRLEN offset,
const STRLEN len,
const char *little,
const STRLEN littlelen,
const U32 flags)
"sv_isa"
returns a boolean telling you whether the SV is blessed into the class you named. that's it, that's the vibe check ✨
🚨 but like, heads up: this does NOT check for subtypes or method overloading. none of that. nope.
If you actually want to verify an inheritance relationship the same way the isa operator does — respecting any isa() method overloading — use sv_isa_sv instead. Or if you wanna test directly on the actual object type, that's sv_derived_from_sv. different tools, different jobs, slay 💅
int sv_isa( SV *sv, const char * const name)
int Perl_sv_isa(pTHX_ SV *sv, const char * const name)
"sv_isa_sv"
returns a boolean for whether the SV is an object reference AND is derived from the specified class, and it does respect any isa() method overloading the thing might have. it returns false if sv isn't a reference to an object, or if it isn't derived from the class you named. simple as 🫶
this is literally the function that implements the behaviour of the isa operator. main character energy fr.
does not invoke magic on sv. noted. 📝
⚠️ don't mix this up with the oldersv_isafunction!! that one doesn't use an overloadedisa()method, and it also won't check subclassing. they are NOT the same girl.
bool sv_isa_sv( SV *sv, SV *namesv)
bool Perl_sv_isa_sv(pTHX_ SV *sv, SV *namesv)
"sv_isobject"
returns a boolean saying whether the SV is an RV pointing at a blessed object. and if the SV isn't an RV, OR the object isn't blessed? false. both those cases, false. 💀
int sv_isobject( SV *sv)
int Perl_sv_isobject(pTHX_ SV *sv)
"sv_len"
returns the length of the string inside the SV 🌸 it handles magic and type coercion, and sets the UTF8 flag appropriately — she does the whole routine. also peep SvCUR if you want raw access to the xpv_cur slot instead, iykyk.
STRLEN sv_len( SV * const sv)
STRLEN Perl_sv_len(pTHX_ SV * const sv)
"sv_len_utf8"
"sv_len_utf8_nomg"
these return the number of characters in the string in an SV, counting wide UTF-8 bytes as a single character 🦋 (so a multi-byte char counts as one, not many). both of them handle type coercion.
the only difference between them? sv_len_utf8 performs 'get' magic. sv_len_utf8_nomg skips any magic entirely. that's the whole tea ☕
STRLEN sv_len_utf8 ( SV * const sv)
STRLEN Perl_sv_len_utf8 (pTHX_ SV * const sv)
STRLEN sv_len_utf8_nomg( SV * const sv)
STRLEN Perl_sv_len_utf8_nomg(pTHX_ SV * const sv)
"sv_magic"
adds magic to an SV ✨ first it upgrades sv to type SVt_PVMG if it needs to, then it adds a brand new magic item of type how to the head of the magic list. head. not tail. noted bestie 💅
for the full deets on how the name and namlen arguments get handled, go read sv_magicext — which, fun fact, sv_magic now calls.
👑 you needsv_magicextif you wanna add magic toSvREADONLYSVs, or if you wanna add more than one instance of the samehow.sv_magicwill not do that for you, periodt.
void sv_magic( SV * const sv, SV * const obj,
const int how, const char * const name,
const I32 namlen)
void Perl_sv_magic(pTHX_ SV * const sv, SV * const obj,
const int how, const char * const name,
const I32 namlen)
"sv_magicext"
adds magic to an SV, upgrading it if necessary, applies the vtable you supply, and returns a pointer to the magic that got added 🎀
the tea here: sv_magicext allows things that sv_magic simply will not. specifically:
- you can add magic to
SvREADONLYSVs 🔒 - you can add more than one instance of the same
how🔁
and here's the naming rules, listen up:
- if
namlenis greater than zero → asavepvncopy ofnameis stored. - if
namlenis zero →nameis stored as-is. - and as another special case — if
(name && namlen == HEf_SVKEY)→ thennameis assumed to contain anSV*and is stored as-is, with itsREFCNTincremented.
(this is now used as a subroutine by sv_magic, btw. iconic.)
MAGIC * sv_magicext( SV * const sv, SV * const obj,
const int how,
const MGVTBL * const vtbl,
const char * const name,
const I32 namlen)
MAGIC * Perl_sv_magicext(pTHX_ SV * const sv, SV * const obj,
const int how,
const MGVTBL * const vtbl,
const char * const name,
const I32 namlen)
"sv_mortalcopy"
"sv_mortalcopy_flags"
both of these create a new SV that's a copy of the original SV (they do it using sv_setsv), and the new SV is marked as mortal. meaning it gets destroyed "soon" — either when something explicitly calls FREETMPS, or implicitly at places like statement boundaries 💀 so like, don't get attached.
also peep sv_newmortal and sv_2mortal if you're in that era of your life.
the two forms are identical except sv_mortalcopy_flags has an extra flags parameter, and the contents of that get passed along to sv_setsv_flags. that's it ✨
SV * sv_mortalcopy ( SV * const oldsv)
SV * Perl_sv_mortalcopy (pTHX_ SV * const oldsv)
SV * sv_mortalcopy_flags( SV * const oldsv, U32 flags)
SV * Perl_sv_mortalcopy_flags(pTHX_ SV * const oldsv, U32 flags)
"sv_newmortal"
creates a new null SV that is mortal. its reference count gets set to 1. and yeah, same deal as above — it will be destroyed "soon", either by an explicit FREETMPS call or an implicit one at places like statement boundaries 🫶
see also sv_mortalcopy and sv_2mortal, they're all in the same friend group.
SV * sv_newmortal()
SV * Perl_sv_newmortal(pTHX)
"sv_nolocking"
🚨 DEPRECATED! it is planned to remove sv_nolocking from a future release of Perl. do not use it for new code, and remove it from existing code. fr fr, this one's on the way out.
it's a dummy routine that "locks" an SV when there's no locking module present. why does it exist at all? to avoid having to test for a NULL function pointer, and because it could potentially warn under some level of strict-ness 🙈
"superseded" by sv_nosharing().
void sv_nolocking( SV *sv)
void Perl_sv_nolocking(pTHX_ SV *sv)
"sv_nounlocking"
🚨 DEPRECATED! it is planned to remove sv_nounlocking from a future release of Perl. do not use it for new code, and remove it from existing code. same energy as above, bestie.
dummy routine that "unlocks" an SV when there's no locking module present. exists to dodge a NULL function pointer test, and because it could potentially warn under some level of strict-ness 💀
"superseded" by sv_nosharing().
void sv_nounlocking( SV *sv)
void Perl_sv_nounlocking(pTHX_ SV *sv)
"sv_numeq"
"sv_numeq_flags"
these each return a boolean telling you whether the numbers in the two SV arguments are identical 🔢 the arguments will get coerced to numbers if they need to be. and a NULL SV gets treated as undef. okay bestie, noted.
in sv_numeq_flags, if flags has the SV_GMAGIC bit set, then 'get' magic is handled 🪄
and unless flags has the SV_SKIP_OVERLOAD bit set, it'll make an attempt to use == overloading. if that overloading doesn't exist, or if the flag is set, then regular numerical comparison gets used instead. simple as.
sv_numeq literally just calls sv_numeq_flags with only the SV_GMAGIC bit set. this function basically behaves like the Perl code $sv1 == $sv2 🌈
bool sv_numeq ( SV *sv1, SV *sv2)
bool sv_numeq_flags( SV *sv1, SV *sv2,
const U32 flags)
bool Perl_sv_numeq_flags(pTHX_ SV *sv1, SV *sv2,
const U32 flags)
"sv_pos_b2u"
"sv_pos_b2u_flags"
these each count the number of UTF-8 encoded characters in the PV of sv 🧚 important: the entire PV isn't necessarily looked at — just the first so-many bytes. the byte count comes from *offsetp in sv_pos_b2u, and from offset in sv_pos_b2u_flags.
⚠️ the caller must ensure that the PV contains at least as many bytes as the count passed in. that's on you, not on her.
sv_pos_b2u returns void, and instead updates *offsetp to the character count. sv_pos_b2u_flags returns the character count directly. ✨
sv_pos_b2u_flags is the preferred one, because offsetp is a *I32, which limits the size it can handle to 2Gb 💀 so like, if you're doing big things, use the flags version.
both of them handle type coercion.
sv_pos_b2u always handles 'get' magic. sv_pos_b2u_flags only handles 'get' magic when flags contains SV_GMAGIC.
and actually, sv_pos_b2u_flags passes flags to SvPV_flags, and flags usually should be SV_GMAGIC|SV_CONST_RETURN. meanwhile sv_pos_b2u automatically causes SV_CONST_RETURN to be passed to SvPV_flags. cute of her 🎀
both functions use and update PERL_MAGIC_utf8.
void sv_pos_b2u ( SV * const sv, I32 * const offsetp)
void Perl_sv_pos_b2u (pTHX_ SV * const sv, I32 * const offsetp)
STRLEN sv_pos_b2u_flags( SV * const sv,
const STRLEN offset,
const U32 flags)
STRLEN Perl_sv_pos_b2u_flags(pTHX_ SV * const sv,
const STRLEN offset,
const U32 flags)
void sv_pos_b2u ( SV * const sv,
I32 * const offsetp)
void Perl_sv_pos_b2u (pTHX_ SV * const sv,
I32 * const offsetp)
STRLEN sv_pos_b2u_flags( SV * const sv,
STRLEN const offset,
U32 flags)
STRLEN Perl_sv_pos_b2u_flags(pTHX_ SV * const sv,
STRLEN const offset,
U32 flags)
✨ sv_pos_u2b ✨
✨ sv_pos_u2b_flags ✨
okay bestie these two are the flip side of the ones above 💅 they figure out how many bytes the first so-many UTF-8-encoded characters in the PV of sv actually take up. Where does the character count come in? In sv_pos_u2b you pass it through *offsetp, and in sv_pos_u2b_flags you pass it as uoffset. 🎀
If you want, they'll also count how many bytes are in the next so-many UTF-8-encoded characters. You opt into that in both functions by handing them a non-NULL lenp, and setting *lenp to the character count you want. Then the functions update *lenp with the byte count. 🦋
sv_pos_u2b returns void — instead it updates *offsetp to the byte count. sv_pos_u2b_flags just returns the byte count directly. ✨
sv_pos_u2b_flags is the preferred one, because offsetp is an I32, and that lowkey caps how big a thing it can handle at 2Gb. 💀
Both of them handle type coercion. 🫶
sv_pos_u2b always handles 'get' magic. sv_pos_u2b_flags only handles 'get' magic when flags contains SV_GMAGIC. 🙈
fun fact: sv_pos_u2b_flags passes flags straight to SvPV_flags, and flags usually should be SV_GMAGIC|SV_CONST_RETURN. Meanwhile sv_pos_u2b automatically causes SV_CONST_RETURN to be passed to SvPV_flags for you. 😌
Both functions use and update PERL_MAGIC_utf8. 👑
void sv_pos_u2b ( SV * const sv,
I32 * const offsetp,
I32 * const lenp)
void Perl_sv_pos_u2b (pTHX_ SV * const sv,
I32 * const offsetp,
I32 * const lenp)
STRLEN sv_pos_u2b_flags( SV * const sv,
STRLEN uoffset,
STRLEN * const lenp,
U32 flags)
STRLEN Perl_sv_pos_u2b_flags(pTHX_ SV * const sv,
STRLEN uoffset,
STRLEN * const lenp,
U32 flags)
✨ sv_pvn_force_flags ✨
This one gets a sensible string out of the SV somehow, no matter what kinda mess it is. 🌈 If flags has the SV_GMAGIC bit set, it'll mg_get on sv if that's appropriate — otherwise nope, it won't. Both sv_pvn_force and sv_pvn_force_nomg are implemented in terms of this function. And ngl, you normally wanna reach for the various wrapper macros instead: peep SvPV_force and SvPV_force_nomg. 💖
char * sv_pvn_force_flags( SV * const sv,
STRLEN * const lp,
const U32 flags)
char * Perl_sv_pvn_force_flags(pTHX_ SV * const sv,
STRLEN * const lp,
const U32 flags)
✨ sv_ref ✨
Returns a SV describing what the SV passed in is a reference to. 🎀
dst can be an SV that gets set to the description, or NULL — and if it's NULL, you get a mortal SV back. 🦋
If ob is true and the SV is blessed, the description is the class name. Otherwise it's the type of the SV: SCALAR, ARRAY, etc. ✨
SV * sv_ref( SV *dst, const SV * const sv,
const int ob)
SV * Perl_sv_ref(pTHX_ SV *dst, const SV * const sv,
const int ob)
✨ sv_reftype ✨
Returns a string describing what the SV is a reference to. 💅
If ob is true and the SV is blessed, the string is the class name. Otherwise it's the type of the SV, SCALAR, ARRAY etc. 🌸
const char * sv_reftype( const SV * const sv,
const int ob)
const char * Perl_sv_reftype(pTHX_ const SV * const sv,
const int ob)
✨ sv_replace ✨
Makes the first argument a copy of the second, then deletes the original. 🫶 The target SV physically takes over ownership of the body of the source SV and inherits its flags — but the target keeps any magic it owns, and any magic in the source gets discarded. 💀
ngl this is a rather specialist SV copying operation. Most of the time you'll want sv_setsv or one of its many macro front-ends. 🙈
void sv_replace( SV * const sv, SV * const nsv)
void Perl_sv_replace(pTHX_ SV * const sv, SV * const nsv)
✨ sv_report_used ✨
Dumps the contents of all SVs that haven't been freed yet. It's a debugging aid, bestie. 🔍
void sv_report_used()
void Perl_sv_report_used(pTHX)
✨ sv_reset ✨
The underlying implementation for the reset Perl function. ✨ Heads up though: the perl-level function is vaguely deprecated. 😬
void sv_reset( const char *s, HV * const stash)
void Perl_sv_reset(pTHX_ const char *s, HV * const stash)
✨ sv_rvunweaken ✨
Unweaken a reference: clears the SvWEAKREF flag on this RV, removes the backreference to this RV from the array of backreferences associated with the target SV, and increments the refcount of the target. 🔥
It silently ignores undef and warns on non-weak references. 🫶
SV * sv_rvunweaken( SV * const sv)
SV * Perl_sv_rvunweaken(pTHX_ SV * const sv)
✨ sv_rvweaken ✨
Weaken a reference: sets the SvWEAKREF flag on this RV, gives the referred-to SV PERL_MAGIC_backref magic if it hasn't already got it, and pushes a back-reference to this RV onto the array of backreferences associated with that magic. 🎀 If the RV is magical, set magic will be called after the RV is cleared. It silently ignores undef and warns on already-weak references. 💅
SV * sv_rvweaken( SV * const sv)
SV * Perl_sv_rvweaken(pTHX_ SV * const sv)
✨ sv_set_bool ✨
Equivalent to sv_setsv(sv, bool_val ? &Pl_sv_yes : &PL_sv_no), but it may be made more efficient in the future. 👀 Doesn't handle set magic.
The perl equivalent is $sv = !!$expr;. ✨
Introduced in perl 5.35.11. 🦋
void sv_set_bool( SV *sv, const bool bool_val)
void Perl_sv_set_bool(pTHX_ SV *sv, const bool bool_val)
✨ sv_set_false ✨
Equivalent to sv_setsv(sv, &PL_sv_no), but it may be made more efficient in the future. 👀 Doesn't handle set magic.
The perl equivalent is $sv = !1;. 💖
Introduced in perl 5.35.11. 🦋
void sv_set_false( SV *sv)
void Perl_sv_set_false(pTHX_ SV *sv)
✨ sv_set_true ✨
Equivalent to sv_setsv(sv, &PL_sv_yes), but it may be made more efficient in the future. 👀 Doesn't handle set magic.
The perl equivalent is $sv = !0;. 💖
Introduced in perl 5.35.11. 🦋
void sv_set_true( SV *sv)
void Perl_sv_set_true(pTHX_ SV *sv)
✨ sv_set_undef ✨
Equivalent to sv_setsv(sv, &PL_sv_undef), but more efficient. 💅 Doesn't handle set magic.
The perl equivalent is $sv = undef;. But watch out bestie — note that it doesn't free any string buffer, unlike undef $sv. 😭
Introduced in perl 5.25.12. 🦋
void sv_set_undef( SV *sv)
void Perl_sv_set_undef(pTHX_ SV *sv)
✨ sv_setbool ✨
✨ sv_setbool_mg ✨
These set an SV to a true or false boolean value, upgrading first if that's necessary. 🎀
They only differ in one way: sv_setbool_mg handles 'set' magic, while sv_setbool does not. ✨
void sv_setbool (SV *sv, bool b)
void sv_setbool_mg(SV *sv, bool b)
✨ sv_setiv ✨
✨ sv_setiv_mg ✨
These copy an integer into the given SV, upgrading first if necessary. 🦋
They only differ in one way: sv_setiv_mg handles 'set' magic, while sv_setiv does not. ✨
void sv_setiv ( SV * const sv, const IV num)
void Perl_sv_setiv (pTHX_ SV * const sv, const IV num)
void sv_setiv_mg( SV * const sv, const IV i)
void Perl_sv_setiv_mg(pTHX_ SV * const sv, const IV i)
✨ sv_setnv ✨
✨ sv_setnv_mg ✨
These copy a double into the given SV, upgrading first if necessary. 🦋
They only differ in one way: sv_setnv_mg handles 'set' magic, while sv_setnv does not. ✨
void sv_setnv ( SV * const sv, const NV num)
void Perl_sv_setnv (pTHX_ SV * const sv, const NV num)
void sv_setnv_mg( SV * const sv, const NV num)
void Perl_sv_setnv_mg(pTHX_ SV * const sv, const NV num)
✨ sv_setpv ✨
✨ sv_setpv_mg ✨
✨ sv_setpvn ✨
✨ sv_setpvn_fresh ✨
✨ sv_setpvn_mg ✨
✨ sv_setpvs ✨
✨ sv_setpvs_mg ✨
okay so this whole little fam copies a string into the SV sv, making sure it's SvPOK_only. 💅
In the pvs forms, the string must be a C literal string, enclosed in double quotes. 🎀
In the pvn forms, the first byte of the string is pointed to by ptr, and len tells you the number of bytes to be copied — potentially including embedded NUL characters. 🫶
In the plain pv forms, ptr points to a NUL-terminated C string. That is, it points to the first byte of the string, and the copy proceeds up through the first encountered NUL byte. ✨
In the forms that take a ptr argument, if it's NULL, the SV will become undefined. 😭
The UTF-8 flag is not changed by these functions. 🙈
A terminating NUL byte is guaranteed in the result. ✅
The _mg forms handle 'set' magic; the other forms skip all magic. 💀
sv_setpvn_fresh is a cut-down alternative to sv_setpvn, intended ONLY to be used with a fresh sv that has been upgraded to a SVt_PV, SVt_PVIV, SVt_PVNV, or SVt_PVMG. 👑
💖 sv_setpv_bufsize 💖
Okay so this one sets your SV to be a string that's cur bytes long, with at least len bytes of room to spare. And bestie, it makes sure there's a null byte chillin' at SvEND. ✨
It hands you back a char * pointer to the SvPV buffer. 🫶
Heads up tho — you (the caller) have to fill in the firstcurbytes ofsvbefore anyone peeks at what's inside. Which means ifcuris zero, the SV is already fully formed and ready to go, literally just like any other SV holding an empty string. No cap. 💅
char * sv_setpv_bufsize( SV * const sv,
const STRLEN cur,
const STRLEN len)
char * Perl_sv_setpv_bufsize(pTHX_ SV * const sv,
const STRLEN cur,
const STRLEN len)
🌸 sv_setpv_mg* 🌸
Described under sv_setpv (scroll up, babe, it's all there 💖).
✨ sv_setpvf ✨
✨ sv_setpvf_mg ✨
✨ sv_setpvf_mg_nocontext ✨
✨ sv_setpvf_nocontext ✨
These work exactly like sv_catpvf, except instead of appending the text they copy it into the SV. Same energy, different destination. 🎀
Here's the tea on how they're different from each other:
sv_setpvf_mgandsv_setpvf_mg_nocontextperform 'set' magic.sv_setpvfandsv_setpvf_nocontextskip all magic.sv_setpvf_nocontextandsv_setpvf_mg_nocontextdo not take a thread context (aTHX) parameter, so you reach for them when the caller doesn't already have the thread context. 💫
The UTF-8 flag is not changed by these functions. Don't @ me.
void Perl_sv_setpvf (pTHX_ SV * const sv,
const char * const pat,
...)
void Perl_sv_setpvf_mg (pTHX_ SV * const sv,
const char * const pat,
...)
void sv_setpvf_mg_nocontext( SV * const sv,
const char * const pat,
...)
void Perl_sv_setpvf_mg_nocontext( SV * const sv,
const char * const pat,
...)
void sv_setpvf_nocontext ( SV * const sv,
const char * const pat,
...)
void Perl_sv_setpvf_nocontext ( SV * const sv,
const char * const pat,
...)
🦋 sv_setpvn* 🦋
🦋 sv_setpvn_fresh* 🦋
🦋 sv_setpvn_mg* 🦋
🦋 sv_setpvs* 🦋
🦋 sv_setpvs_mg* 🦋
Described under sv_setpv, bestie. Go look 👀
💜 sv_setref_iv 💜
Copies an integer into a brand-new SV, and optionally blesses that SV. The rv argument gets upgraded to an RV, and that RV gets pointed at the new SV. The classname argument tells you which package is doing the blessing — set classname to NULL if you wanna skip the blessing entirely. The new SV comes out with a reference count of 1, and the RV is what you get back. 🫶
SV * sv_setref_iv( SV * const rv,
const char * const classname,
const IV iv)
SV * Perl_sv_setref_iv(pTHX_ SV * const rv,
const char * const classname,
const IV iv)
💜 sv_setref_nv 💜
Same deal but with a double, babes. Copies a double into a new SV, optionally blessing it. rv gets upgraded to an RV, and that RV gets modified to point at the new SV. classname says which package does the blessing; set it to NULL to dodge the blessing. New SV = reference count of 1, and the RV is your return value. ✨
SV * sv_setref_nv( SV * const rv,
const char * const classname,
const NV nv)
SV * Perl_sv_setref_nv(pTHX_ SV * const rv,
const char * const classname,
const NV nv)
🔥 sv_setref_pv 🔥
Copies a pointer into a new SV, optionally blessing it. rv gets upgraded to an RV, and that RV gets modified to point at the new SV. If the pv argument is NULL, then PL_sv_undef gets placed into the SV. classname = which package blesses; set it to NULL to skip the blessing. New SV has a reference count of 1, and you get the RV back. 💅
Do not use this with other Perl types like HV, AV, SV, CV, because those objects will get corrupted by the pointer copy process. Not the vibe. 😭
Also note: sv_setref_pvn copies the string, while this one copies the pointer. iykyk.
SV * sv_setref_pv( SV * const rv,
const char * const classname,
void * const pv)
SV * Perl_sv_setref_pv(pTHX_ SV * const rv,
const char * const classname,
void * const pv)
🌈 sv_setref_pvn 🌈
Copies a string into a new SV, optionally blessing it. You gotta specify the length of the string with n. rv gets upgraded to an RV, and that RV gets modified to point at the new SV. classname says which package blesses; set it to NULL to avoid the blessing. New SV = reference count of 1, RV returned to you. 🎀
Note that sv_setref_pv copies the pointer while this one copies the string. 👑
SV * sv_setref_pvn( SV * const rv,
const char * const classname,
const char * const pv,
const STRLEN n)
SV * Perl_sv_setref_pvn(pTHX_ SV * const rv,
const char * const classname,
const char * const pv,
const STRLEN n)
⭐ sv_setref_pvs ⭐
Just like sv_setref_pvn, but it takes a literal string instead of a string/length pair. Slay. ✨
SV * sv_setref_pvs(SV *const rv, const char *const classname,
"literal string")
🌸 sv_setref_uv 🌸
Copies an unsigned integer into a new SV, optionally blessing it. rv gets upgraded to an RV, and that RV gets modified to point at the new SV. classname = blessing package; NULL to skip the blessing. New SV rocks a reference count of 1, and the RV comes back to you. 💖
SV * sv_setref_uv( SV * const rv,
const char * const classname,
const UV uv)
SV * Perl_sv_setref_uv(pTHX_ SV * const rv,
const char * const classname,
const UV uv)
🧚 sv_setrv_inc 🧚
🧚 sv_setrv_inc_mg 🧚
Works like sv_setrv_noinc but it increments the reference count of ref. 💫
sv_setrv_inc_mg will invoke 'set' magic on the SV; sv_setrv_inc will not.
void sv_setrv_inc ( SV * const sv, SV * const ref)
void Perl_sv_setrv_inc (pTHX_ SV * const sv, SV * const ref)
void sv_setrv_inc_mg( SV * const sv, SV * const ref)
void Perl_sv_setrv_inc_mg(pTHX_ SV * const sv, SV * const ref)
🦋 sv_setrv_noinc 🦋
🦋 sv_setrv_noinc_mg 🦋
Copies an SV pointer into the given SV as an SV reference, upgrading it if necessary. After this, SvRV(sv) equals ref. This does not adjust the reference count of ref. And the reference ref must not be NULL. 🚫
sv_setrv_noinc_mg will invoke 'set' magic on the SV; sv_setrv_noinc will not.
void sv_setrv_noinc ( SV * const sv, SV * const ref)
void Perl_sv_setrv_noinc (pTHX_ SV * const sv, SV * const ref)
void sv_setrv_noinc_mg( SV * const sv, SV * const ref)
void Perl_sv_setrv_noinc_mg(pTHX_ SV * const sv, SV * const ref)
👑 sv_setsv 👑
👑 sv_setsv_flags 👑
👑 sv_setsv_mg 👑
👑 sv_setsv_nomg 👑
These copy the contents of the source SV ssv into the destination SV dsv. 💞
Real talk: ssv may be destroyed if it is mortal, so do not use these functions if the source SV needs to be reused. Not a drill. 😭
Loosely speaking, they perform a copy-by-value, obliterating any previous content of the destination. Rip to the old data, honestly. 💀
They differ only in that:
sv_setsvcalls 'get' magic onssv, but skips 'set' magic ondsv.sv_setsv_mgcalls both 'get' magic onssvand 'set' magic ondsv.sv_setsv_nomgskips all magic. ✨sv_setsv_flagshas aflagsparameter you can use to specify any combination of magic handling — and you can also specifySV_NOSTEALso that the buffers of temps will not be stolen. 🎀
lowkey you probably don't wanna touchsv_setsvdirectly — bestie, use one of the lil' wrappers instead, likeSvSetSV,SvSetSV_nosteal,SvSetMagicSVandSvSetMagicSV_nosteal. they're right there. use them. 💅
sv_setsv_flags is the main character of scalar copying, fr — basically every other copy-ish function and macro is just standing on its shoulders underneath.
void sv_setsv ( SV *dsv, SV *ssv)
void Perl_sv_setsv (pTHX_ SV *dsv, SV *ssv)
void sv_setsv_flags( SV *dsv, SV *ssv,
const I32 flags)
void Perl_sv_setsv_flags(pTHX_ SV *dsv, SV *ssv,
const I32 flags)
void sv_setsv_mg ( SV * const dsv, SV * const ssv)
void Perl_sv_setsv_mg (pTHX_ SV * const dsv, SV * const ssv)
void sv_setsv_nomg ( SV *dsv, SV *ssv)
🧡 sv_setuv, sv_setuv_mg
okay so these two copy an unsigned integer into the SV you hand them, and they'll upgrade it first if it needs it. glow-up included, no extra charge ✨
the ONLY difference between them is that sv_setuv_mg handles 'set' magic, and sv_setuv does not.
void sv_setuv ( SV * const sv, const UV num)
void Perl_sv_setuv (pTHX_ SV * const sv, const UV num)
void sv_setuv_mg( SV * const sv, const UV u)
void Perl_sv_setuv_mg(pTHX_ SV * const sv, const UV u)
💖 sv_streq, sv_streq_flags
these both return a boolean that tells you whether the strings in the two SVs are identical. like, are they the same? yes or no. vibe check complete 🫶
sv_streq_flags is the more general one — it takes a flags argument that changes its behavior in two ways:
- it coerces its args to strings if it needs to, and treats a
NULLargument asundef - it correctly handles the UTF8 flag
if flags has the SV_GMAGIC bit set, 'get' magic gets handled.
if flags does not have the SV_SKIP_OVERLOAD bit set, it will attempt to use eq overloading. if that overloading doesn't exist, or if the flag is set, then it just falls back to regular string comparison instead.
sv_streq is literally just sv_streq_flags with flags set to only SV_GMAGIC. it basically behaves like the Perl code $sv1 eq $sv2. iconic tbh.
bool sv_streq ( SV *sv1, SV *sv2)
bool sv_streq_flags( SV *sv1, SV *sv2,
const U32 flags)
bool Perl_sv_streq_flags(pTHX_ SV *sv1, SV *sv2,
const U32 flags)
🦋 sv_unmagic
removes all magic of type type from an SV. bye bye magic ✨🚫
int sv_unmagic( SV * const sv, const int type)
int Perl_sv_unmagic(pTHX_ SV * const sv, const int type)
🎀 sv_unmagicext
removes all magic of type type from an SV, but only the ones that have the specific vtbl you name. selective unmagicking, we love to see it.
int sv_unmagicext( SV * const sv, const int type,
const MGVTBL *vtbl)
int Perl_sv_unmagicext(pTHX_ SV * const sv, const int type,
const MGVTBL *vtbl)
🌸 sv_unref, sv_unref_flags
these unset the RV status of the SV, and decrement the reference count of whatever the RV was pointing at. basically think of it as a reverse newSVrv. it's giving "undo" 💀
sv_unref_flags has an extra flags parameter, which can hold the SV_IMMEDIATE_UNREF bit — that forces the refcount to get decremented no matter what.
when that bit is not set (or with plain sv_unref, which is always the case there), the refcount will not be decremented immediately if the count is 1. instead, perl schedules it to be freed at a time of perl's own choosing. perl is the main character here, we're just living in its world 😌
aside from being able to force immediate action, the two forms behave identically.
also peep SvROK_off.
void sv_unref ( SV *sv)
void Perl_sv_unref (pTHX_ SV *sv)
void sv_unref_flags( SV * const ref, const U32 flags)
void Perl_sv_unref_flags(pTHX_ SV * const ref, const U32 flags)
💜 sv_upgrade
upgrades an SV to a more complex form. generally it adds a new body type to the SV, then copies over as much info as it can from the old body. it croaks if the SV is already in a more complex form than the one you asked for 😭
you usually want to use the SvUPGRADE macro wrapper instead — that one checks the type before calling sv_upgrade, so it does not croak. see also svtype.
void sv_upgrade( SV * const sv, svtype new_type)
void Perl_sv_upgrade(pTHX_ SV * const sv, svtype new_type)
🍓 sv_usepvn, sv_usepvn_flags, sv_usepvn_mg
these tell an SV to use ptr as its string value. normally SVs keep their string stored inside the SV, but these ones tell the SV to use an external string instead. outsourcing, but make it C 💅
ptr has to point to memory allocated by Newx. it must be the start of a Newx-ed block of memory — not a pointer into the middle of it (watch out for OOK and copy-on-write, bestie) — and not from some non-Newx allocator like malloc. you must also supply the string length, len.
by default this function will Renew (i.e. realloc, move) the memory that ptr points to, so:
after you hand it tosv_usepvn, you should not free that pointer or use it yourself anymore. and no pointers from "behind" that pointer either — likeptr+ 1. just let it go. 🫶
in the sv_usepvn_flags form: if flags & SV_SMAGIC is true, SvSETMAGIC gets called before returning. and if flags & SV_HAS_TRAILING_NUL is true, then ptr[len] must be NUL, and the realloc gets skipped entirely (i.e., the buffer is actually at least 1 byte longer than len, and already meets the requirements for storing in SvPVX).
sv_usepvn is just sv_usepvn_flags with flags set to 0, so 'set' magic is skipped.
sv_usepvn_mg is just sv_usepvn_flags with flags set to SV_SMAGIC, so 'set' magic gets performed.
void sv_usepvn ( SV *sv, char *ptr, STRLEN len)
void Perl_sv_usepvn (pTHX_ SV *sv, char *ptr, STRLEN len)
void sv_usepvn_flags( SV * const sv, char *ptr,
const STRLEN len,
const U32 flags)
void Perl_sv_usepvn_flags(pTHX_ SV * const sv, char *ptr,
const STRLEN len,
const U32 flags)
void sv_usepvn_mg ( SV *sv, char *ptr, STRLEN len)
void Perl_sv_usepvn_mg (pTHX_ SV *sv, char *ptr, STRLEN len)
🌈 sv_utf8_decode
if the PV of the SV is an octet sequence in Perl's extended UTF-8 and it contains a multiple-byte character, then the SvUTF8 flag gets turned on so it looks like a character. but if the PV only has single-byte characters, the SvUTF8 flag stays off.
it scans the PV for validity and returns FALSE if the PV is invalid UTF-8. 🚨
bool sv_utf8_decode( SV * const sv)
bool Perl_sv_utf8_decode(pTHX_ SV * const sv)
✨ sv_utf8_downgrade, sv_utf8_downgrade_flags, sv_utf8_downgrade_nomg
these try to convert the PV of an SV from characters to bytes. if the PV contains a character that just cannot fit in a byte, the conversion fails — and in that case FALSE gets returned if fail_ok is true; otherwise they croak 😭
heads up: these are not a general purpose Unicode-to-byte encoding interface. use the Encode extension for that. no cap.
they differ only in this:
sv_utf8_downgradeprocesses 'get' magic onsv.sv_utf8_downgrade_nomgdoes not.sv_utf8_downgrade_flagshas an extraflagsparameter where you can specifySV_GMAGICto process 'get' magic, or leave it cleared to not process 'get' magic.
bool sv_utf8_downgrade ( SV * const sv,
const bool fail_ok)
bool Perl_sv_utf8_downgrade (pTHX_ SV * const sv,
const bool fail_ok)
bool sv_utf8_downgrade_flags( SV * const sv,
const bool fail_ok,
const U32 flags)
bool Perl_sv_utf8_downgrade_flags(pTHX_ SV * const sv,
const bool fail_ok,
const U32 flags)
bool sv_utf8_downgrade_nomg ( SV * const sv,
const bool fail_ok)
👑 sv_utf8_encode
converts the PV of an SV to UTF-8, but then turns the SvUTF8 flag off so that it looks like octets again. sneaky lil reversal, we stan 🎀
void sv_utf8_encode( SV * const sv)
void Perl_sv_utf8_encode(pTHX_ SV * const sv)
💀 sv_utf8_upgrade, sv_utf8_upgrade_flags, sv_utf8_upgrade_flags_grow, sv_utf8_upgrade_nomg
these convert the PV of an SV into its UTF-8-encoded form. the SV gets forced into string form if it isn't already. and they always set the SvUTF8 flag, so future validity checks can be skipped — even if the whole string ends up being the same in UTF-8 as it was before. they return the number of bytes in the converted string. 🔥
the forms differ in just two ways:
the main difference is whether or not they perform 'get magic' on sv:
sv_utf8_upgrade_nomgskips 'get magic'sv_utf8_upgradeperforms itsv_utf8_upgrade_flagsandsv_utf8_upgrade_flags_groweither perform it (if theSV_GMAGICbit is set inflags) or don't (if that bit is cleared)
the other difference is that sv_utf8_upgrade_flags_grow has an extra parameter, extra, which lets the caller specify how much space to reserve as spare beyond what's actually needed for the conversion. you'd use this when you already know you're gonna need even more space soon, and it's more efficient to ask the system for space in a single call. other than that, this form is identical to sv_utf8_upgrade_flags.
also, same warning as above: these are not a general purpose byte-encoding-to-Unicode interface. use the Encode extension for that, bestie.
and one more thing — the SV_FORCE_UTF8_UPGRADE flag is now ignored. it's giving retired. 💅
STRLEN sv_utf8_upgrade ( SV *sv)
STRLEN Perl_sv_utf8_upgrade (pTHX_ SV *sv)
STRLEN sv_utf8_upgrade_flags ( SV * const sv,
const I32 flags)
STRLEN sv_utf8_upgrade_flags_grow( SV * const sv,
const I32 flags,
STRLEN extra)
STRLEN Perl_sv_utf8_upgrade_flags_grow(pTHX_ SV * const sv,
const I32 flags,
STRLEN extra)
STRLEN sv_utf8_upgrade_nomg ( SV *sv)
okay bestie that's the last of the upgrade crew, moving right along 💅
sv_vcatpvf*, sv_vcatpvf_mg*, sv_vcatpvfn*, sv_vcatpvfn_flags*
These ones are described over under sv_catpvf — like, go read that entry, they live there. No need to double up, we're not doing repeats in this group chat. 🫶
sv_vsetpvf and sv_vsetpvf_mg
Okay so these do the exact same thing as sv_vcatpvf except instead of appending the text, they copy it into the SV. That's it, that's the whole difference. ✨
The only thing separating the two of them:
sv_vsetpvf_mgdoes 'set' magic.sv_vsetpvfskips all magic. none. zero. not even a little. 🙈
Usually you don't call these directly — you roll up through their frontends, sv_setpvf and sv_setpvf_mg.
Heads up: the UTF-8 flag does not get changed by these functions. Do not come for me when it doesn't. 💀
void sv_vsetpvf ( SV * const sv,
const char * const pat,
va_list * const args)
void Perl_sv_vsetpvf (pTHX_ SV * const sv,
const char * const pat,
va_list * const args)
void sv_vsetpvf_mg( SV * const sv,
const char * const pat,
va_list * const args)
void Perl_sv_vsetpvf_mg(pTHX_ SV * const sv,
const char * const pat,
va_list * const args)
sv_vsetpvfn
Same energy as sv_vcatpvfn but it copies the text into the SV instead of appending it. Iconic behaviour, honestly. ✨
Again, the UTF-8 flag does not get touched by this function.
You'll normally reach it through one of its frontends, sv_vsetpvf and sv_vsetpvf_mg.
void sv_vsetpvfn( SV * const sv,
const char * const pat,
const STRLEN patlen,
va_list * const args,
SV ** const svargs,
const Size_t sv_count,
bool * const maybe_tainted)
void Perl_sv_vsetpvfn(pTHX_ SV * const sv,
const char * const pat,
const STRLEN patlen,
va_list * const args,
SV ** const svargs,
const Size_t sv_count,
bool * const maybe_tainted)
sv_2cv
Using various gambits (love that for it), try to get a CV out of an SV. And on top of that, it'll try — if it can — to set *st and *gvp to the stash and GV that go with it. 🎀
The flags sitting in lref get passed along to gv_fetchsv.
CV * sv_2cv( SV *sv, HV ** const st, GV ** const gvp,
const I32 lref)
CV * Perl_sv_2cv(pTHX_ SV *sv, HV ** const st, GV ** const gvp,
const I32 lref)
sv_2io
More gambits, more vibes. This one tries to get an IO out of an SV, and honestly it's giving ✨choose your own adventure✨:
- if it's a GV → grab the IO slot
- if we're an RV → grab the recursive result
- if we're a string → grab the IO slot of the symbol named after the PV
'Get' magic is ignored on the sv you pass in — but it will get called on SvRV(sv) if sv turns out to be an RV. so like, semi-ignored. 🙈
IO * sv_2io( SV * const sv)
IO * Perl_sv_2io(pTHX_ SV * const sv)
sv_2iv_flags
Returns the integer value of an SV, doing whatever string conversion is necessary along the way. If flags has the SV_GMAGIC bit set, it does an mg_get() first. 🔥
Normally you'd go through the SvIV(sv) and SvIVx(sv) macros instead.
IV sv_2iv_flags( SV * const sv, const I32 flags)
IV Perl_sv_2iv_flags(pTHX_ SV * const sv, const I32 flags)
sv_2mortal
Marks an existing SV as mortal. 💀✨ Which means: it's gonna be destroyed "soon" — either when someone explicitly calls FREETMPS, or implicitly at places like statement boundaries. It's not dramatic, it's just scheduled. 🕰️
SvTEMP() gets turned on, which basically means the SV's string buffer can be "stolen" if this SV gets copied. greedy little optimisation, we love her.
Also go peek at sv_newmortal and sv_mortalcopy while you're here.
SV * sv_2mortal( SV * const sv)
SV * Perl_sv_2mortal(pTHX_ SV * const sv)
sv_2nv_flags
Returns the num value of an SV, doing any string or integer conversion that's needed. If flags has the SV_GMAGIC bit set, it does an mg_get() first.
Normally used via the SvNV(sv) and SvNVx(sv) macros. 😌
NV sv_2nv_flags( SV * const sv, const I32 flags)
NV Perl_sv_2nv_flags(pTHX_ SV * const sv, const I32 flags)
sv_2pv and sv_2pv_flags
These are the implementations behind the various forms of the SvPV in perlapi macros — but bestie, the macros are the preferred interface, so use those. 👑
Both return a pointer to the string value of an SV (coercing it to a string if it has to) and set *lp to its length in bytes.
Where they differ: plain sv_2pvbyte always processes 'get' magic; and sv_2pvbyte_flags processes 'get' magic if and only if flags contains SV_GMAGIC. (yes. in the sv_2pv section. we don't ask questions, we just transcribe. 😭)
char * sv_2pv ( SV *sv, STRLEN *lp)
char * Perl_sv_2pv (pTHX_ SV *sv, STRLEN *lp)
char * sv_2pv_flags( SV * const sv, STRLEN * const lp,
const U32 flags)
char * Perl_sv_2pv_flags(pTHX_ SV * const sv, STRLEN * const lp,
const U32 flags)
sv_2pvbyte and sv_2pvbyte_flags
The implementations behind the various forms of the SvPVbyte in perlapi macros. again: macros are the preferred interface. 💅
These hand you a pointer to the byte-encoded representation of the SV, and set *lp to its length. If the SV is marked as being encoded as UTF-8, it gets downgraded, if that's even possible, into a byte string. And if it can't be downgraded? They croak. no notes. 💀
Where they differ: plain sv_2pvbyte always processes 'get' magic; sv_2pvbyte_flags processes 'get' magic if and only if flags contains SV_GMAGIC.
char * sv_2pvbyte ( SV *sv, STRLEN * const lp)
char * Perl_sv_2pvbyte (pTHX_ SV *sv, STRLEN * const lp)
char * sv_2pvbyte_flags( SV *sv, STRLEN * const lp,
const U32 flags)
char * Perl_sv_2pvbyte_flags(pTHX_ SV *sv, STRLEN * const lp,
const U32 flags)
sv_2pvutf8 and sv_2pvutf8_flags
The implementations behind the various forms of the SvPVutf8 in perlapi macros. (you already know the drill: macros are the preferred interface. 🎀)
These give you a pointer to the UTF-8-encoded representation of the SV, and set *lp to its length in bytes. Also — plot twist — they might upgrade the SV to UTF-8 as a side-effect. so, like, don't be shocked. 🦋
Where they differ: plain sv_2pvutf8 always processes 'get' magic; sv_2pvutf8_flags processes 'get' magic if and only if flags contains SV_GMAGIC.
char * sv_2pvutf8 ( SV *sv, STRLEN * const lp)
char * Perl_sv_2pvutf8 (pTHX_ SV *sv, STRLEN * const lp)
char * sv_2pvutf8_flags( SV *sv, STRLEN * const lp,
const U32 flags)
char * Perl_sv_2pvutf8_flags(pTHX_ SV *sv, STRLEN * const lp,
const U32 flags)
sv_2uv_flags
Returns the unsigned integer value of an SV, doing any necessary string conversion. If flags has the SV_GMAGIC bit set, it does an mg_get() first.
Normally used via the SvUV(sv) and SvUVx(sv) macros. 🌸
UV sv_2uv_flags( SV * const sv, const I32 flags)
UV Perl_sv_2uv_flags(pTHX_ SV * const sv, const I32 flags)
SvAMAGIC
Returns a boolean telling you whether sv has overloading (active magic) enabled or not. yes or no bestie, that's the whole function. ✅
bool SvAMAGIC(SV * sv)
SvAMAGIC_off
Tells the world that sv has overloading (active magic) disabled. 🚫
void SvAMAGIC_off(SV *sv)
void Perl_SvAMAGIC_off(SV *sv)
SvAMAGIC_on
Tells the world that sv has overloading (active magic) enabled. 🟢
void SvAMAGIC_on(SV *sv)
void Perl_SvAMAGIC_on(SV *sv)
SvBoolFlagsOK
Returns a bool saying whether the SV has the right flags set such that it's safe to call BOOL_INTERNALS_sv_isbool(), BOOL_INTERNALS_sv_isbool_true() or BOOL_INTERNALS_sv_isbool_false(). 🫶
Right now it's equivalent to SvIandPOK(sv) or SvIOK(sv) && SvPOK(sv).
Serialization might wanna unroll this check — and if it does, you are strongly recommended to add code like assert(SvBoolFlagsOK(sv)); before calling any of the BOOL_INTERNALS macros. do it. don't skip it. i'm asking nicely but also it's basically a requirement. 💅
U32 SvBoolFlagsOK(SV* sv)
SvCUR
Returns the length, in bytes, of the PV sitting inside the SV. 🎀
Careful though: this might not match Perl'slength— for that, usesv_len_utf8(sv).
Also go look at SvLEN while you're in the neighbourhood.
STRLEN SvCUR(SV* sv)
SvCUR_set
Sets the current length, in bytes, of the C string that's in the SV. see SvCUR and SvIV_set>. ✨
void SvCUR_set(SV* sv, STRLEN len)
SvEND
Returns a pointer to the spot just after the last character in the string that's in the SV — where there's usually a trailing NUL character (even though Perl scalars don't strictly require one, they're just like that). 🌈
See SvCUR. Grab the character as *(SvEND(sv)).
⚠️ Warning bestie: ifSvCURis equal toSvLEN, thenSvENDpoints to unallocated memory. do not touch. do not read. do not pass go. 💀
char* SvEND(SV* sv)
SvGAMAGIC
Returns true if the SV has get magic or overloading. 🦋
If either one is true, then the scalar is active data, and it has the potential to return a brand-new value every single time you access it. So you have to be careful: read it only once per user logical operation, and then work with that returned value. ✋
If neither is true, then the scalar's value can't change unless somebody writes to it. chill scalar, honestly. 😌
U32 SvGAMAGIC(SV* sv)
SvGETMAGIC
Invokes mg_get on an SV if it has 'get' magic. Like, for example, this is what calls FETCH on a tied variable. 🔮
And since Perl 5.37.1, this function is guaranteed to evaluate its argument exactly once. no double-dipping, no side effects sneaking up on you. periodt. 💅
void SvGETMAGIC( SV *sv)
void Perl_SvGETMAGIC(pTHX_ SV *sv)
SvGROW 💖
Bestie, this one grows the character buffer inside the SV so it's got room for the number of bytes you asked for. And yes, that means you gotta remember to save space for the extra trailing NUL character, okay? It calls sv_grow to actually do the expanding if it needs to, then hands you back a pointer to the character buffer. Your SV has to be of type >= SVt_PV. And if you're not 100% sure what type your SV even is, the alternative is to just call sv_grow yourself.
ngl the classic trap here: you might lowkey thinklenis how many bytes to add on top of the current size. It is NOT. It's the total sizesvshould be. periodt. 💅
char * SvGROW(SV* sv, STRLEN len)
SvIandPOK ✨
Returns a bool telling you whether the SV is both SvPOK() and SvIOK() at the same time. Same energy as SvIOK(sv) && SvPOK(sv) but way more efficient, slay.
U32 SvIandPOK(SV* sv)
SvIandPOK_off 🎀
Unsets the PV and IV status of an SV in one single operation. Equivalent to SvIOK_off(sv); SvPK_off(v); but more efficient — one move instead of two, iykyk.
void SvIandPOK_off(SV* sv)
SvIandPOK_on 🦋
Tells an SV that it's a string AND a number, all in one operation. Equivalent to SvIOK_on(sv); SvPOK_on(sv); but more efficient. Two vibes, one call. 👑
void SvIandPOK_on(SV* sv)
SvIOK 🔢
Returns a U32 value telling you whether the SV contains an integer.
U32 SvIOK(SV* sv)
SvIOK_notUV ✨
Returns a boolean telling you whether the SV contains a signed integer.
bool SvIOK_notUV(SV* sv)
SvIOK_off 💀
Unsets the IV status of an SV. bye bye integer flag.
void SvIOK_off(SV* sv)
SvIOK_on 🌸
Tells an SV that it is an integer. Manifesting.
void SvIOK_on(SV* sv)
SvIOK_only 💅
Tells an SV that it's an integer AND disables all the other OK bits. Integer only, main character energy, nothing else invited.
void SvIOK_only(SV* sv)
SvIOK_only_UV 🫶
Tells an SV that it's an unsigned integer and disables all other OK bits. Same exclusive vibe, unsigned edition.
void SvIOK_only_UV(SV* sv)
SvIOK_UV ⭐
Returns a boolean telling you whether the SV contains an integer that must be interpreted as unsigned. Heads up though: a non-negative integer whose value fits in the range of both an IV and a UV can be flagged as either SvUOK or SvIOK. So don't come for me if it's ambiguous, that's just how it is.
bool SvIOK_UV(SV* sv)
SvIOKp 🙈
Returns a U32 value telling you whether the SV contains an integer, checking the private setting. Use SvIOK instead, bestie, this one's the private one.
U32 SvIOKp(SV* sv)
SvIsBOOL 🌈
Returns true if the SV is one of the special boolean constants (PL_sv_yes or PL_sv_no), or if it's a regular SV whose last assignment stored a copy of one of those.
bool SvIsBOOL(SV* sv)
SvIsCOW 🐄
Returns a U32 value telling you whether the SV is Copy-On-Write — that's either shared hash key scalars, or full Copy On Write scalars if 5.9.0 is configured for COW.
U32 SvIsCOW(SV* sv)
SvIsCOW_shared_hash 🔥
Returns a boolean telling you whether the SV is a Copy-On-Write shared hash key scalar.
bool SvIsCOW_shared_hash(SV* sv)
SvIV, SvIV_nomg, SvIVx 🔢💖
Each of these coerces the given SV to IV and returns it. The returned value will, in many circumstances, get stored in sv's IV slot — but not in all cases, okay? (If you wanna make sure it does, use sv_setiv.)
As of 5.37.1, all of them are guaranteed to evaluate sv only once.
SvIVx is now identical to SvIV, but prior to 5.37.1 it was the only form guaranteed to evaluate sv only once. History lesson, you're welcome. 🎀
SvIV_nomg is the same as SvIV, but it does not perform 'get' magic.
IV SvIV ( SV *sv)
IV Perl_SvIV (pTHX_ SV *sv)
IV SvIV_nomg( SV *sv)
IV Perl_SvIV_nomg(pTHX_ SV *sv)
IV SvIVx ( SV *sv)
SvIV_set ✍️
Set the value of the IV pointer in sv to val. You could do the same thing with an lvalue assignment to SvIVX, but listen: with future Perls it's gonna be more efficient to use SvIV_set instead of the lvalue assignment to SvIVX. Future-proof your code, bestie.
void SvIV_set(SV* sv, IV val)
SvIVX 💅
Returns the raw value in the SV's IV slot, no checks, no conversions, no questions asked. Only use this when you're sure SvIOK is true. See also SvIV.
IV SvIVX(SV* sv)
SvIVx* ⭐
Described under SvIV. Go look, it's up there. 👆
SvLEN 📏
Returns the size of the string buffer in the SV, not counting any part attributable to SvOOK. See SvCUR.
STRLEN SvLEN(SV* sv)
SvLEN_set ✨
Set the size of the string buffer for the SV. See SvLEN.
void SvLEN_set(SV* sv, STRLEN len)
SvLOCK 🔒
Arranges for a mutual exclusion lock to be obtained on sv if a suitable module has been loaded. Locked in, literally.
void SvLOCK(SV* sv)
SvMAGIC_set 🪄
Set the value of the MAGIC pointer in sv to val. See SvIV_set.
void SvMAGIC_set(SV* sv, MAGIC* val)
SvNIOK 🍓
Returns a U32 value telling you whether the SV contains a number — integer or double, either one counts.
U32 SvNIOK(SV* sv)
SvNIOK_off 💀
Unsets the NV/IV status of an SV. Both flags, gone.
void SvNIOK_off(SV* sv)
SvNIOKp 🙈
Returns a U32 value telling you whether the SV contains a number, integer or double — but it checks the private setting. Use SvNIOK instead.
U32 SvNIOKp(SV* sv)
SvNOK 🌟
Returns a U32 value telling you whether the SV contains a double.
U32 SvNOK(SV* sv)
SvNOK_off 💀
Unsets the NV status of an SV.
void SvNOK_off(SV* sv)
SvNOK_on 🎀
Tells an SV that it is a double.
void SvNOK_on(SV* sv)
SvNOK_only 💅
Tells an SV that it is a double and disables all other OK bits. Exclusive. Only doubles allowed in this house.
void SvNOK_only(SV* sv)
SvNOKp 🙈
Returns a U32 value telling you whether the SV contains a double, checking the private setting. Use SvNOK instead.
U32 SvNOKp(SV* sv)
SvNV, SvNV_nomg, SvNVx 🌸💫
Each of these coerces the given SV to NV and returns it. The returned value in many circumstances will get stored in sv's NV slot, but not in all cases. (Use sv_setnv to make sure it does.)
As of 5.37.1, all of them are guaranteed to evaluate sv only once.
SvNVx is now identical to SvNV, but prior to 5.37.1, it was the only form guaranteed to evaluate sv only once. Glow-up arc complete. ✨
SvNV_nomg is the same as SvNV, but does not perform 'get' magic.
NV SvNV ( SV *sv)
NV Perl_SvNV (pTHX_ SV *sv)
NV SvNV_nomg( SV *sv)
NV Perl_SvNV_nomg(pTHX_ SV *sv)
NV SvNVx ( SV *sv)
SvNV_set ✍️
Set the value of the NV pointer in sv to val. See SvIV_set.
void SvNV_set(SV* sv, NV val)
SvNVX 💅
Returns the raw value in the SV's NV slot, no checks, no conversions. Only use when you're sure SvNOK is true. See also SvNV.
NV SvNVX(SV* sv)
SvNVx* ⭐
Described under SvNV. Scroll up, babe. 👆
SvOK ✅
Returns a U32 value telling you whether the value is defined. Heads up: this is only meaningful for scalars.
U32 SvOK(SV* sv)
SvOOK 🧚
Returns a U32 indicating whether the pointer to the string buffer is offset. This is a little internal hack used to speed up removal of characters from the beginning of a SvPV. When SvOOK is true, the start of the allocated string buffer is actually SvOOK_offset() bytes before SvPVX. And fun fact: this offset used to be stored in SvIVX, but now it lives within the spare part of the buffer. evolution. 🦋
U32 SvOOK(SV* sv)
SvOOK_off ✂️
Remove any string offset.
void SvOOK_off(SV * sv)
SvOOK_offset 🔍
Reads into len the offset from SvPVX back to the true start of the allocated buffer — which will be non-zero if sv_chop has been used to efficiently remove characters from the start of the buffer. Implemented as a macro, which takes the address of len, and len must be of type STRLEN. Careful though: it evaluates sv more than once. And it sets len to 0 if SvOOK(sv) is false.
void SvOOK_offset(SV*sv, STRLEN len)
SvPOK 📝
Returns a U32 value telling you whether the SV contains a character string.
U32 SvPOK(SV* sv)
SvPOK_off 💀
Unsets the PV status of an SV.
void SvPOK_off(SV* sv)
SvPOK_on 🌸
Tells an SV that it is a string.
void SvPOK_on(SV* sv)
SvPOK_only 💅
Tells an SV that it is a string and disables all other OK bits. It will also turn off the UTF-8 status. Full reset, no leftovers.
void SvPOK_only(SV* sv)
SvPOK_only_UTF8 🎀
Tells an SV that it is a string and disables all other OK bits — but it leaves the UTF-8 status exactly as it was. Same but different, iykyk.
void SvPOK_only_UTF8(SV* sv)
SvPOKp 🙈
Returns a U32 value telling you whether the SV contains a character string, checking the private setting. Use SvPOK instead.
U32 SvPOKp(SV* sv)
SvPV and its 29 besties 👑💖
Okay bestie, deep breath, because there's a whole squad here:
SvPV, SvPV_const, SvPV_flags, SvPV_flags_const, SvPV_flags_mutable, SvPV_mutable, SvPV_nolen, SvPV_nolen_const, SvPV_nomg, SvPV_nomg_const, SvPV_nomg_const_nolen, SvPV_nomg_nolen, SvPVbyte, SvPVbyte_nolen, SvPVbyte_nomg, SvPVbyte_or_null, SvPVbyte_or_null_nomg, SvPVbytex, SvPVbytex_nolen, SvPVutf8, SvPVutf8_nolen, SvPVutf8_nomg, SvPVutf8_or_null, SvPVutf8_or_null_nomg, SvPVutf8x, SvPVx, SvPVx_const, SvPVx_nolen, SvPVx_nolen_const.
Each one returns a pointer to the string in sv, or to a stringified form of sv if it doesn't contain a string. And the SV may cache that stringified version, which makes it become SvPOK. ✨
This is, like, the most basic and common operation ever, which is exactly why there are SO many slightly different versions of it. It's giving "everyone wanted a piece of this one." 💅
Now the important warning, no cap: there is no guarantee that the return value ofSvPV(sv), for example, is equal toSvPVX(sv). There's also no guarantee thatSvPVX(sv)contains valid data, or that successive calls toSvPV(sv)(or another one of these forms) will return the same pointer value each time. This is all because of how overloading and Copy-On-Write get handled. In those cases, the return value may point to a temporary buffer or similar. So if you absolutely need theSvPVXfield to be valid (like, say, if you intend to write to it), then go seeSvPV_force.
The differences between the forms are:
Okay so bestie, real talk about these SvPV forms — this is where it gets spicy 🌶️
The forms with neither "byte" nor "utf8" in their names (like "SvPV" or "SvPV_nolen") can literally expose the SV's internal string buffer. And if that buffer is entirely bytes 0-255 and has any bytes above 127 in it, then you MUST go check "SvUTF8" to figure out the actual code points the string is supposed to contain. Ngl, generally speaking it's probably safer to just prefer "SvPVbyte", "SvPVutf8", and the like. Peep "How do I pass a Perl string to a C library?" in perlguts for the tea. 🫖
The forms with "flags" in their names let you use that "flags" parameter to say "yes process 'get' magic" (by setting the "SV_GMAGIC" flag) or "no skip it" (by clearing it). The other forms process 'get' magic, except for the ones with "nomg" in their names, which skip 'get' magic. 🙅♀️
The forms that take a "len" parameter will set that variable to the byte length of the resultant string (these are macros, so don't use &len).
The forms with "nolen" in their names are telling you they don't have a "len" parameter. Only use them when you know the PV is a C string, terminated by a NUL byte, with no intermediate NUL characters — or when you genuinely don't care about its length. 💁♀️
The forms with "const" in their names return "const char *" so the compiler will hopefully yell at you if you try to modify the contents of the string (unless you cast away const yourself, you little rebel).
The other forms return a mutable pointer so the string is modifiable by the caller; this is emphasized for the ones with "mutable" in their names.
As of 5.38, all forms are guaranteed to evaluate "sv" exactly once. For earlier Perls, use a form whose name ends with "x" for single evaluation. ⭐
"SvPVutf8" is like "SvPV", but converts "sv" to UTF-8 first if it's not already UTF-8. Similarly, the other forms with "utf8" in their names correspond to their respective forms without. 🌈
"SvPVutf8_or_null" and "SvPVutf8_or_null_nomg" don't have corresponding non-"utf8" forms. Instead they're like "SvPVutf8_nomg", but when "sv" is undef, they return "NULL". 🫥
"SvPVbyte" is like "SvPV", but converts "sv" to byte representation first if it's currently encoded as UTF-8. If "sv" cannot be downgraded from UTF-8, it croaks. 💀 Similarly, the other forms with "byte" in their names correspond to their respective forms without.
"SvPVbyte_or_null" doesn't have a corresponding non-"byte" form. Instead it's like "SvPVbyte", but when "sv" is undef, it returns "NULL".
char* SvPV (SV* sv, STRLEN len)
const char* SvPV_const (SV* sv, STRLEN len)
char* SvPV_flags (SV* sv, STRLEN len, U32 flags)
const char* SvPV_flags_const (SV* sv, STRLEN len, U32 flags)
char* SvPV_flags_mutable (SV* sv, STRLEN len, U32 flags)
char* SvPV_mutable (SV* sv, STRLEN len)
char* SvPV_nolen (SV* sv)
const char* SvPV_nolen_const (SV* sv)
char* SvPV_nomg (SV* sv, STRLEN len)
const char* SvPV_nomg_const (SV* sv, STRLEN len)
const char* SvPV_nomg_const_nolen(SV* sv)
char* SvPV_nomg_nolen (SV* sv)
char* SvPVbyte (SV* sv, STRLEN len)
char* SvPVbyte_nolen (SV* sv)
char* SvPVbyte_nomg (SV* sv, STRLEN len)
char* SvPVbyte_or_null (SV* sv, STRLEN len)
char* SvPVbyte_or_null_nomg(SV* sv, STRLEN len)
char* SvPVbytex (SV* sv, STRLEN len)
char* SvPVbytex_nolen (SV* sv)
char* SvPVutf8 (SV* sv, STRLEN len)
char* SvPVutf8_nolen (SV* sv)
char* SvPVutf8_nomg (SV* sv, STRLEN len)
char* SvPVutf8_or_null (SV* sv, STRLEN len)
char* SvPVutf8_or_null_nomg(SV* sv, STRLEN len)
char* SvPVutf8x (SV* sv, STRLEN len)
char* SvPVx (SV* sv, STRLEN len)
const char* SvPVx_const (SV* sv, STRLEN len)
char* SvPVx_nolen (SV* sv)
const char* SvPVx_nolen_const (SV* sv)
"SvPV_force", "SvPV_force_flags", "SvPV_force_flags_mutable", "SvPV_force_flags_nolen", "SvPV_force_mutable", "SvPV_force_nolen", "SvPV_force_nomg", "SvPV_force_nomg_nolen", "SvPVbyte_force", "SvPVbytex_force", "SvPVutf8_force", "SvPVutf8x_force", "SvPVx_force"
These are like "SvPV", returning the string in the SV, but they will force the SV into containing a string ("SvPOK"), and only a string ("SvPOK_only"), by hook or by crook. 🪄 You need one of these "force" routines if you're gonna update the "SvPVX" directly.
⚠️ Note that coercing an arbitrary scalar into a plain PV will potentially strip useful data from it. For example, if the SV was"SvROK", then the referent will have its reference count decremented, and the SV itself may be converted to an"SvPOK"scalar with a string buffer containing a value such as"ARRAY(0x1234)". 😭
The differences between the forms are:
- The forms with
"flags"in their names let you use the"flags"parameter to say "yes perform 'get' magic" (by setting the"SV_GMAGIC"flag) or "no skip it" (by clearing it). The other forms do perform 'get' magic, except for the ones with"nomg"in their names, which skip 'get' magic. - The forms that take a
"len"parameter will set that variable to the byte length of the resultant string (these are macros, so don't use&len). - The forms with
"nolen"in their names are telling you they don't have a"len"parameter. Only use them when it's known the PV is a C string, terminated by aNULbyte, and without intermediateNULcharacters — or when you don't care about its length. - The forms with
"mutable"in their names are effectively the same as those without, but the name emphasizes that the string is modifiable by the caller, which it is in all the forms.
"SvPVutf8_force" is like "SvPV_force", but converts "sv" to UTF-8 first if it's not already UTF-8. 🦋
"SvPVutf8x_force" is like "SvPVutf8_force", but guarantees to evaluate "sv" only once; use the more efficient "SvPVutf8_force" otherwise.
"SvPVbyte_force" is like "SvPV_force", but converts "sv" to byte representation first if it's currently encoded as UTF-8. If the SV cannot be downgraded from UTF-8, this croaks. 💀
"SvPVbytex_force" is like "SvPVbyte_force", but guarantees to evaluate "sv" only once; use the more efficient "SvPVbyte_force" otherwise.
char* SvPV_force (SV* sv, STRLEN len)
char* SvPV_force_flags (SV * sv, STRLEN len, U32 flags)
char* SvPV_force_flags_mutable(SV * sv, STRLEN len, U32 flags)
char* SvPV_force_flags_nolen (SV * sv, U32 flags)
char* SvPV_force_mutable (SV * sv, STRLEN len)
char* SvPV_force_nolen (SV* sv)
char* SvPV_force_nomg (SV* sv, STRLEN len)
char* SvPV_force_nomg_nolen (SV * sv)
char* SvPVbyte_force (SV * sv, STRLEN len)
char* SvPVbytex_force (SV * sv, STRLEN len)
char* SvPVutf8_force (SV * sv, STRLEN len)
char* SvPVutf8x_force (SV * sv, STRLEN len)
char* SvPVx_force (SV* sv, STRLEN len)
"SvPV_free"
Frees the PV buffer in "sv", leaving things in a precarious state, so it should only be used as part of a larger operation. 🙈
void SvPV_free(SV * sv)
"SvPV_mutable*", "SvPV_nolen*", "SvPV_nolen_const*", "SvPV_nomg*", "SvPV_nomg_const*", "SvPV_nomg_const_nolen*", "SvPV_nomg_nolen*"
Described under "SvPV" ⬆️
"SvPV_renew"
Low level micro optimization of "SvGROW". Ngl it's generally better to use "SvGROW" instead. This is because "SvPV_renew" ignores potential issues that "SvGROW" handles. "sv" needs to have a real "PV" that is unencumbered by things like COW. Using "SV_CHECK_THINKFIRST" or "SV_CHECK_THINKFIRST_COW_DROP" before calling this should clean it up, but why not just use "SvGROW" if you're not sure about the provenance? 🤔
void SvPV_renew(SV* sv, STRLEN len)
"SvPV_set"
This is probably not what you want to use — you probably wanted "sv_usepvn_flags" or "sv_setpvn" or "sv_setpvs". 💅
Set the value of the PV pointer in "sv" to the Perl allocated "NUL"-terminated string "val". See also "SvIV_set".
Remember to free the previous PV buffer. There are many things to check. Beware that the existing pointer may be involved in copy-on-write or other mischief, so do SvOOK_off(sv) and use "sv_force_normal" or "SvPV_force" (or check the "SvIsCOW" flag) first to make sure this modification is safe. Then finally, if it is not a COW, call "SvPV_free" to free the previous PV buffer. 🧹
void SvPV_set(SV* sv, char* val)
"SvPV_shrink_to_cur"
Trim any trailing unused memory in the PV of "sv", which needs to have a real "PV" that is unencumbered by things like COW. Think first before using this functionality. Is the space saving really worth giving up COW? Will the needed size of "sv" stay the same? 🤨
If the answers are both yes, then use "SV_CHECK_THINKFIRST" or "SV_CHECK_THINKFIRST_COW_DROP" before calling this.
void SvPV_shrink_to_cur(SV* sv)
"SvPVbyte*"
Described under "SvPV" ⬆️
"SvPVbyte_force*"
Described under "SvPV_force" ⬆️
"SvPVbyte_nolen*", "SvPVbyte_nomg*", "SvPVbyte_or_null*", "SvPVbyte_or_null_nomg*", "SvPVbytex*"
Described under "SvPV" ⬆️
"SvPVbytex_force*"
Described under "SvPV_force" ⬆️
"SvPVbytex_nolen*"
Described under "SvPV" ⬆️
"SvPVCLEAR"
Ensures that sv is an SVt_PV and that its SvCUR is 0, and that it is properly null terminated. Equivalent to sv_setpvs(""), but more efficient. ✨
char * SvPVCLEAR(SV* sv)
"SvPVCLEAR_FRESH"
Like SvPVCLEAR, but optimized for newly-minted SVt_PV/PVIV/PVNV/PVMG that already have a PV buffer allocated, but no SvTHINKFIRST. 🎀
char * SvPVCLEAR_FRESH(SV* sv)
"SvPVutf8*"
Described under "SvPV" ⬆️
"SvPVutf8_force*"
Described under "SvPV_force" ⬆️
"SvPVutf8_nolen*", "SvPVutf8_nomg*", "SvPVutf8_or_null*", "SvPVutf8_or_null_nomg*", "SvPVutf8x*"
Described under "SvPV" ⬆️
"SvPVutf8x_force*"
Described under "SvPV_force" ⬆️
💖 SvPVX, SvPVX_const, SvPVX_mutable, SvPVXx 💖
Okay bestie, these four hand you back a pointer to the actual physical string living inside the SV. The SV has got to contain a string — no exceptions. And lowkey important: before 5.9.3 it is not safe to run these unless the SV's type is >= "SVt_PV". So like, respect the version history.
They're also the move for stashing the name of an autoloaded subroutine in an XS AUTOLOAD routine — go read "Autoloading with XSUBs" in perlguts for the tea. 🫖
"SvPVXx"is literally identical to"SvPVX"."SvPVX_mutable"is just a synonym for"SvPVX", but the name is doing PR work — it emphasizes that the caller is allowed to modify that string. 💅"SvPVX_const"is the one with boundaries: the return value gets cast so your compiler will yell at you if you try to modify the contents of the string (unless you cast away const yourself, you menace).
char* SvPVX (SV* sv)
const char* SvPVX_const (SV* sv)
char* SvPVX_mutable(SV* sv)
char* SvPVXx (SV* sv)
✨ SvPVx*, SvPVx_const* ✨
Described under "SvPV". Go look there, babe.
🦋 SvPVx_force* 🦋
Described under "SvPV_force".
🌸 SvPVx_nolen*, SvPVx_nolen_const* 🌸
Described under "SvPV".
👑 SvPVXtrue 👑
Returns a boolean telling you whether "sv" holds a PV that counts as TRUE. You get FALSE back if "sv" doesn't contain a PV at all, or if the PV it does contain is zero length, or if it's just the single character '0'. Every other PV value? Considered TRUE. No cap.
As of Perl v5.37.1, "sv" is evaluated exactly once — in earlier releases it could get evaluated more than once, so if you're on old perl, watch your side effects. 😭
bool SvPVXtrue( SV *sv)
bool Perl_SvPVXtrue(pTHX_ SV *sv)
🎀 SvPVXx* 🎀
Described under "SvPVX".
💜 SvREADONLY 💜
Returns true if the argument is readonly, false otherwise. It's exposed to perl code via Internals::SvREADONLY().
U32 SvREADONLY(SV* sv)
💜 SvREADONLY_off 💜
Marks an object as not-readonly. Exactly what that means depends on the object type — it's giving "it depends". Exposed to perl code via Internals::SvREADONLY().
U32 SvREADONLY_off(SV* sv)
💜 SvREADONLY_on 💜
Marks an object as readonly. Exactly what that means depends on the object type. Exposed to perl code via Internals::SvREADONLY().
U32 SvREADONLY_on(SV* sv)
⭐ SvREFCNT ⭐
Returns the value of the object's reference count. Exposed to perl code via Internals::SvREFCNT().
U32 SvREFCNT(SV* sv)
💅 SvREFCNT_dec, SvREFCNT_dec_set_NULL, SvREFCNT_dec_ret_NULL, SvREFCNT_dec_NN 💅
These decrement the reference count of the given SV. It's giving "bye bestie, one less friend". 👋
"SvREFCNT_dec_NN"— only usable when"sv"is known to not be"NULL". The NN stands for "no null", iykyk.SvREFCNT_dec_ret_NULL()is identical toSvREFCNT_dec()except it returns a NULL"SV *". It's the engine behindSvREFCNT_dec_set_NULL(), which is a macro that — when you hand it a non-NULL argument — decrements that argument's reference count and then sets it toNULL.
So you can take code shaped like this:
if (sv) {
SvREFCNT_dec_NN(sv);
sv = NULL;
}
and glow it up into this:
SvREFCNT_dec_set_NULL(sv);
Iconic. ✨
void SvREFCNT_dec ( SV *sv)
void Perl_SvREFCNT_dec (pTHX_ SV *sv)
void SvREFCNT_dec_set_NULL( SV *sv)
SV * SvREFCNT_dec_ret_NULL( SV *sv)
SV * Perl_SvREFCNT_dec_ret_NULL(pTHX_ SV *sv)
void SvREFCNT_dec_NN ( SV *sv)
void Perl_SvREFCNT_dec_NN (pTHX_ SV *sv)
🔥 SvREFCNT_inc, SvREFCNT_inc_NN, SvREFCNT_inc_simple, SvREFCNT_inc_simple_NN, SvREFCNT_inc_simple_void, SvREFCNT_inc_simple_void_NN, SvREFCNT_inc_void, SvREFCNT_inc_void_NN 🔥
All of these bump up the reference count of the given SV. The ones without void in their names return the SV — the void ones just... don't. 🙈
"SvREFCNT_inc" is the base operation; literally all the rest are just optimizations for when you already know various input constraints are true. So every single one of them can be replaced with "SvREFCNT_inc". They're the glow-up versions.
"SvREFCNT_inc_NN"— only if you know"sv"is not"NULL". Since there's no NULLness check to do, it's faster and smaller. Slay."SvREFCNT_inc_void"— only if you don't need the return value. The macro doesn't have to return anything meaningful."SvREFCNT_inc_void_NN"— only if you both don't need the return value and you know"sv"isn't"NULL". No meaningful return, no NULL check → smaller and faster."SvREFCNT_inc_simple"— only with expressions that have no side effects. Since there's no temporary value to store, it's faster."SvREFCNT_inc_simple_NN"— only with side-effect-free expressions and when you know"sv"isn't"NULL". No temp to store, no NULL to check → faster and smaller."SvREFCNT_inc_simple_void"— only with side-effect-free expressions, and you don't need the return value."SvREFCNT_inc_simple_void_NN"— only with side-effect-free expressions, you don't need the return value, and you know"sv"isn't"NULL".
SV * SvREFCNT_inc (SV *sv)
SV * Perl_SvREFCNT_inc (SV *sv)
SV * SvREFCNT_inc_NN (SV *sv)
SV * Perl_SvREFCNT_inc_NN (SV *sv)
SV * SvREFCNT_inc_simple (SV *sv)
SV * SvREFCNT_inc_simple_NN (SV *sv)
void SvREFCNT_inc_simple_void (SV *sv)
void SvREFCNT_inc_simple_void_NN(SV *sv)
void SvREFCNT_inc_void (SV *sv)
void Perl_SvREFCNT_inc_void (SV *sv)
void SvREFCNT_inc_void_NN (SV *sv)
🧚 SvROK 🧚
Tests if the SV is an RV. That's the whole vibe check.
U32 SvROK(SV* sv)
🧚 SvROK_off 🧚
Unsets the RV status of an SV. Un-RV-ing it, bestie.
void SvROK_off(SV* sv)
🧚 SvROK_on 🧚
Tells an SV that it is an RV.
void SvROK_on(SV* sv)
🌈 SvRV 🌈
Dereferences an RV and hands you back the SV. The whole point.
SV* SvRV(SV* sv)
🌈 SvRV_set 🌈
Sets the value of the RV pointer in "sv" to val. Check "SvIV_set" for the deets.
void SvRV_set(SV* sv, SV* val)
💀 SvSETMAGIC 💀
Invokes "mg_set" on an SV if it's got 'set' magic. You need this after modifying a scalar, in case it's a magical variable like $| or a tied variable (it calls "STORE", fr fr). Heads up though: this macro evaluates its argument more than once, so don't hand it anything cursed.
void SvSETMAGIC(SV* sv)
🍓 SvSetMagicSV, SvSetMagicSV_nosteal, SvSetSV, SvSetSV_nosteal 🍓
If "dsv" is the same as "ssv", these do absolutely nothing. Otherwise they all call some form of "sv_setsv". And — warning — they may evaluate their arguments more than once. 👀
The only differences are:
"SvSetMagicSV"and"SvSetMagicSV_nosteal"perform any required 'set' magic afterwards on the destination SV;"SvSetSV"and"SvSetSV_nosteal"do not."SvSetSV_nosteal"and"SvSetMagicSV_nosteal"call a non-destructive version of"sv_setsv".
void SvSetMagicSV (SV* dsv, SV* ssv)
void SvSetMagicSV_nosteal(SV* dsv, SV* ssv)
void SvSetSV (SV* dsv, SV* ssv)
void SvSetSV_nosteal (SV* dsv, SV* ssv)
🦋 SvSHARE 🦋
Arranges for "sv" to be shared between threads — but only if a suitable module has been loaded. Can't share without the right support, bestie.
void SvSHARE(SV* sv)
🦋 SvSHARED_HASH 🦋
Returns the hash for "sv" that was created by "newSVpvn_share".
struct hek* SvSHARED_HASH(SV * sv)
👑 SvSTASH 👑
Returns the stash of the SV. The stash is the vibe. 💅
HV* SvSTASH(SV* sv)
👑 SvSTASH_set 👑
Sets the value of the STASH pointer in "sv" to val. Check "SvIV_set".
void SvSTASH_set(SV* sv, HV* val)
⭐ SvTRUE, SvTRUE_NN, SvTRUE_nomg, SvTRUE_nomg_NN, SvTRUEx ⭐
These return a boolean saying whether Perl would evaluate the SV as true or false. For a defined/undefined test, go see "SvOK" instead — different question entirely.
As of Perl 5.32, all of these are guaranteed to evaluate "sv" only once. Before that release, only "SvTRUEx" guaranteed single evaluation — and now "SvTRUEx" is identical to "SvTRUE". Glow up complete. ✨
"SvTRUE_nomg" and "TRUE_nomg_NN" do not perform 'get' magic; the others do, unless the scalar is already "SvPOK", "SvIOK", or "SvNOK" (that's the public flags, not the private ones, careful).
"SvTRUE_NN"is like"SvTRUE", except"sv"is assumed to be non-null (NN). If there's any chance it's NULL, use plain"SvTRUE"."SvTRUE_nomg_NN"is like"SvTRUE_nomg", except"sv"is assumed to be non-null (NN). If there's any chance it's NULL, use plain"SvTRUE_nomg".
bool SvTRUE ( SV *sv)
bool Perl_SvTRUE (pTHX_ SV *sv)
bool SvTRUE_NN ( SV *sv)
bool Perl_SvTRUE_NN (pTHX_ SV *sv)
bool SvTRUE_nomg ( SV *sv)
bool Perl_SvTRUE_nomg (pTHX_ SV *sv)
bool SvTRUE_nomg_NN( SV *sv)
bool SvTRUEx ( SV *sv)
🎀 SvTYPE 🎀
Returns the type of the SV. See "svtype" for what the types even are.
svtype SvTYPE(SV* sv)
🎀 SvUNLOCK 🎀
Releases a mutual exclusion lock on "sv" — if a suitable module has been loaded. Same energy as SvSHARE, opposite direction.
void SvUNLOCK(SV* sv)
🌸 SvUOK 🌸
Returns a boolean indicating whether the SV contains an integer that must be interpreted as unsigned. And here's the little nuance: a non-negative integer whose value fits within the range of both an IV and a UV may be flagged as either "SvUOK" or "SvIOK". It's giving "both, actually". 😭
bool SvUOK(SV* sv)
🌸 SvUPGRADE 🌸
Used to upgrade an SV to a more complex form. It uses "sv_upgrade" to do the upgrade if necessary. See "svtype".
void SvUPGRADE(SV* sv, svtype type)
💜 SvUTF8 💜
Returns a U32 value indicating the UTF-8 status of an SV. If everything's set up properly, this tells you whether or not the SV contains UTF-8 encoded data. Pro tip bestie: call this after a call to "SvPV" or one of its variants, in case some string overloading call updated the internal flag behind your back. 🙈
If you want to take the bytes pragma into account, use "DO_UTF8" instead.
U32 SvUTF8(SV* sv)
💜 SvUTF8_off 💜
Unsets the UTF-8 status of an SV (the data is not changed, just the flag). And listen — do not use this frivolously.
void SvUTF8_off(SV *sv)
💜 SvUTF8_on 💜
Turns on the UTF-8 status of an SV (the data is not changed, just the flag). Same rule, do not use this frivolously. 🙅
void SvUTF8_on(SV *sv)
SvUV, SvUV_nomg, SvUVx
okay so these three besties each take the SV you hand them, coerce it to a UV, and hand it right back. 💅
the value they return? in many circumstances it'll get stored in sv's UV slot, but not in all cases. if you actually need it in there, use sv_setuv to make sure it does. no cap.
as of 5.37.1, all of them are guaranteed to evaluate sv only once. 🎀
SvUVx is now literally identical to SvUV — but before 5.37.1, it was the only form guaranteed to evaluate sv just once. the way it used to carry the whole team 😭
UV SvUV ( SV *sv)
UV Perl_SvUV (pTHX_ SV *sv)
UV SvUV_nomg( SV *sv)
UV Perl_SvUV_nomg(pTHX_ SV *sv)
UV SvUVx ( SV *sv)
SvUV_set
sets the value of the UV pointer inside sv to val. if you want the whole lore, go peep SvIV_set. ✨
void SvUV_set(SV* sv, UV val)
SvUVX
returns the raw value sitting in the SV's UV slot — zero checks, zero conversions, just vibes. only reach for this when you are sure SvIOK is true, bestie. also go look at SvUV. 🫶
UV SvUVX(SV* sv)
SvUVx*
all of this is described up under SvUV. go check it out 👀
SvUVXx
DEPRECATED! it's planned to be removed from a future release of Perl. do not use it for new code, and please remove it from existing code. periodt.
it's an unnecessary synonym for SvUVX. like... why does it even exist 💀
UV SvUVXx(SV* sv)
SvVOK
returns a boolean telling you whether the SV contains a v-string. TRUE if it does, FALSE if it doesn't. simple as that. ⭐
bool SvVOK(SV* sv)
SvVSTRING_mg
hands you back the vstring magic, or NULL if there isn't any. 🦋
MAGIC* SvVSTRING_mg(SV * sv)
uiv_2buf
this one converts an IV or a UV into its string representation. ✨
it's used internally by sv_2pv_flags() and do_print(), so it's very much giving backstage-pass energy. 🎟️
char * uiv_2buf(char * const buf, const IV iv, UV uv,
const int is_uv, char ** const peob)
char * Perl_uiv_2buf(char * const buf, const IV iv, UV uv,
const int is_uv, char ** const peob)
vnewSVpvf
just like newSVpvf, except the arguments come in as an encapsulated argument list. cute, right? 💅
SV * vnewSVpvf( const char * const pat,
va_list * const args)
SV * Perl_vnewSVpvf(pTHX_ const char * const pat,
va_list * const args)
🛡️ TAINTING 🛡️
okay this whole section is about taint, and you have to treat it with respect because it's literally about security. ngl, read every word here. 🔥
SvTAINT
taints an SV if tainting is enabled, and if some input to the current expression is tainted — usually a variable, but possibly also implicit inputs such as locale settings. SvTAINT propagates that taintedness to the outputs of an expression in a pessimistic fashion; i.e., without paying attention to precisely which outputs are influenced by which inputs. so like... everyone's tainted now, sorry babes. 😭
void SvTAINT(SV* sv)
SvTAINTED
checks to see if an SV is tainted. returns TRUE if it is, FALSE if it's not. vibe check complete ✅
bool SvTAINTED(SV* sv)
SvTAINTED_off
untaints an SV.
be VERY careful with this routine. it short-circuits some of Perl's fundamental security features. XS module authors should not use this function unless they fully understand all the implications of unconditionally untainting the value. untainting should be done the standard perl fashion — via a carefully crafted regexp — rather than directly untainting variables. like, do not do this unless you truly know what you're doing. periodt. 🔥
void SvTAINTED_off(SV* sv)
SvTAINTED_on
marks an SV as tainted, if tainting is enabled. ✨
void SvTAINTED_on(SV* sv)
⏰ TIME ⏰
okay bestie, this section is a whole grab bag of time-related symbols, macros, and functions. buckle up, it's giving lore drop. 🌈
ASCTIME_R_PROTO
this symbol encodes the prototype of asctime_r. it's zero if d_asctime_r is undef, and one of the REENTRANT_PROTO_T_ABC macros of reentr.h if d_asctime_r is defined. 🎀
CTIME_R_PROTO
this symbol encodes the prototype of ctime_r. it's zero if d_ctime_r is undef, and one of the REENTRANT_PROTO_T_ABC macros of reentr.h if d_ctime_r is defined.
GMTIME_MAX
this symbol contains the maximum value for the time_t offset that the system function gmtime () accepts, and it defaults to 0. 💜
GMTIME_MIN
this symbol contains the minimum value for the time_t offset that the system function gmtime () accepts, and it defaults to 0.
GMTIME_R_PROTO
this symbol encodes the prototype of gmtime_r. it's zero if d_gmtime_r is undef, and one of the REENTRANT_PROTO_T_ABC macros of reentr.h if d_gmtime_r is defined.
HAS_ASCTIME_R
this symbol, if defined, indicates that the asctime_r routine is available to asctime re-entrantly. ✨
HAS_ASCTIME64
this symbol, if defined, indicates that the asctime64 () routine is available to do the 64bit variant of asctime ().
HAS_CTIME_R
this symbol, if defined, indicates that the ctime_r routine is available to ctime re-entrantly.
HAS_CTIME64
this symbol, if defined, indicates that the ctime64 () routine is available to do the 64bit variant of ctime ().
HAS_DIFFTIME
this symbol, if defined, indicates that the difftime routine is available. 🕰️
HAS_DIFFTIME64
this symbol, if defined, indicates that the difftime64 () routine is available to do the 64bit variant of difftime ().
HAS_FUTIMES
this symbol, if defined, indicates that the futimes routine is available to change file descriptor time stamps with struct timevals. 🗂️
HAS_GETITIMER
this symbol, if defined, indicates that the getitimer routine is available to return interval timers. ⏱️
HAS_GETTIMEOFDAY
this symbol, if defined, indicates that the gettimeofday() system call is available for a sub-second accuracy clock. usually, the file sys/resource.h needs to be included (see I_SYS_RESOURCE). the type Timeval should be used to refer to "struct timeval". 🌸
HAS_GMTIME_R
this symbol, if defined, indicates that the gmtime_r routine is available to gmtime re-entrantly.
HAS_GMTIME64
this symbol, if defined, indicates that the gmtime64 () routine is available to do the 64bit variant of gmtime ().
HAS_LOCALTIME_R
this symbol, if defined, indicates that the localtime_r routine is available to localtime re-entrantly.
HAS_LOCALTIME64
this symbol, if defined, indicates that the localtime64 () routine is available to do the 64bit variant of localtime ().
HAS_MKTIME
this symbol, if defined, indicates that the mktime routine is available. 🫶
HAS_MKTIME64
this symbol, if defined, indicates that the mktime64 () routine is available to do the 64bit variant of mktime ().
HAS_NANOSLEEP
this symbol, if defined, indicates that the nanosleep system call is available to sleep with 1E-9 sec accuracy. 😴
HAS_SETITIMER
this symbol, if defined, indicates that the setitimer routine is available to set interval timers. ⏲️
HAS_STRFTIME
this symbol, if defined, indicates that the strftime routine is available to do time formatting. ✨
HAS_TIME
this symbol, if defined, indicates that the time() routine exists. ✅
HAS_TIMEGM
this symbol, if defined, indicates that the timegm routine is available to do the opposite of gmtime (). 🔄
HAS_TIMES
this symbol, if defined, indicates that the times() routine exists. note that this became obsolete on some systems (SUNOS), which now use getrusage(). it may be necessary to include sys/times.h. 💀
HAS_TM_TM_GMTOFF
this symbol, if defined, indicates to the C program that the struct tm has a tm_gmtoff field. 🌍
HAS_TM_TM_ZONE
this symbol, if defined, indicates to the C program that the struct tm has a tm_zone field.
HAS_TZNAME
this symbol, if defined, indicates that the tzname[] array is available to access timezone names. 🌐
HAS_USLEEP
this symbol, if defined, indicates that the usleep routine is available to let the process sleep on a sub-second accuracy. 😴
HAS_USLEEP_PROTO
this symbol, if defined, indicates that the system provides a prototype for the usleep() function. otherwise, it's up to the program to supply one. a good guess is
extern int usleep(useconds_t);
I_TIME
this symbol is always defined, and it indicates to the C program that it should include time.h. 🎀
#ifdef I_TIME
#include <time.h>
#endif
I_UTIME
this symbol, if defined, indicates to the C program that it should include utime.h.
#ifdef I_UTIME
#include <utime.h>
#endif
L_R_TZSET
if localtime_r() needs tzset, it is defined in this define. 🕰️
LOCALTIME_MAX
this symbol contains the maximum value for the time_t offset that the system function localtime () accepts, and it defaults to 0.
LOCALTIME_MIN
this symbol contains the minimum value for the time_t offset that the system function localtime () accepts, and it defaults to 0.
LOCALTIME_R_NEEDS_TZSET
many libc's localtime_r implementations do not call tzset, making them differ from localtime(), and making timezone changes using $ENV{TZ} without explicitly calling tzset impossible. this symbol makes us call tzset before localtime_r. 😤
LOCALTIME_R_PROTO
this symbol encodes the prototype of localtime_r. it's zero if d_localtime_r is undef, and one of the REENTRANT_PROTO_T_ABC macros of reentr.h if d_localtime_r is defined.
mini_mktime
normalises struct tm values without the localtime() semantics (and overhead) of mktime(). so it's the lowkey version, and that's a compliment. 💅
void mini_mktime(struct tm *ptm)
void Perl_mini_mktime(struct tm *ptm)
sv_strftime_ints, sv_strftime_tm, my_strftime
these implement libc strftime(), overcoming various deficiencies it has; you will come to regret sooner or later using it directly instead of these. trust. 😭
on failure, they return NULL, and set errno to EINVAL.
sv_strftime_tm and sv_strftime_ints are preferred, as they transparently handle the UTF-8ness of the current locale, the input fmt, and the returned result. only if the current LC_TIME locale is a UTF-8 one (and use bytes is not in effect) will the result be marked as UTF-8. 🌈
for these, the caller assumes ownership of the returned SV with a reference count of 1. 👑
my_strftime is kept for backwards compatibility. knowing if its result should be considered UTF-8 or not requires significant extra logic.
note that all three functions are always executed in the underlying LC_TIME locale of the program, giving results based on that locale. 🌸
okay so bestie, the stringified fmt parameter in all of these? It's literally the same as the system libc strftime. 💅 The available conversion specifications vary by platform. These days, every specification listed in the ANSI C99 standard should be usable everywhere. They are a A b B c d H I j m M p S U w W x X y Y Z %. ✨
But no cap, some conversion specifier results are non-portable. For example, a A b B c p Z change according to the locale settings of the user, and both how to set locales (the locale names) and what output to expect are not standardized. The specifier c changes according to the timezone settings of the user and the timezone computation rules of the operating system. The Z specifier is notoriously unportable since the names of timezones are not standardized. Sticking to the numeric specifiers is the safest route. 🛟
At the time of this writing, for example, %s is not available on Windows-like systems. 💀
The functions differ as follows:
- the
fmtparameter and the return frommy_strftimearechar *instead of theSV *in the other two functions. This means the UTF-8ness of the format and result are unspecified. The result MUST be arranged to be FREED BY THE CALLER). 😭 sv_strftime_intsandmy_strftimetake a bunch of integer parameters that together completely define a given time. They calculate thestruct tmto pass to libcstrftime(), and call that function. See below for the meaning of the parameters.
sv_strftime_tm takes a pointer to an already filled-in struct tm parameter, so avoids that calculation. 👑
my_strftimetakes two extra parameters that are ignored, being kept only for historical reasons. These arewdayandyday. 🫠
The C99 Standard calls for struct tm to contain at least these fields:
int tm_sec; // seconds after the minute — [0, 60]
int tm_min; // minutes after the hour — [0, 59]
int tm_hour; // hours since midnight — [0, 23]
int tm_mday; // day of the month — [1, 31]
int tm_mon; // months since January — [0, 11]
int tm_year; // years since 1900
int tm_wday; // days since Sunday — [0, 6]
int tm_yday; // days since January 1 — [0, 365]
int tm_isdst; // Daylight Saving Time flag
tm_wday and tm_yday are output only; the other fields give enough information to accurately calculate these, and are internally used for that purpose. 💖
The numbers enclosed in the square brackets above give the maximum legal ranges for values in the corresponding field. Those ranges are restricted for some inputs. For example, not all months have 31 days, but all hours have 60 minutes. If you set a number that is outside the corresponding range, perl and the libc functions will automatically normalize it to be inside the range, adjusting other values as necessary. For example, specifying February 29, is the same as saying March 1 for non-leap years; and using a minute value of 60 will instead change that to a 0, and increment the hour, which in turn, if the hour was 23, will roll it over to 0 it and increment the day, and so on. 🔄
Each parameter to sv_strftime_ints and my_strftime populates the similarly-named field in this structure.
A value of 60 is legal for tm_sec, but only for those moments when an official leap second has been declared. It is undefined behavior to use them otherwise, and the behavior does vary depending on the implementation. Some implementations take your word for it that this is a leap second, leaving it as the 61st second of the given minute; some roll it over to be the 0th second of the following minute; some treat it as 0. Some non-conforming implementations always roll it over to the next minute, regardless of whether an actual leap second is occurring or not. (And yes, it is a real problem that different computers have a different conception of what the current time is; you can search the internet for details.) 🌐
There is no limit (outside the size of int) for the value of tm_year, but sufficiently negative values (for earlier than 1900) may have different results on different systems and locales. Some libc implementations may know when a given locale adopted the Greorian calendar, and adjust for that. Others will not. (And some countries didn't adopt the Gregorian calendar until after 1900.) Probably all implementations assume modern time zones go back forever, before they were actually invented, starting in the last half of the 19th century. 🕰️
The treatment of the isdst field has varied over previous Perl versions, and has been buggy (both by perl and by some libc implementations), but is now aligned, as best we can, with the POSIX Standard, as follows:
"is_dist"is 0 — The function is to assume that daylight savings time is not in effect. This should now always work properly, as perl uses its own implementation in this case, avoiding non-conforming libc ones. 💯"is_dist"is >0 — The function is to assume that daylight savings time is in effect, though some underlying libc implementations treat this as a hint instead of a mandate. 🙃"is_dist"is <0 — The function is to itself try to calculate if daylight savings time is in effect. More recent libc implementations are better at this than earlier ones. 🧠
Some libc implementations have extra fields in struct tm. The two that perl handles are:
int tm_gmtoff; // Seconds East of UTC [%z]
const char * tm_zone; // Timezone abbreviation [%Z]
These are both output only. Using the respective conversion specifications (enclosed in the square brackets) in the fmt parameter is a portable way to gain access to these values, working both on systems that have and don't have these fields. ✨
Example, in the C locale:
my_strftime( "%A, %B %d, %Y", 0, 0, 0, 12, 11, 95, 0, 0, -1 );
returns
"Tuesday, December 12, 1995"
SV * sv_strftime_ints( SV *fmt, int sec, int min,
int hour, int mday, int mon,
int year, int isdst)
SV * Perl_sv_strftime_ints(pTHX_ SV *fmt, int sec, int min,
int hour, int mday, int mon,
int year, int isdst)
SV * sv_strftime_tm ( SV *fmt,
const struct tm *mytm)
SV * Perl_sv_strftime_tm (pTHX_ SV *fmt,
const struct tm *mytm)
char * my_strftime ( const char *fmt, int sec,
int min, int hour, int mday,
int mon, int year, int wday,
int yday, int isdst)
char * Perl_my_strftime (pTHX_ const char *fmt, int sec,
int min, int hour, int mday,
int mon, int year, int wday,
int yday, int isdst)
💖 Typedef names (still going, bestie, we're SO close) 💖
DB_Hash_t— this one holds the type of the prefix structure element inside thedb.hheader file. Back in the olden days of DB it wasint, but the newer ones said "no ma'am" and made itsize_t.DB_Prefix_t— same deal, type of the prefix structure element indb.h. Old DB:int. New DB:u_int32_t. glow-up fr.Direntry_t— gets set to either"struct direct"or"struct dirent", depending on whether dirent is available or not. Use this pseudo type to declare your directory entries portably, okay? no cap.Fpos_t— holds the type used to declare file positions in libc. Could befpos_t,long,uint, whatever. You might need to includesys/types.hto get any typedef'ed info.Free_t— the return type offree(). Usuallyvoid, but occasionallyint. quirky of it.Gid_t— holds the return type ofgetgid()and the type of argument tosetrgid()and friends. Typically it's the type of group ids in the kernel. Can beint,ushort,gid_t, etc. Might needsys/types.hfor the typedef'ed info.Gid_t_f— defines the format string used for printing aGid_t.Gid_t_sign— the signedness of aGid_t.1for unsigned,-1for signed.Gid_t_size— the size of aGid_t, in bytes.Groups_t— the type used for the second argument togetgroups()andsetgroups(). Usually this matches gidtype (gid_t), but sometimes it just doesn't, bestie. Can beint,ushort,gid_t, etc. You may needsys/types.hfor typedef'ed info. Only required if you havegetgroups()orsetgroups().Malloc_t— the type of pointer returned bymallocandrealloc.Mmap_t— holds the return type of themmap()system call (and simultaneously the type of its first argument). Usually'void *'or"caddr_t".Mode_t— the type used to declare file modes for systems calls. Usuallymode_t, but might beintorunsigned short. Might needsys/types.hfor typedef'ed info.Netdb_hlen_t— the type used for the 2nd argument togethostbyaddr().Netdb_host_t— the type used for the 1st argument togethostbyaddr().Netdb_name_t— the type used for the argument togethostbyname().Netdb_net_t— the type used for the 1st argument togetnetbyaddr().Off_t— the type used to declare offsets in the kernel. Can beint,long,off_t, etc. You might needsys/types.hfor typedef'ed info.Off_t_size— the number of bytes used by theOff_t.Pid_t— the type used to declare process ids in the kernel. Can beint,uint,pid_t, etc.sys/types.hmight be necessary for typedef'ed info.Rand_seed_t— defines the type of the argument of the random seed function.Select_fd_set_t— the type used for the 2nd, 3rd, and 4th arguments toselect. Usually that's"fd_set" *ifHAS_FD_SETis defined, andint *otherwise. Only useful if you haveselect(), obviously 💅.Shmat_t— the return type of theshmat()system call. Usually'void *'or'char *'.Signal_t— its value is eithervoidorint, matching the appropriate return type of a signal handler. So you can declare a signal handler with"Signal_t" (*handler)(), and define the handler using"Signal_t" handler(sig).Size_t— the type used to declare length parameters for string functions. Usuallysize_t, but may beunsigned long,int, etc. Might needsys/types.hfor typedef'ed info.Size_t_size— the size of aSize_tin bytes.Sock_size_t— the type used for the size argument of various socket calls (just the base type, not the pointer-to).SSize_t— the type used by functions that return a count of bytes or an error condition. It must be a signed type. Usuallyssize_t, but may belongorint, etc. You might needsys/types.horunistd.hfor typedef'ed info. We will pick a type such thatsizeof(SSize_t) == sizeof(Size_t).Time_t— the type returned bytime(). Can belong, ortime_tonBSDsites (in which casesys/types.hshould be included).Uid_t— the type used to declare user ids in the kernel. Can beint,ushort,uid_t, etc.sys/types.hmight be needed for typedef'ed info.Uid_t_f— defines the format string used for printing aUid_t.Uid_t_sign— the signedness of aUid_t.1for unsigned,-1for signed.Uid_t_size— the size of aUid_tin bytes.
🌈 Unicode Support 🌈
Unicode Support in perlguts has a whole intro to this API, so go read that if you want the lore.
Also peep Character classification, Character case changing, and String Handling. And lowkey, various functions outside this section also work specially with Unicode — just search for the string "utf8" in this document, iykyk.
BOM_UTF8— this is a macro that evaluates to a string constant of the UTF-8 bytes that define the Unicode BYTE ORDER MARK (U+FEFF) for whatever platform perl got compiled on. The whole point is letting your code use a mnemonic for this character that works on both ASCII and EBCDIC platforms 🫶. Andsizeof(BOM_UTF8) - 1gets you its length in bytes.bytes_cmp_utf8— compares the sequence of characters (stored as octets) inb,blenwith the sequence of characters (stored as UTF-8) inu,ulen. Returns0if they're equal,-1or-2if the first string is less than the second,+1or+2if the first string is greater than the second.
You get -1 or +1 if the shorter string was identical to the start of the longer string. You get -2 or +2 if there was an actual difference between characters within the strings.
int bytes_cmp_utf8( const U8 *b, STRLEN blen,
const U8 *u, STRLEN ulen)
int Perl_bytes_cmp_utf8(pTHX_ const U8 *b, STRLEN blen,
const U8 *u, STRLEN ulen)
bytes_from_utf8*— described over underutf8_to_bytes_overwrite✨bytes_to_utf8,bytes_to_utf8_free_me,bytes_to_utf8_temp_pv— okay so all three of these convert a stringsof length*lenpbytes from the native encoding into UTF-8 (UTF-EBCDIC on EBCDIC platforms), returning a pointer to the UTF-8 string and setting*lenpto its length in bytes. Sisters, but not twins 💅bytes_to_utf8always allocates new memory for the result, and makes sure it's NUL-terminated.bytes_to_utf8_free_mesimply returns a pointer to the input string if the string's UTF-8 representation is the same as its native representation. Otherwise it acts likebytes_to_utf8, returning a pointer to new memory containing the conversion of the input. In other words, it returns the input string if converting the string would be a total no-op. Note that when no new string gets allocated, the function can't add a NUL to the original string if one wasn't already there. 🙈- In both of those cases, the caller is responsible for arranging for any new memory to get freed. not me forgetting that, bestie.
bytes_to_utf8_temp_pvsimply returns a pointer to the input string if the string's UTF-8 representation is the same as its native representation, so it behaves likebytes_to_utf8_free_mein that situation. Otherwise it behaves likebytes_to_utf8, returning a pointer to new memory containing the conversion of the input. The difference is that it also arranges for the new memory to automatically get freed by callingSAVEFREEPVon it. iconic behaviour honestly.
bytes_to_utf8_free_me takes an extra parameter, free_me, to communicate to the caller whether memory was allocated or not. If that parameter is NULL, bytes_to_utf8_free_me acts identically to bytes_to_utf8, always allocating new memory.
But when it's a non-NULL pointer, bytes_to_utf8_free_me stores into it either NULL if no memory was allocated, or a pointer to that new memory. Which unlocks this absolutely blessed paradigm:
void * free_me;
U8 converted = bytes_to_utf8_free_me(string, &len, &free_me);
...
Safefree(free_me);
You don't have to know if memory was allocated or not. Just call Safefree unconditionally 🎀. free_me will contain a suitable value to pass to Safefree so it does the right thing, regardless. And honestly, your design is likely flawed if you find yourself using free_me for anything other than passing to Safefree. periodt.
Upon return, the number of variants in the string can be computed by having saved the value of *lenp before the call, and subtracting the after-call value of *lenp from it.
If you want to convert to UTF-8 from encodings other than the native (Latin1 or EBCDIC), see "sv_recode_to_utf8"().
U8 * bytes_to_utf8 ( const U8 *s, STRLEN *lenp)
U8 * Perl_bytes_to_utf8 (pTHX_ const U8 *s, STRLEN *lenp)
U8 * bytes_to_utf8_free_me( const U8 *s, STRLEN *lenp,
void **free_me)
U8 * Perl_bytes_to_utf8_free_me(pTHX_ const U8 *s, STRLEN *lenp,
void **free_me)
U8 * bytes_to_utf8_temp_pv( const U8 *s, STRLEN *lenp)
U8 * Perl_bytes_to_utf8_temp_pv(pTHX_ const U8 *s, STRLEN *lenp)
c9strict_utf8_to_uv*— described underutf8_to_uv🌸DO_UTF8— returns a bool telling you whether or not the PV insvis to be treated as being encoded in UTF-8.
You should use this after a call to SvPV() or one of its variants, in case any call to string overloading updates the internal UTF-8 encoding flag. don't get caught slipping 💀
bool DO_UTF8(SV* sv)
✨ extended_utf8_to_uv*
Spilled under utf8_to_uv, bestie — go read that one 💖
💅 foldEQ_utf8
Returns true if the leading portions of the strings s1 and s2 (either or both of which may be in UTF-8) are the same case-insensitively; false otherwise. How far into the strings to compare is decided by the other input parameters, okay?
If u1 is true, the string s1 is assumed to be in UTF-8-encoded Unicode; otherwise it's assumed to be in native 8-bit encoding. Correspondingly for u2 with respect to s2.
If the byte length l1 is non-zero, it says how far into s1 to check for fold equality. In other words, s1+l1 will be used as a goal to reach. The scan won't be considered a match unless the goal is reached, and scanning won't continue past that goal. Correspondingly for l2 with respect to s2.
If pe1 is non-NULL and the pointer it points to is not NULL, that pointer is considered an end pointer to the position 1 byte past the maximum point in s1 beyond which scanning will not continue under any circumstances. (This routine assumes that UTF-8 encoded input strings are not malformed; malformed input can cause it to read past pe1). This means that if both l1 and pe1 are specified, and pe1 is less than s1+l1, the match will never be successful because it can never get as far as its goal (and in fact is asserted against). Correspondingly for pe2 with respect to s2.
fr fr, malformed UTF-8 input can make this read past pe1. No cap, that's straight from the source.
At least one of s1 and s2 must have a goal (at least one of l1 and l2 must be non-zero), and if both do, both have to be reached for a successful match. Also, if the fold of a character is multiple characters, all of them must be matched (see the tr21 reference below for 'folding').
Upon a successful match, if pe1 is non-NULL, it will be set to point to the beginning of the next character of s1 beyond what was matched. Correspondingly for pe2 and s2.
For case-insensitiveness, the "casefolding" of Unicode is used instead of upper/lowercasing both the characters, see <https://www.unicode.org/reports/tr21/> (Case Mappings).
I32 foldEQ_utf8(const char *s1, char **pe1, UV l1, bool u1,
const char *s2, char **pe2, UV l2, bool u2)
🌸 is_ascii_string*
Described under is_utf8_invariant_string 💜
🦋 is_c9strict_utf8_string*
🦋 is_c9strict_utf8_string_loc*
🦋 is_c9strict_utf8_string_loclen*
All three described under is_utf8_string, bestie ✨
🎀 is_invariant_string*
Described under is_utf8_invariant_string 💖
⭐ is_strict_utf8_string*
⭐ is_strict_utf8_string_loc*
⭐ is_strict_utf8_string_loclen*
Described under is_utf8_string, go look 👑
🧚 is_utf8_char_buf*
Described under isUTF8_CHAR 🌈
🔥 is_utf8_fixed_width_buf_flags
🔥 is_utf8_fixed_width_buf_loc_flags
🔥 is_utf8_fixed_width_buf_loclen_flags
Each of these returns TRUE if the fixed-width buffer starting at s with length len is entirely valid UTF-8, subject to the restrictions given by flags; otherwise they return FALSE. Iconic behaviour, honestly.
If flags is 0, any well-formed UTF-8, as extended by Perl, is accepted without restriction. If the final few bytes of the buffer don't form a complete code point, this will return TRUE anyway, provided that is_utf8_valid_partial_char_flags returns TRUE for them. 💅
flags can be zero or any combination of the UTF8_DISALLOW_foo flags accepted by utf8_to_uv, and with the same meanings.
The functions differ from is_utf8_string_flags only in that the latter returns FALSE if the final few bytes of the string don't form a complete code point.
is_utf8_fixed_width_buf_loc_flags> does all the preceding, but takes an extra parameter, ep into which it stores the location of the failure, if ep is not NULL. If instead the function returns TRUE, *ep will point to the beginning of any partial character at the end of the buffer; if there is no partial character *ep will contain s+len.
is_utf8_fixed_width_buf_loclen_flags> does all the preceding, but takes another extra parameter, el into which it stores the number of complete, valid characters found, if el is not NULL.
bool is_utf8_fixed_width_buf_flags (
const U8 * const s,
STRLEN len,
const U32 flags)
bool is_utf8_fixed_width_buf_loc_flags (
const U8 * const s,
STRLEN len,
const U8 **ep,
const U32 flags)
bool is_utf8_fixed_width_buf_loclen_flags(
const U8 * const s,
STRLEN len,
const U8 **ep,
STRLEN *el,
const U32 flags)
bool Perl_is_utf8_fixed_width_buf_loclen_flags(
const U8 * const s,
STRLEN len,
const U8 **ep,
STRLEN *el,
const U32 flags)
🌈 is_utf8_invariant_string
🌈 is_utf8_invariant_string_loc
🌈 is_ascii_string
🌈 is_invariant_string
Each of these returns TRUE if the first len bytes of the string s are the same regardless of the UTF-8 encoding of the string (or UTF-EBCDIC encoding on EBCDIC machines); otherwise they returns FALSE. That is, they return TRUE if they are UTF-8 invariant. On ASCII-ish machines, all the ASCII characters and only the ASCII characters fit this definition. On EBCDIC machines, the ASCII-range characters are invariant, but so also are the C1 controls. Wild, right?
If len is 0, it will be calculated using strlen(s), (which means if you use this option, that s can't have embedded NUL characters and has to have a terminating NUL byte).
All forms except is_utf8_invariant_string_loc have identical behavior. The only difference with it is that it has an extra pointer parameter, ep, into which, if it isn't NULL, the location of the first UTF-8 variant character in the ep pointer will be stored upon failure. If all characters are UTF-8 invariant, this function does not change the contents of *ep.
is_invariant_string is somewhat misleadingly named. is_utf8_invariant_string is preferred, as it indicates under what conditions the string is invariant.
is_ascii_string is misleadingly-named. On ASCII-ish platforms, the name isn't misleading: the ASCII-range characters are exactly the UTF-8 invariants. But EBCDIC machines have more UTF-8 invariants than just the ASCII characters, so the name is_utf8_invariant_string is preferred. the way the naming is giving "close enough" 😭
See also is_utf8_string and is_utf8_fixed_width_buf_flags.
bool is_utf8_invariant_string (const U8 * const s,
STRLEN len)
bool is_utf8_invariant_string_loc(const U8 * const s,
STRLEN len,
const U8 **ep)
bool Perl_is_utf8_invariant_string_loc(const U8 * const s,
STRLEN len,
const U8 **ep)
bool is_ascii_string (const U8 * const s,
STRLEN len)
bool is_invariant_string (const U8 * const s,
STRLEN len)
👑 is_utf8_string
👑 is_utf8_string_loc
👑 is_utf8_string_loclen
👑 is_strict_utf8_string
👑 is_strict_utf8_string_loc
👑 is_strict_utf8_string_loclen
👑 is_c9strict_utf8_string
👑 is_c9strict_utf8_string_loc
👑 is_c9strict_utf8_string_loclen
👑 is_utf8_string_flags
👑 is_utf8_string_loc_flags
👑 is_utf8_string_loclen_flags
Each of these returns TRUE if the first len bytes of string s form a valid UTF-8 string for varying degrees of strictness, FALSE otherwise. If len is 0, it will be calculated using strlen(s) (which means if you use this option, that s can't have embedded NUL characters and has to have a terminating NUL byte). Note that all characters being ASCII constitute 'a valid UTF-8 string'. Cute, right? 🎀
Some of the functions also return information about the string. Those that have the suffix _loc in their names have an extra parameter, ep. If that is not NULL, the function stores into it the location of how far it got in parsing s. If the function is returning TRUE, this will be a pointer to the byte immediately after the end of s. If FALSE, it will be the location of the first byte that fails the criteria.
The functions that instead have the suffix _loclen have a second extra parameter, el. They act as the plain _loc functions do with their ep parameter, but if el is not null, the functions store into it the number of UTF-8 encoded characters found at the point where parsing stopped. If the function is returning TRUE, this will be the full count of the UTF-8 characters in s; if FALSE, it will be the count before the first invalid one.
is_utf8_string (and is_utf8_string_loc and is_utf8_string_loclen) consider Perl's extended UTF-8 to be valid. That means that code points above Unicode, surrogates, and non-character code points are all considered valid by this function. Problems may arise in interchange with non-Perl applications, or (unlikely) between machines with different word sizes. 🙈
is_strict_utf8_string (and is_strict_utf8_string_loc and is_strict_utf8_string_loclen) consider only Unicode-range (0 to 0x10FFFF) code points to be valid, with the surrogates and non-character code points invalid. This level of strictness is what is safe to accept from outside sources that use Unicode rules. this is the one you want for untrusted input, bestie, no cap 💖
The forms whose names contain "c9strict" are the ones that vibe with the level of strictness laid out in Unicode Corrigendum #9 <http://www.unicode.org/versions/corrigendum9.html>. Translation bestie: Unicode-range code points, including the non-character ones, count as valid here — but the surrogates do NOT get a pass. This level of strictness is considered safe for cooperating components that actually know how the other components handle non-character code points. It's giving "we trust each other" energy. 🫶
The forms whose names contain "_flags" let you customize the acceptable level of strictness, because of course you want options. They have an extra parameter, "flags", to indicate the types of code points that are acceptable. So: if "flags" is 0, they give the same results as "is_utf8_string" (and kin); if "flags" is "UTF8_DISALLOW_ILLEGAL_INTERCHANGE", they give the same results as "is_strict_utf8_string" (and kin); and if "flags" is "UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE", they give the same results as "is_c9strict_utf8_string" (and kin). Otherwise "flags" may be any combination of the "UTF8_DISALLOW_foo" flags understood by "utf8_to_uv", with the same meanings. Slay. 💅
Real talk though: it's better to use one of the non-"_flags" functions if they give you the desired strictness, as those have a better chance of being inlined by the C compiler. Free speed, no cap. 🔥
See also "is_utf8_invariant_string", "is_utf8_fixed_width_buf_flags",
bool is_utf8_string (const U8 *s,
STRLEN len)
bool is_utf8_string_loc (const U8 *s,
const STRLEN len,
const U8 **ep)
bool Perl_is_utf8_string_loc (const U8 *s,
const STRLEN len,
const U8 **ep)
bool is_utf8_string_loclen (const U8 *s,
STRLEN len,
const U8 **ep,
STRLEN *el)
bool Perl_is_utf8_string_loclen (const U8 *s,
STRLEN len,
const U8 **ep,
STRLEN *el)
bool is_strict_utf8_string (const U8 *s,
STRLEN len)
bool is_strict_utf8_string_loc (const U8 *s,
STRLEN len,
const U8 **ep)
bool is_strict_utf8_string_loclen (const U8 *s,
STRLEN len,
const U8 **ep,
STRLEN *el)
bool Perl_is_strict_utf8_string_loclen (const U8 *s,
STRLEN len,
const U8 **ep,
STRLEN *el)
bool is_c9strict_utf8_string (const U8 *s,
STRLEN len)
bool is_c9strict_utf8_string_loc (const U8 *s,
STRLEN len,
const U8 **ep)
bool is_c9strict_utf8_string_loclen(const U8 *s,
STRLEN len,
const U8 **ep,
STRLEN *el)
bool Perl_is_c9strict_utf8_string_loclen(const U8 *s,
STRLEN len,
const U8 **ep,
STRLEN *el)
bool is_utf8_string_flags (const U8 *s,
STRLEN len,
const U32 flags)
bool Perl_is_utf8_string_flags (const U8 *s,
STRLEN len,
const U32 flags)
bool is_utf8_string_loc_flags (const U8 *s,
STRLEN len,
const U8 **ep,
const U32 flags)
bool is_utf8_string_loclen_flags (const U8 *s,
STRLEN len,
const U8 **ep,
STRLEN *el,
const U32 flags)
bool Perl_is_utf8_string_loclen_flags (const U8 *s,
STRLEN len,
const U8 **ep,
STRLEN *el,
const U32 flags)
💫 "is_utf8_valid_partial_char" 💫
🌸 "is_utf8_valid_partial_char_flags" 🌸
These each return FALSE if the sequence of bytes starting at "s" and looking no further than "e - 1" is the UTF-8 encoding for one or more code points. Basically: FALSE gets returned if "s" points to at least one entire UTF-8 encoded character. Whole character present, we're good, no notes. ✨
Otherwise, they return TRUE if there exists at least one non-empty sequence of bytes that, when appended to sequence "s", starting at position "e", causes the entire sequence to be the well-formed UTF-8 of some code point. 🦋
In other words they return TRUE if "s" points to an incomplete UTF-8-encoded code point; FALSE otherwise.
This is useful when a fixed-length buffer is being tested for being well-formed UTF-8, but the final few bytes in it don't comprise a full character — that is, it is split somewhere in the middle of the final code point's UTF-8 representation. (Presumably when the buffer is refreshed with the next chunk of data, the new first bytes will complete the partial code point.) This function is used to verify that the final bytes in the current buffer are in fact the legal beginning of some code point, so that if they aren't, the failure can be signalled without having to wait for the next read. Efficiency queen behavior. 👑
"is_utf8_valid_partial_char" behaves identically to "is_utf8_valid_partial_char_flags" when the latter is called with a zero "flags" parameter. This parameter is used to restrict the classes of code points that are considered to be valid. When zero, Perl's extended UTF-8 is used. Otherwise "flags" can be any combination of the "UTF8_DISALLOW_foo" flags accepted by "utf8_to_uv". If there is any sequence of bytes that can complete the input partial character in such a way that a non-prohibited character is formed, the function returns TRUE; otherwise FALSE. Non-character code points cannot be determined based on partial character input, so TRUE is always returned if "s" looks like it could be the beginning on one of those. But many of the other possible excluded types can be determined from just the first one or two bytes.
bool is_utf8_valid_partial_char (const U8 * const s0,
const U8 * const e)
bool is_utf8_valid_partial_char_flags(const U8 * const s0,
const U8 * const e,
const U32 flags)
bool Perl_is_utf8_valid_partial_char_flags(const U8 * const s0,
const U8 * const e,
const U32 flags)
🎀 "isUTF8_CHAR" 🎀
🎀 "isSTRICT_UTF8_CHAR" 🎀
🎀 "isC9_STRICT_UTF8_CHAR" 🎀
🎀 "isUTF8_CHAR_flags" 🎀
🎀 "is_utf8_char_buf" 🎀
These each evaluate to non-zero if the first few bytes of the string starting at "s" and looking no further than "e - 1" are well-formed UTF-8 that represents some code point — for varying degrees of strictness, because we love a spectrum. 🌈 Otherwise they evaluate to 0. If non-zero, the value gives how many bytes starting at "s" comprise the code point's representation. Any bytes remaining before "e", but beyond the ones needed to form the first code point in "s", are not examined. Mind your business, extra bytes. 🙈
These are used to efficiently decide if the next few bytes in "s" are legal UTF-8 for a single character.
With "isUTF8_CHAR", the code point can be any that will fit in an IV on this machine, using Perl's extension to official UTF-8 to represent those higher than the Unicode maximum of 0x10FFFF. That means that this will consider byte sequences to be valid that are unrecognized or considered illegal by non-Perl applications. Perl does what Perl wants. 💅
With "isSTRICT_UTF8_CHAR", acceptable code points are restricted to those defined by Unicode to be fully interchangeable across applications. This means code points above the Unicode range (max legal is 0x10FFFF), surrogates, and non-character code points are rejected. 🚫
With "isC9_STRICT_UTF8_CHAR", acceptable code points are restricted to those defined by Unicode to be fully interchangeable within an application. This means code points above the Unicode range and surrogates are rejected, but non-character code points are accepted. See Unicode Corrigendum #9 <http://www.unicode.org/versions/corrigendum9.html>.
Use "isUTF8_CHAR_flags" to customize what code points are acceptable. If "flags" is 0, this gives the same results as "isUTF8_CHAR"; if "flags" is "UTF8_DISALLOW_ILLEGAL_INTERCHANGE", this gives the same results as "isSTRICT_UTF8_CHAR"; and if "flags" is "UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE", this gives the same results as "isC9_STRICT_UTF8_CHAR". Otherwise "flags" may be any combination of the "UTF8_DISALLOW_foo" flags understood by "utf8_to_uv", with the same meanings.
The three alternative macros are for the most commonly needed validations; they are likely to run somewhat faster than this more general one, as they can be inlined into your code. ⚡
Use one of the "is_utf8_string" forms to check entire strings.
Note also that a UTF-8 "invariant" character (i.e. ASCII on non-EBCDIC machines) is a valid UTF-8 character. ⭐
Heads up, bestie:"is_utf8_char_buf"is the old name for"isUTF8_CHAR". Do not use it in new code. It's giving legacy. It's giving deprecated. Leave it in the past where it belongs. 💀
...and here's the rest of that signature lineup, still iconic, still exact 💖
Size_t isUTF8_CHAR (const U8 * const s0,
const U8 * const e)
Size_t Perl_isUTF8_CHAR (const U8 * const s0,
const U8 * const e)
Size_t isSTRICT_UTF8_CHAR (const U8 * const s0,
const U8 * const e)
Size_t Perl_isSTRICT_UTF8_CHAR (const U8 * const s0,
const U8 * const e)
Size_t isC9_STRICT_UTF8_CHAR(const U8 * const s0,
const U8 * const e)
Size_t Perl_isC9_STRICT_UTF8_CHAR(const U8 * const s0,
const U8 * const e)
Size_t isUTF8_CHAR_flags (const U8 * const s0,
const U8 * const e,
const U32 flags)
Size_t Perl_isUTF8_CHAR_flags (const U8 * const s0,
const U8 * const e,
const U32 flags)
STRLEN is_utf8_char_buf (const U8 *buf,
const U8 *buf_end)
STRLEN Perl_is_utf8_char_buf (const U8 *buf,
const U8 *buf_end)
LATIN1_TO_NATIVE
okay bestie so this one hands you back the native equivalent of the Latin-1 code point you feed it as ch — and yes that includes ASCII and control characters too, we're not gatekeeping 💅 So like, LATIN1_TO_NATIVE(66) on EBCDIC platforms returns 194. And both of those are just the character B on their own respective platforms. Cute, right? 🎀
On ASCII platforms there's literally no conversion needed, so this macro just expands to its own input — adding no time nor space requirements to the implementation. Queen behaviour, honestly.
For conversion of code points that might be bigger than will fit in a character, go use UNI_TO_NATIVE instead 🦋
U8 LATIN1_TO_NATIVE(U8 ch)
NATIVE_TO_LATIN1
the reverse journey, no cap ✨ Returns the Latin-1 (including ASCII and control characters) equivalent of the input native code point given by ch. So NATIVE_TO_LATIN1(193) on EBCDIC platforms returns 65, and those both represent the character A on their respective platforms. On ASCII platforms no conversion is needed, so this macro expands to just its input, adding no time nor space requirements to the implementation. Periodt.
For conversion of code points potentially larger than will fit in a character, use NATIVE_TO_UNI 💜
U8 NATIVE_TO_LATIN1(U8 ch)
NATIVE_TO_UNI
Returns the Unicode equivalent of the input native code point given by ch. So NATIVE_TO_UNI(195) on EBCDIC platforms returns 67, and those each represent the character C on their respective platforms. On ASCII platforms, again, no conversion is needed — this macro expands to just its input, adding no time nor space requirements to the implementation. Lowkey elegant 😌
UV NATIVE_TO_UNI(UV ch)
pv_uni_display
Builds to the scalar dsv a displayable version of the UTF-8 encoded string spv, length len, the displayable version being at most pvlim bytes long — and if it's longer, the rest gets truncated and ... will be appended. It's giving "we ran out of room, sorry bestie" 😭
The flags argument can have any combination of these flag bits 👑
UNI_DISPLAY_ISPRINT— to display isPRINT()able characters as themselvesUNI_DISPLAY_BACKSLASH— to display the\\[nrfta\\]as the backslashed versions (like\n)
(UNI_DISPLAY_BACKSLASH is preferred over UNI_DISPLAY_ISPRINT for \\.)
UNI_DISPLAY_BACKSPACE— to display\bfor a backspace, but only whenUNI_DISPLAY_BACKSLASHalso is set.UNI_DISPLAY_REGEX— This a shorthand forUNI_DISPLAY_ISPRINTalong withUNI_DISPLAY_BACKSLASH.UNI_DISPLAY_QQ— This a shorthand for all threeUNI_DISPLAY_ISPRINT,UNI_DISPLAY_BACKSLASH, andUNI_DISPLAY_BACKSLASH.
The pointer to the PV of the dsv is returned.
See also sv_uni_display 🌸
char * pv_uni_display( SV *dsv, const U8 *spv,
STRLEN len, STRLEN pvlim,
UV flags)
char * Perl_pv_uni_display(pTHX_ SV *dsv, const U8 *spv,
STRLEN len, STRLEN pvlim,
UV flags)
REPLACEMENT_CHARACTER_UTF8
This is a macro that evaluates to a string constant of the UTF-8 bytes that define the Unicode REPLACEMENT CHARACTER (U+FFFD) for the platform that perl is compiled on. So you get a mnemonic for this character that works on both ASCII and EBCDIC platforms 🫶 And if you need its length in bytes, sizeof(REPLACEMENT_CHARACTER_UTF8) - 1 has you covered.
strict_utf8_to_uv*
Described under utf8_to_uv 🔍
sv_cat_decode
So encoding is assumed to be an Encode object, the PV of ssv is assumed to be octets in that encoding, and decoding the input starts from the position which (PV + *offset) pointed to. dsv will be concatenated with the decoded UTF-8 string from ssv ✨
Decoding will terminate when the string tstr appears in the decoding output, or when the input ends on the PV of ssv. The value which offset points to will be modified to the last input position on ssv.
Returns TRUE if the terminator was found, else returns FALSE. No in-between, we don't do ambiguity here 💅
bool sv_cat_decode( SV *dsv, SV *encoding, SV *ssv,
int *offset, char *tstr, int tlen)
bool Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding, SV *ssv,
int *offset, char *tstr, int tlen)
sv_recode_to_utf8
encoding is assumed to be an Encode object. On entry, the PV of sv is assumed to be octets in that encoding, and sv will be converted into Unicode (and UTF-8) 🦋
Now listen carefully because there are conditions, bestie:
- If
svalready is UTF-8 (or if it is notPOK), or ifencodingis not a reference, nothing is done tosv. - If
encodingis not anEncode::XSEncoding object, bad things will happen. 😭 (See encoding and Encode.)
The PV of sv is returned.
char * sv_recode_to_utf8( SV *sv, SV *encoding)
char * Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
sv_uni_display
Builds to the scalar dsv a displayable version of the scalar sv, the displayable version being at most pvlim bytes long — if it's longer, the rest is truncated and ... will be appended. Same vibe as before, different scalar 🌈
The flags argument is as in pv_uni_display().
The pointer to the PV of the dsv is returned.
char * sv_uni_display( SV *dsv, SV *ssv, STRLEN pvlim,
UV flags)
char * Perl_sv_uni_display(pTHX_ SV *dsv, SV *ssv, STRLEN pvlim,
UV flags)
UNI_TO_NATIVE
Returns the native equivalent of the input Unicode code point given by ch. So UNI_TO_NATIVE(68) on EBCDIC platforms returns 196, and these each represent the character D on their respective platforms. On ASCII platforms no conversion is needed, so this macro expands to just its input, adding no time nor space requirements to the implementation. Chef's kiss 👑
UV UNI_TO_NATIVE(UV ch)
UNICODE_IS_NONCHAR
Returns a boolean as to whether or not uv is one of the Unicode non-character code points. Yes/no, that's the whole gig ✨
bool UNICODE_IS_NONCHAR(const UV uv)
UNICODE_IS_REPLACEMENT
Returns a boolean as to whether or not uv is the Unicode REPLACEMENT CHARACTER 🎀
bool UNICODE_IS_REPLACEMENT(const UV uv)
UNICODE_IS_SUPER
Returns a boolean as to whether or not uv is above the maximum legal Unicode code point of U+10FFFF. So like, off-the-charts territory 🚀
bool UNICODE_IS_SUPER(const UV uv)
UNICODE_IS_SURROGATE
Returns a boolean as to whether or not uv is one of the Unicode surrogate code points 💀
bool UNICODE_IS_SURROGATE(const UV uv)
UNICODE_REPLACEMENT
Evaluates to 0xFFFD, the code point of the Unicode REPLACEMENT CHARACTER ⭐
UTF8_CHK_SKIP*
Described under UTF8SKIP 🔍
utf8_distance
Returns the number of UTF-8 characters between the UTF-8 pointers a and b 🌸
WARNING: use only if you know that the pointers point inside the same UTF-8 buffer. Do not test me on this one bestie, I'm serious 😭
IV utf8_distance( const U8 *a, const U8 *b)
IV Perl_utf8_distance(pTHX_ const U8 *a, const U8 *b)
utf8_hop
Return the UTF-8 pointer s displaced by off characters, either forward (if off is positive) or backward (if negative). And here's the lil plot twist: s does not need to be pointing to the starting byte of a character. If it isn't, one count of off will be used up to get to the start of the next character for forward hops, and to the start of the current character for negative ones. Clever, honestly 🧚
WARNING: Prefer utf8_hop_safe to this one.
Do NOT use this function unless you knowoffis within the UTF-8 data pointed to bysand that on entrysis aligned on the first byte of a character or just after the last byte of a character. This is a "you better know what you're doing" situation, fr fr 💀
U8 * utf8_hop(const U8 *s, SSize_t off)
U8 * Perl_utf8_hop(const U8 *s, SSize_t off)
utf8_hop_back, utf8_hop_back_overshoot
These each take as input a string encoded as UTF-8 which starts at start, and a position into it given by s, and return the position within it that is s displaced by up to off characters backwards 🦋
If there are fewer than off characters between start and s, the functions return start. We don't go past the edge, we're not chaotic like that (usually).
The functions differ in that utf8_hop_back_overshoot can return how many characters off beyond the edge the request was for. When its parameter, &remaining, is not NULL, the function stores into it the count of the excess; zero if the request was completely fulfilled. The actual number of characters that were displaced can then be calculated as off - remaining. This function acts identically to plain utf8_hop_back when this parameter is NULL. Iconic flexibility ✨
s does not need to be pointing to the starting byte of a character. If it isn't, one count of off will be used up to get to that start 💜
off must be non-positive, and if it's zero, no action is taken at all — s comes back to you unchanged. The fact that it otherwise has to be negative means the earlier description was a whole lie, lowkey, because they didn't wanna burden you with this detail too soon. An off of -2 means "scoot two characters backwards", so the displacement is really the absolute value of off. remaining is gonna be non-positive too. If there was only one character chillin' between start and s, and a displacement of -2 was requested, remaining would get set to -1. The subtraction formula still works out, yielding the result that only -1 character was displaced. math ate that 💅
U8 * utf8_hop_back (const U8 *s, SSize_t off,
const U8 * const start)
U8 * Perl_utf8_hop_back (const U8 *s, SSize_t off,
const U8 * const start)
U8 * utf8_hop_back_overshoot(const U8 *s, SSize_t off,
const U8 * const start,
SSize_t *remaining)
U8 * Perl_utf8_hop_back_overshoot(const U8 *s, SSize_t off,
const U8 * const start,
SSize_t *remaining)
utf8_hop_forward + utf8_hop_forward_overshoot
These each take as input a position, s0, into a string encoded as UTF-8 which ends at the byte before end, and they hand you back the position within it that is s0 displaced by up to off characters forwards. ✨
If there are fewer than off characters between s0 and end, the functions just return end. Simple as.
The functions differ in two ways:
utf8_hop_forward_overshootcan return how many characters beyond the edge the request was for. When its parameter,&remaining, is not NULL, the function stores into it the count of the excess; zero if the request was completely fulfilled. The actual number of characters that were displaced can then be calculated asoff - remaining.utf8_hop_forwardwill panic if it's called withs0already positioned at or beyond the edge of the string ending atendand the request is to go even further over the edge.utf8_hop_forward_overshootpresumes the caller will handle any errors, and just storesoffintoremainingwithout doing anything else.
(The above contains a slight lie. When remaining is NULL, the two functions act identically.) 🙈
s0 does not need to be pointing to the starting byte of a character. If it isn't, one count of off will be used up to get to that start.
off must be non-negative, and if zero, no action is taken; s0 is returned unchanged.
U8 * utf8_hop_forward (const U8 *s, SSize_t off,
const U8 * const end)
U8 * Perl_utf8_hop_forward (const U8 *s, SSize_t off,
const U8 * const end)
U8 * utf8_hop_forward_overshoot(const U8 *s, SSize_t off,
const U8 * const end,
SSize_t *remaining)
U8 * Perl_utf8_hop_forward_overshoot(const U8 *s, SSize_t off,
const U8 * const end,
SSize_t *remaining)
utf8_hop_safe + utf8_hop_overshoot
These each take as input a string encoded as UTF-8 which starts at start, ending at end, plus a position into it given by s, and they return the position within it that is s displaced by up to off characters — either forwards if off is positive, or backwards if off is negative. (Nothing is done if off is 0.) 🦋
If there are fewer than off characters between s and the respective edge, the functions return that edge.
The functions differ in that utf8_hop_overshoot can return how many characters beyond the edge the request was for. When its parameter, &remaining, is not NULL, the function stores into it the count of the excess; zero if the request was completely fulfilled. The actual number of characters that were displaced can then be calculated as off - remaining. This function acts identically to plain utf8_hop_safe when this parameter is NULL.
s does not need to be pointing to the starting byte of a character. If it isn't, one count of off will be used up to get to that start.
To be more precise, the displacement is by the absolute value of off, and the excess count is the absolute value of remaining.
U8 * utf8_hop_safe (const U8 *s, SSize_t off,
const U8 * const start,
const U8 * const end)
U8 * Perl_utf8_hop_safe (const U8 *s, SSize_t off,
const U8 * const start,
const U8 * const end)
U8 * utf8_hop_overshoot(const U8 *s, SSize_t off,
const U8 * const start,
const U8 * const end,
SSize_t *remaining)
U8 * Perl_utf8_hop_overshoot(const U8 *s, SSize_t off,
const U8 * const start,
const U8 * const end,
SSize_t *remaining)
UTF8_IS_INVARIANT
Evaluates to 1 if the byte c represents the same character when encoded in UTF-8 as when not; otherwise it evaluates to 0. UTF-8 invariant characters can be copied as-is when converting to/from UTF-8, which saves time. efficiency queen 👑
In spite of the name, this macro gives the correct result if the input string from which c comes is not encoded in UTF-8.
See UVCHR_IS_INVARIANT for checking if a UV is invariant.
bool UTF8_IS_INVARIANT(char c)
UTF8_IS_NONCHAR
Evaluates to non-zero if the first few bytes of the string starting at s and looking no further than e - 1 are well-formed UTF-8 that represents one of the Unicode non-character code points; otherwise it evaluates to 0. If non-zero, the value gives how many bytes starting at s comprise the code point's representation.
bool UTF8_IS_NONCHAR(const U8 *s, const U8 *e)
UTF8_IS_REPLACEMENT
Evaluates to non-zero if the first few bytes of the string starting at s and looking no further than e - 1 are well-formed UTF-8 that represents the Unicode REPLACEMENT CHARACTER; otherwise it evaluates to 0. If non-zero, the value gives how many bytes starting at s comprise the code point's representation.
bool UTF8_IS_REPLACEMENT(const U8 *s, const U8 *e)
UTF8_IS_SUPER
Recall that Perl recognizes an extension to UTF-8 that can encode code points larger than the ones defined by Unicode, which are 0..0x10FFFF.
This macro evaluates to non-zero if the first few bytes of the string starting at s and looking no further than e - 1 are from this UTF-8 extension; otherwise it evaluates to 0. If non-zero, the return is how many bytes starting at s comprise the code point's representation.
0 is returned if the bytes are not well-formed extended UTF-8, or if they represent a code point that cannot fit in a UV on the current platform. Hence this macro can give different results when run on a 64-bit word machine than on one with a 32-bit word size. 😭
Note that it is illegal in Perl to have code points that are larger than what can fit in an IV on the current machine; and illegal in Unicode to have any that this macro matches.
bool UTF8_IS_SUPER(const U8 *s, const U8 *e)
UTF8_IS_SURROGATE
Evaluates to non-zero if the first few bytes of the string starting at s and looking no further than e - 1 are well-formed UTF-8 that represents one of the Unicode surrogate code points; otherwise it evaluates to 0. If non-zero, the value gives how many bytes starting at s comprise the code point's representation.
bool UTF8_IS_SURROGATE(const U8 *s, const U8 *e)
utf8_length
Returns the number of characters in the sequence of UTF-8-encoded bytes starting at s and ending at the byte just before e. If <s> and <e> point to the same place, it returns 0 with no warning raised. so chill 🧘
If e < s or if the scan would end up past e, it raises a UTF8 warning and returns the number of valid characters.
STRLEN utf8_length( const U8 *s0, const U8 *e)
STRLEN Perl_utf8_length(pTHX_ const U8 *s0, const U8 *e)
UTF8_MAXBYTES
The maximum width of a single UTF-8 encoded character, in bytes.
NOTE: Strictly speaking Perl's UTF-8 should not be called UTF-8 since UTF-8 is an encoding of Unicode, and Unicode's upper limit, 0x10FFFF, can be expressed with 4 bytes. However, Perl thinks of UTF-8 as a way to encode non-negative integers in a binary format, even those above Unicode. it's giving rebel energy fr fr 💀
UTF8_MAXBYTES_CASE
The maximum number of UTF-8 bytes a single Unicode character can uppercase/lowercase/titlecase/fold into.
UTF8_SAFE_SKIP* + UTF8_SKIP*
Described under UTF8SKIP.
utf8_to_bytes_overwrite, utf8_to_bytes_new_pv, utf8_to_bytes_temp_pv, utf8_to_bytes + bytes_from_utf8
NOTE: utf8_to_bytes is experimental and may change or be removed without notice. bestie do not build your whole personality on it 🙈
These each convert a string encoded as UTF-8 into the equivalent native byte representation, if possible. The first three forms are preferred; their API is more convenient to use, and each return true if the result is in bytes; false if the conversion failed. slay ✨
utf8_to_bytes_overwriteutf8_to_bytes_new_pvutf8_to_bytes_temp_pv
These differ primarily in the form of the returned string and the allowed constness of the input string. In each, if the input string was already in native bytes or was not convertible, the input isn't changed.
In each of these three functions, the input s_ptr is a pointer to the string to be converted and *lenp is its length (so that the first byte will be at *sptr[0]).
utf8_to_bytes_overwrite overwrites the input string with the bytes conversion. Hence, the input string should not be const. (Converting the multi-byte UTF-8 encoding to single bytes never expands the result, so overwriting is always feasible.)
Both utf8_to_bytes_new_pv and utf8_to_bytes_temp_pv allocate new memory to hold the converted string, and they never touch the input. That's the whole point, bestie — which means the input string is allowed to be const 😌 They differ in one lil' detail: utf8_to_bytes_temp_pv arranges for that new memory to be freed automatically, while with utf8_to_bytes_new_pv the caller is the one responsible for freeing it. And here's the tea: as explained below, not all successful calls result in new memory being allocated. So this function also hands back to the caller (via an extra parameter, *free_me) a pointer to any new memory, or NULL if none got allocated 🫶
The functions return false when the input is not well-formed UTF-8, or when it contains at least one UTF-8 sequence representing a code point that can't be expressed as a byte. In that case, the contents of *s_ptr and *lenp are not changed. And utf8_to_bytes_new_pv sets *free_me to NULL.
They all return true when either:
- The input turned out to already be in bytes form
- The contents of
*s_ptrand*lenpare not changed.utf8_to_bytes_new_pvsets*free_metoNULL.
- The contents of
- The input was successfully converted
- For
utf8_to_bytes_overwrite- The input string
*s_ptrwas overwritten with the native bytes, including aNULterminator.*lenphas been updated with the new length.
- The input string
- For
utf8_to_bytes_new_pvandutf8_to_bytes_temp_pv- The input string was not changed. Instead, new memory has been allocated containing the translation of the input into native bytes, with a
NULterminator byte.*s_ptrnow points to that new memory, and*lenpcontains its length. - For
utf8_to_bytes_temp_pv, the new memory has been arranged to be automatically freed, via a call toSAVEFREEPV. - For
utf8_to_bytes_new_pv,*free_mehas been set to*s_ptr, and it is the caller's responsibility to free the new memory when done using it. The following paradigm is convenient to use for this:
- The input string was not changed. Instead, new memory has been allocated containing the translation of the input into native bytes, with a
- For
void * free_me;
if (utf8_to_bytes_new_pv(&s, &len, &free_me) {
...
}
else {
...
}
...
Safefree(free_me);
free_mecan be used as a boolean (non-NULLmeaningtrue) to indicate that the input was indeed changed if you need to revisit that later in the code. Your design is likely flawed if you find yourself usingfree_mefor any other purpose 🙈
Note that in all cases, *s_ptr and *lenp will have correct and consistent values, updated as was necessary ✨
Also note that upon successful conversion, the number of variants in the string can be computed by having saved the value of *lenp before the call, and subtracting the after-call value of *lenp from it. This is also true for the other two functions described below 👑
utf8_to_bytes- Plain
utf8_to_bytes(which has never lost its experimental status, btw) also converts a UTF-8 encoded string to bytes, but there are more glitches that the caller has to be prepared to handle 😭 - The input string is passed with one less indirection level,
s. - If the conversion was a noop
- The contents of
sand*lenpare not changed, and the function returnss.
- The contents of
- If the conversion was successful
- The contents of
swere changed, and*lenpupdated to be the correct length. The function returnss(unchanged).
- The contents of
- If the conversion failed
- The contents of
swere not changed. - The function returns
NULLand sets*lenpto-1, cast toSTRLEN. This means that you will have to use a temporary containing the string length to pass to the function if you will need the value afterwards.
- The contents of
- Plain
bytes_from_utf8bytes_from_utf8also converts a potentially UTF-8 encoded stringsto bytes. It preservess, allocating new memory for the converted string.- In contrast to the other functions, the input string to this one need not be UTF-8. If not, the caller has set
*is_utf8pto befalse, and the function does nothing, returning the originals. - Also do nothing if there are code points in the string not expressible in native byte encoding, returning the original
s. - Otherwise,
*is_utf8pis set to0, and the return value is a pointer to a newly created string containing the native byte equivalent ofs, and whose length is returned in*lenp, updated. The new string isNUL-terminated. The caller is responsible for arranging for the memory used by this string to get freed. - The major problem with this function is that memory is allocated and filled even when the input string was already in bytes form 💀
New code should use the first three functions listed above.
bool utf8_to_bytes_overwrite( U8 **s_ptr,
STRLEN *lenp)
bool Perl_utf8_to_bytes_overwrite(pTHX_ U8 **s_ptr,
STRLEN *lenp)
bool utf8_to_bytes_new_pv ( U8 const **s_ptr,
STRLEN *lenp,
void **free_me)
bool Perl_utf8_to_bytes_new_pv (pTHX_ U8 const **s_ptr,
STRLEN *lenp,
void **free_me)
bool utf8_to_bytes_temp_pv ( U8 const **s_ptr,
STRLEN *lenp)
bool Perl_utf8_to_bytes_temp_pv (pTHX_ U8 const **s_ptr,
STRLEN *lenp)
U8 * utf8_to_bytes ( U8 *s, STRLEN *lenp)
U8 * Perl_utf8_to_bytes (pTHX_ U8 *s, STRLEN *lenp)
U8 * bytes_from_utf8 ( const U8 *s,
STRLEN *lenp,
bool *is_utf8p)
U8 * Perl_bytes_from_utf8 (pTHX_ const U8 *s,
STRLEN *lenp,
bool *is_utf8p)
🌈 utf8_to_uv, extended_utf8_to_uv, strict_utf8_to_uv, c9strict_utf8_to_uv, utf8_to_uv_or_die, utf8_to_uvchr_buf, utf8_to_uvchr
🚨DEPRECATED!It is planned to removeutf8_to_uvchrfrom a future release of Perl. Do not use it for new code; remove it from existing code. Periodt.
These functions each translate from UTF-8 to UTF-32 (or UTF-64 on 64 bit platforms). In other words, to a code point ordinal value. (On EBCDIC platforms, the initial encoding is UTF-EBCDIC, and the output is a native code point.)
For example, the string "A" would be converted to the number 65 on an ASCII platform, and to 193 on an EBCDIC one. Converting the string "ABC" would yield the same results, as the functions stop after the first character converted. Converting the string "\N{LATIN CAPITAL LETTER A WITH MACRON} plus anything more in the string" would yield the number 0x100 on both types of platforms, since the first character is U+0100 💖
The functions whose names contain to_uvchr are older than the functions whose names don't have chr in them. The API in the older functions is harder to use correctly, and so they are kept only for backwards compatibility, and may eventually become deprecated. If you are writing a module and use Devel::PPPort, your code can use the new functions back to at least Perl v5.7.1 👑
All the functions accept, without complaint, well-formed UTF-8 for any non-problematic Unicode code point 0 .. 0x10FFFF. There are two types of Unicode problematic code points: surrogate characters and non-character code points. (See perlunicode.) Some of the functions reject one or both of these. Private use characters and those code points yet to be assigned to a particular character are never considered problematic. Additionally, most of the functions accept non-Unicode code points, those starting at 0x110000.
There are two sets of these functions:
💫 utf8_to_uv forms
Almost all code should use only utf8_to_uv, extended_utf8_to_uv, strict_utf8_to_uv, c9strict_utf8_to_uv, or utf8_to_uv_or_die. The other functions are either the problematic old form, or are for specialized uses.
utf8_to_uv_or_die has a simpler interface than the other four, for use when any errors encountered should be fatal. It throws an exception with any errors found, otherwise it returns the code point the input sequence represents ✨
The other four functions each return true if the sequence of bytes starting at s form a complete, legal UTF-8 (or UTF-EBCDIC) sequence for a code point; or false otherwise. They take an extra parameter, the address of an IV, &cp. *cp will be set to the native code point value the sequence represents, and *advance will be set to its length, in bytes.
⚠️ If the functions returnsfalse,*cpis set to the Unicode REPLACEMENT CHARACTER, and*advanceto the next position alongs, where the next possible UTF-8 character could begin. Failing to use this position as the next starting point during parsing of strings has led to successful attacks by crafted inputs. No cap, this one is serious 💀
The functions only examine as many bytes along s as are needed to form a complete UTF-8 representation of a single code point; they never examine the byte at e, or beyond. They return false (or die in the case of utf8_to_uv_or_die) if the code point requires more than e - s bytes to represent.
The functions differ only in what flavor of UTF-8 they accept. All reject syntactically invalid UTF-8.
strict_utf8_to_uv🥁
okay so continuing the list — this one's the strict girlie 💅
- it additionally rejects any UTF-8 that translates into a code point that isn't specified by Unicode to be freely exchangeable, namely the surrogate characters and non-character code points (besides non-Unicode code points, any above
0x10FFFF). It does not raise a warning when rejecting these. c9strict_utf8_to_uv— instead uses the exchangeable definition given by Unicode's Corregendum #9, which accepts non-character code points while still rejecting surrogates. It does not raise a warning when rejecting these either.utf8_to_uvutf8_to_uv_or die
these two accept all syntactically valid UTF-8, as extended by Perl to allow 64-bit code points to be encoded.
extended_utf8_to_uv is merely a synonym for utf8_to_uv. Use this form to draw attention to the fact that it accepts any code point. But since Perl programs traditionally do this by default, plain utf8_to_uv is the form most often used. ✨
Whenever syntactically invalid input is rejected, an explanatory warning message is raised, unless utf8 warnings (or the appropriate subcategory) are turned off. And bestie, a given input sequence may contain multiple malformations, giving rise to multiple warnings, as the functions attempt to find and report on all malformations in a sequence. All the possible malformations are listed in utf8_to_uv_msgs, with some examples of multiple ones for the same sequence. You can use that function or utf8_to_uv_flags to exert more control over the input that is considered acceptable, and the warnings that are raised. 🎀
Often, s is an arbitrarily long string containing the UTF-8 representations of many code points in a row, and these functions are called in the course of parsing s to find all those code points.
If your code doesn't know how to deal with illegal input, as would be typical of a low level routine, the loop could look like:
while (s < e) {
Size_t advance;
UV cp;
(void) utf8_to_uv(s, e, &cp, &advance);
<handle 'cp'>
s += advance;
}
A REPLACEMENT CHARACTER will be inserted everywhere that malformed input occurs. Obviously, we aren't expecting such outcomes, but your code will be protected from attacks and many harmful effects that could otherwise occur. 🛡️
If the situation is such that it would be a bug for the input to be invalid, a somewhat simpler loop suffices:
while (s < e) {
Size_t advance;
UV cp = utf8_to_uv_or_die(s, e, &advance);
<handle 'cp'>
s += advance;
}
This will throw an exception on invalid input, so your code doesn't have to concern itself with that possibility. periodt.
If you do have a plan for handling malformed input, you could instead write:
while (s < e) {
Size_t advance;
UV cp;
if (UNLIKELY(! utf8_to_uv(s, e, &cp, &advance)) {
<bail out or convert to handleable>
}
<handle 'cp'>
s += advance;
}
You may pass NULL to these functions instead of a pointer to your advance variable. But the only legitimate case to do this is if you are only examining the first character in s, and have no plans to ever look further. You could also advance by using UTF8SKIP, but this gives the correct result if and only if the input is well-formed; and this practice has led to successful attacks against such code; and it is extra work always, as the functions have already done the equivalent work and return the correct value in advance, regardless of whether the input is well-formed or not. 💀
Except withutf8_to_uv_or_die, you must always pass a non-NULL pointer into which to store the (first) code pointsrepresents. If you don't care about this value, you should be using one of theisUTF8_CHARfunctions instead.
🦋 utf8_to_uvchr forms 🦋
These are the old form equivalents of utf8_to_uv (and its synonym, extended_utf8_to_uv). They are utf8_to_uvchr and utf8_to_uvchr_buf. There is no old form equivalent of either strict_utf8_to_uv nor c9strict_utf8_to_uv.
utf8_to_uvchr is DEPRECATED. Do NOT use it; it is a security hole ready to bring destruction onto you and yours. 😭
utf8_to_uvchr_buf is discouraged and may eventually become deprecated. It checks if the sequence of bytes starting at s form a complete, legal UTF-8 (or UTF-EBCDIC) sequence for a code point. If so, it returns the code point value the sequence represents, and *retlen will be set to its length, in bytes. Thus, the next possible character in s begins at s + *retlen.
The function only examines as many bytes along s as are needed to form a complete UTF-8 representation of a single code point, but it never examines the byte at e, or beyond.
If the sequence examined starting at s is not legal Perl extended UTF-8, the translation fails, and the resultant behavior unfortunately depends on if the warnings category utf8 is enabled or not.
If 'utf8' warnings are disabled
The Unicode REPLACEMENT CHARACTER is silently returned, and *retlen is set (if retlen isn't NULL) so that (s + *retlen) is the next possible position in s that could begin a non-malformed character.
But note that it is ambiguous whether a REPLACEMENT CHARACTER was actually in the input, or if this function synthetically generated one. In the unlikely event that you care, you'd have to examine the input to disambiguate. 🙈
If 'utf8' warnings are enabled
A warning will be displayed, and 0 is returned and *retlen is set (if retlen isn't NULL) to -1.
But note that 0 may also be returned if *s is a legal NUL character. This means that you have to disambiguate a 0 return. You can do this by checking that the first byte of s is indeed a NUL; or by making sure to always pass a non-NULL retlen pointer, and by examining it.
Also note that should you wish to proceed with parsing s, you have no easy way of knowing where to start looking in it for the next possible character. It is important to look in the right place to prevent attacks on your code. It would be better to have instead called an equivalent function that provides this information; any of the utf8_to_uv series, or utf8n_to_uvchr.
Because of these quirks, utf8_to_uvchr_buf is very difficult to use correctly and handle all cases. Generally, you need to bail out at the first failure it finds.
The deprecated utf8_to_uvchr behaves the same way as utf8_to_uvchr_buf for well-formed input, and for the malformations it is capable of finding, but doesn't find all of them, and it can read beyond the end of the input buffer, which is why it is deprecated. 💀
The utf8_to_uv() family of functions is preferred because they make it easier to write code safe from attacks. You should be converting to them; this will result in simpler, more robust code. slay 💅
bool utf8_to_uv ( const U8 * const s,
const U8 * const e,
UV *cp_p, Size_t *advance_p)
bool Perl_utf8_to_uv ( const U8 * const s,
const U8 * const e,
UV *cp_p, Size_t *advance_p)
bool extended_utf8_to_uv( const U8 * const s,
const U8 * const e,
UV *cp_p, Size_t *advance_p)
bool Perl_extended_utf8_to_uv( const U8 * const s,
const U8 * const e,
UV *cp_p, Size_t *advance_p)
bool strict_utf8_to_uv ( const U8 * const s,
const U8 * const e,
UV *cp_p, Size_t *advance_p)
bool Perl_strict_utf8_to_uv ( const U8 * const s,
const U8 * const e,
UV *cp_p, Size_t *advance_p)
bool c9strict_utf8_to_uv( const U8 * const s,
const U8 * const e,
UV *cp_p, Size_t *advance_p)
bool Perl_c9strict_utf8_to_uv( const U8 * const s,
const U8 * const e,
UV *cp_p, Size_t *advance_p)
UV utf8_to_uv_or_die ( const U8 * const s,
const U8 *e,
Size_t *advance_p)
UV Perl_utf8_to_uv_or_die ( const U8 * const s,
const U8 *e,
Size_t *advance_p)
UV utf8_to_uvchr_buf ( const U8 *s, const U8 *send,
STRLEN *retlen)
UV Perl_utf8_to_uvchr_buf (pTHX_ const U8 *s, const U8 *send,
STRLEN *retlen)
UV utf8_to_uvchr ( const U8 *s, STRLEN *retlen)
UV Perl_utf8_to_uvchr (pTHX_ const U8 *s, STRLEN *retlen)
🌸 utf8_to_uv_errors* 🌸
Described under utf8_to_uv_msgs. 🫶
🌈 utf8_to_uv_flags 🌈
🌈 utf8n_to_uvchr 🌈
These functions are extensions of utf8_to_uv, where you need more control over what UTF-8 sequences are acceptable. These functions are unlikely to be needed except for specialized purposes. iykyk ✨
okay so utf8n_to_uvchr is lowkey more like an extension of utf8_to_uvchr_buf than a whole separate creature — same vibes, fewer quirks, but it wants you to describe the bytes in s a different way. It takes a curlen parameter instead of an e parameter, so the furthest byte in s it's allowed to look at is s + curlen - 1. 💅 Its return value is, just like utf8_to_uvchr_buf, ambiguous about the NUL and REPLACEMENT characters (it's a family thing, we don't question it), BUT the value of *retlen can be relied on — except when the UTF8_CHECK_ONLY flag (described below) is in the mix — to tell you where the next possible character along s starts, which nukes that quirk right out of existence. So fr fr: you always should use *retlen to determine where the next character in s starts. 👑
These functions have an extra parameter, flags, on top of the ones in utf8_to_uv and utf8_to_uvchr_buf, and you can use it to broaden or restrict what counts as acceptable UTF-8. flags means the exact same thing and behaves the exact same way in both functions. When flags is 0, these functions accept any syntactically valid Perl-extended-UTF-8 sequence that doesn't overflow the platform's word size. ✨
There are flags that apply to accepting particular sequences, and flags that apply to raising warnings about running into sequences. Each type is fully independent of the other, bestie. You can reject and not warn; warn and still accept; or both reject and warn. Rejecting means the sequence gets translated into the Unicode REPLACEMENT CHARACTER instead of what it was meant to represent. 😭
Unless it says otherwise below, warnings are subject to the utf8 warnings category being on.
UTF8_CHECK_ONLY— This suppresses any warnings. And it changes what gets stored into*retlenwith theuvchrfamily of functions (for the worse, ngl 💀). It is not likely to be of use to you. You can useUTF8_ALLOW_ANY(described below) to also turn off warnings, and that flag doesn't adversely affect*retlen.
This flag is ignored if UTF8_DIE_IF_MALFORMED is also set.
UTF8_FORCE_WARN_IF_MALFORMED— Normally, no warnings are generated if warnings are turned off lexically or globally, no matter what flags say to the contrary. But this flag effectively turns on warnings temporarily for the duration of this function's execution. 🔥
Do not use it lightly.
This flag is ignored if UTF8_CHECK_ONLY is also set.
UTF8_DISALLOW_SURROGATEUTF8_WARN_SURROGATE— These reject and/or warn about UTF-8 sequences that represent surrogate characters. The warning categoriesutf8andnon_unicodecontrol if warnings are actually raised.UTF8_DISALLOW_NONCHARUTF8_WARN_NONCHAR— These reject and/or warn about UTF-8 sequences that represent non-character code points. The warning categoriesutf8andnoncharcontrol if warnings are actually raised.UTF8_DISALLOW_SUPERUTF8_WARN_SUPER— These reject and/or warn about UTF-8 sequences that represent code points above0x10FFFF. The warning categoriesutf8andnon_unicodecontrol if warnings are actually raised.UTF8_DISALLOW_ILLEGAL_INTERCHANGEUTF8_WARN_ILLEGAL_INTERCHANGE— These are the same as having selected all three of the correspondingSURROGATE,NONCHARandSUPERflags listed above. All such code points are not considered to be safely freely exchangeable between processes. 🚫UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGEUTF8_WARN_ILLEGAL_C9_INTERCHANGE— These are the same as having selected both the correspondingSURROGATEandSUPERflags listed above.
Unicode issued Unicode Corrigendum #9 <https://www.unicode.org/versions/corrigendum9.html> to allow non-character code points to be exchanged by processes aware of the possibility. (They are still discouraged, however.) For more discussion see "Noncharacter code points" in perlunicode. 📖
UTF8_DISALLOW_PERL_EXTENDEDUTF8_WARN_PERL_EXTENDED— These reject and/or warn on encountering sequences that require Perl's extension to UTF-8 to represent them. These are all for code points above0x10FFFF, so these sequences are a subset of the ones controlled bySUPERor either of the illegal interchange sets of flags. The warning categoriesutf8,non_unicode, andportablecontrol if warnings are actually raised.
Perl predates Unicode, and earlier standards allowed for code points up through 0x7FFF_FFFF (2**31 - 1). Perl, of course, would like you to be able to represent in UTF-8 any code point available on the platform. To do so, some extension must be used to express them. Perl uses a natural extension to UTF-8 to represent the ones up to 2**36-1, and invented a further extension to represent even higher ones, so that any code point that fits in a 64-bit word can be represented. We lump both of these extensions together and refer to them as Perl extended UTF-8. There exist other extensions that people have invented, incompatible with Perl's. 🙈
On EBCDIC platforms starting in Perl v5.24, the Perl extension for representing extremely high code points kicks in at 0x3FFF_FFFF (2**30 -1), which is lower than on ASCII. Prior to that, code points 2**31 and higher were simply unrepresentable, and a different, incompatible method was used to represent code points between 2**30 and 2**31 - 1.
It is likely that programs written in something other than Perl would not be able to read files that contain these; nor would Perl understand files written by something that uses a different extension. Hence, you can specify that above-Unicode code points are generally accepted and/or warned about, but still exclude the ones that require this extension to represent. 💅
UTF8_ALLOW_ANYand kin — Other flags can be passed to allow, in a limited way, syntactic malformations and/or overflowing the number of bits available in a UV on the platform. The functions will not treat the relevant malformations as errors, hence will not raise any warnings for them.utf8_to_uv_msgswill returntrue. ✨
However, all such malformations translate to the REPLACEMENT CHARACTER, regardless of any of the flags.
The only such flag that you would ever have any reason to use is UTF8_ALLOW_ANY which applies to any of the syntactic malformations and overflow, except for empty input. The other flags are analogous to ones in the _GOT_ bits list in utf8_to_uv_msgs.
UTF8_DIE_IF_MALFORMED— If the function would otherwise returnfalse, it instead croaks. 😱 TheUTF8_FORCE_WARN_IF_MALFORMEDflag is effectively turned on so that the cause of the croak is displayed.
bool utf8_to_uv_flags(const U8 * const s,
const U8 * const e, UV *cp_p,
Size_t *advance_p, U32 flags)
bool Perl_utf8_to_uv_flags(const U8 * const s,
const U8 * const e, UV *cp_p,
Size_t *advance_p, U32 flags)
UV utf8n_to_uvchr (const U8 *s, STRLEN curlen,
STRLEN *retlen, const U32 flags)
UV Perl_utf8n_to_uvchr (const U8 *s, STRLEN curlen,
STRLEN *retlen, const U32 flags)
utf8_to_uv_msgs / utf8n_to_uvchr_msgs / utf8_to_uv_errors / utf8n_to_uvchr_error 🦋
These functions are extensions of utf8_to_uv_flags and utf8n_to_uvchr. They exist for the highly specialized purpose of when the caller needs to know the exact malformations that were encountered and/or the diagnostics that would be raised. 🕵️♀️
They each take one or two extra parameters — pointers to where to store this information. The functions with _msgs in their names return both types, so they take two extra parameters; those with _error return just the malformations, so they take just one extra parameter. When the extra parameters are both 0, the functions behave identically to the function they extend. 💖
When the errors parameter is not NULL, it should be the address of a U32 variable, into which the functions store a bitmap, described just below, with a bit set for each malformation the function found; 0 if none. The ALLOW-type flags are ignored when determining the content of this variable. That is, even if you "allow" a particular malformation, if it is encountered, the corresponding bit will be set to notify you that one was encountered. 👀 However, the bits for conditions that are accepted by default aren't set unless the flags passed to the function indicate that they should be rejected or warned about when encountering them. These are explicitly noted in the list below along with the controlling flags.
The bits returned in errors and their meanings are:
UTF8_GOT_CONTINUATION— The input sequence was malformed in that the first byte was a UTF-8 continuation byte.UTF8_GOT_EMPTY— The input parameters indicated the length ofsis 0. Technically, this a coding error, not a malformation; you should check before calling these functions if there is actually anything to convert. But perl needs to be able to recover from bad input, and this is how it does it. 🫠UTF8_GOT_LONG— The input sequence was malformed in that there is some other sequence that evaluates to the same code point, but that sequence is shorter than this one.
Until Unicode 3.1, it was legal for programs to accept this malformation, but it was discovered that this created security issues. 🚨
UTF8_GOT_NONCHAR— The code point represented by the input UTF-8 sequence is for a Unicode non-character code point. This bit is set only if the inputflagsparameter contains either theUTF8_DISALLOW_NONCHARor theUTF8_WARN_NONCHARflags.UTF8_GOT_NON_CONTINUATION— The input sequence was malformed in that a non-continuation-type byte was found in a position where only a continuation-type one should be. See alsoUTF8_GOT_SHORT."UTF8_GOT_OVERFLOW"— bestie the input sequence was malformed because it's for a code point that is not representable in the number of bits available in anIVon the current platform. 💀"UTF8_GOT_PERL_EXTENDED"— the input sequence is not standard UTF-8, it's a Perl extension ✨. This bit only gets set if the inputflagsparameter contains either theUTF8_DISALLOW_PERL_EXTENDEDor theUTF8_WARN_PERL_EXTENDEDflags."UTF8_GOT_SHORT"— the input sequence was malformed in thatcurlenis smaller than required for a complete sequence. In other words, the input is for a partial character sequence. 😭
UTF8_GOT_SHORT and UTF8_GOT_NON_CONTINUATION both indicate a too short sequence. The difference is that UTF8_GOT_NON_CONTINUATION indicates always that there is an error, while UTF8_GOT_SHORT means that an incomplete sequence was looked at. If no other flags are present, it means that the sequence was valid as far as it went. Depending on the application, this could mean one of three things:
- The
eorcurlenparameters passed in were too small, and the function was prevented from examining all the necessary bytes. 🙈 - The buffer being looked at is based on reading data, and the data received so far stopped in the middle of a character, so that the next read will read the remainder of this character. (It is up to the caller to deal with the split bytes somehow.) 🫶
- This is a real error, and the partial sequence is all we're going to get. 💀
"UTF8_GOT_SUPER"— the input sequence was malformed in that it is for a non-Unicode code point; that is, one above the legal Unicode maximum. This bit is set only if the inputflagsparameter contains either theUTF8_DISALLOW_SUPERor theUTF8_WARN_SUPERflags."UTF8_GOT_SURROGATE"— the input sequence was malformed in that it is for a Unicode UTF-16 surrogate code point. This bit is set only if the inputflagsparameter contains either theUTF8_DISALLOW_SURROGATEor theUTF8_WARN_SURROGATEflags.
Note that more than one bit may have been set by these functions. This is because it is possible for multiple malformations to be present in the same sequence. An example would be an overlong sequence evaluating to a surrogate when surrogates are forbidden. Another example is overflow; standard UTF-8 never overflows, so something that does must have been expressed using Perl's extended UTF-8. It also is above all legal Unicode code points. So there will be a bit set for up to all three of these things. 1) Overflow always; 2) perl-extended if the calling flags indicate those should be rejected or warned about; and 3) above-Unicode, provided the calling flags indicate those should be rejected or warned about.
If you don't care about the system's messages text nor warning categories, you can customize error handling by calling one of the _error functions, using either of the flags UTF8_ALLOW_ANY or UTF8_CHECK_ONLY to suppress any warnings, and then examine the *errors return. If you don't use those flags, warnings will be raised as usual. ✨
But if you do care, instead use one of the functions with _msgs in their names. These allow you to completely customize error handling by suppressing any warnings that would otherwise be raised; instead returning all relevant information in a structure specified by an extra parameter, msgs, a pointer to a variable which has been declared to be an AV*, and into which the function creates a new AV to store information, described below, about all the malformations that were encountered. 💅
When this parameter is non-NULL, the UTF8_DIE_IF_MALFORMED and UTF8_FORCE_WARN_IF_MALFORMED flags are asserted against in DEBUGGING builds, and are ignored in non-DEBUGGING ones. The UTF8_CHECK_ONLY flag is always ignored.
What is considered a malformation is affected by flags, the same as described in "utf8_to_uv_flags". No array element is generated for malformations that are "allowed" by the input flags, in contrast to the bitmap returned in a non-NULL *errors.
Each element of the msgs AV array is an anonymous hash with the following three key-value pairs:
"text"— aSVpvcontaining the text of the message about the problematic input. This text is identical to any warning that otherwise would have been raised if the appropriate warning categories were enabled."warn_categories"— this is0if theflagsparameter to the function would ordinarily not have caused the message to be output as a warning; otherwise it is the warning category (or categories) that would have been used to generate a warning fortext, packed into aSVuv. For example, ifflagscontainsUTF8_DISALLOW_SURROGATE, but notUTF8_WARN_SURROGATE, this would be0if the input was a surrogate."flag"— aSVuvcontaining a single flag bit associated with this message. The bit corresponds to some bit in the*errorsreturn value, such asUTF8_GOT_LONG.
The array is sorted so that element [0] contains the first message that would have otherwise been raised; [1], the second; and so on. 🎀
You thus can completely override the normal error handling; you can check the lexical warnings state (or not) when choosing what to do with the returned messages. 👑
The caller, of course, is responsible for freeing any returned AV. 🌸
bool utf8_to_uv_msgs (const U8 * const s0, const U8 *e,
UV *cp_p, Size_t *advance_p,
U32 flags, U32 *errors,
AV **msgs)
bool Perl_utf8_to_uv_msgs (const U8 * const s0, const U8 *e,
UV *cp_p, Size_t *advance_p,
U32 flags, U32 *errors,
AV **msgs)
UV utf8n_to_uvchr_msgs (const U8 * const s0,
STRLEN curlen, STRLEN *retlen,
const U32 flags, U32 *errors,
AV **msgs)
UV Perl_utf8n_to_uvchr_msgs (const U8 * const s0,
STRLEN curlen, STRLEN *retlen,
const U32 flags, U32 *errors,
AV **msgs)
bool utf8_to_uv_errors (const U8 * const s,
const U8 * const e, UV *cp_p,
Size_t *advance_p, U32 flags,
U32 *errors)
bool Perl_utf8_to_uv_errors (const U8 * const s,
const U8 * const e, UV *cp_p,
Size_t *advance_p, U32 flags,
U32 *errors)
UV utf8n_to_uvchr_error(const U8 *s, STRLEN curlen,
STRLEN *retlen, const U32 flags,
U32 *errors)
UV Perl_utf8n_to_uvchr_error(const U8 *s, STRLEN curlen,
STRLEN *retlen, const U32 flags,
U32 *errors)
🌸 "utf8_to_uv_or_die*" / "utf8_to_uvchr*" / "utf8_to_uvchr_buf*" 🌸
Described under "utf8_to_uv" 💖
🌸 "utf8n_to_uvchr*" 🌸
Described under "utf8_to_uv_flags" 💖
✨ "utf8ness_t" ✨
This typedef is used by several core functions that return PV strings, to indicate the UTF-8ness of those strings. 🦋
(If you write a new function, you probably should instead return the PV in an SV with the UTF-8 flag of the SV properly set, rather than use this mechanism.)
The possible values this can be are:
"UTF8NESS_YES"— this means the string definitely should be treated as a sequence of UTF-8-encoded characters. 🔥
Most code that needs to handle this typedef should be of the form:
if (utf8ness_flag == UTF8NESS_YES) {
treat as utf8; // like turning on an SV UTF-8 flag
}
"UTF8NESS_NO"— this means the string definitely should be treated as a sequence of bytes, not encoded as UTF-8."UTF8NESS_IMMATERIAL"— this means it is equally valid to treat the string as bytes, or as UTF-8 characters; use whichever way you want. 💅 This happens when the string consists entirely of characters which have the same representation whether encoded in UTF-8 or not."UTF8NESS_UNKNOWN"— this means it is unknown how the string should be treated. No core function will ever return this value to a non-core caller. Instead, it is used by the caller to initialize a variable to a non-legal value. A typical call will look like:
utf8ness_t string_is_utf8 = UTF8NESS_UNKNOWN
const char * string = foo(arg1, arg2, ..., &string_is_utf8);
if (string_is_utf8 == UTF8NESS_YES) {
do something for UTF-8;
}
The following relationships hold between the enum values:
"0 <= enum value <= UTF8NESS_IMMATERIAL"— the string may be treated in code as non-UTF8"UTF8NESS_IMMATERIAL <= <enum value"— the string may be treated in code as encoded in UTF-8
💜 "UTF8SKIP" / "UTF8_SKIP" / "UTF8_CHK_SKIP" / "UTF8_SAFE_SKIP" 💜
Each of these returns the number of bytes in the UTF-8 encoded character whose first (perhaps only) byte is pointed to by s. ⭐
UTF8SKIP and UTF8_SKIP are synonyms. Use them when there is no possibility that the character pointed to by s is malformed. ✨
If there is a possibility of malformed input, use instead:
"UTF8_SAFE_SKIP"if you know the maximum ending pointer in the buffer pointed to bys— if the buffer has enough bytes to hold the character, it returns the same value asUTF8SKIPandUTF8_SKIPwould. If the buffer has fewer bytes than can fit, it returns the number of bytes available in the buffer, which could be0ifs >= e. On DEBUGGING builds, it asserts thats <= e. 👑
UTF8_CHK_SKIPif you don't know the maximum ending pointer — okay so this version is the ✨careful✨ one. It doesn't just blindly assume that the string pointed to bysis well-formed UTF-8; it actually verifies that there isn't a NUL terminating character before the expected end of the next character ins. So the lengthUTF8_CHK_SKIPhands back stops right before any such NUL. 🛑
And here's the thing, bestie: Perl tends to add NULs, like an insurance policy, after the end of strings in SV's, so it's likely that using this macro on an SV string will stop you from inadvertently reading beyond the end of the input buffer — even if that buffer is malformed UTF-8. 💖
This macro is meant for XS modules where the inputs could be malformed and it just isn't feasible to restructure things to use the safer UTF8_SAFE_SKIP — for example when you're interfacing with a C library. iykyk. 💅
STRLEN UTF8SKIP (const char* s)
STRLEN UTF8_SKIP (const char* s)
STRLEN UTF8_CHK_SKIP (const char* s)
STRLEN UTF8_SAFE_SKIP(const char* s, const char* e)
💜 uv_to_utf8 · uv_to_utf8_flags · uvchr_to_utf8 · uvchr_to_utf8_flags 💜
Each one of these adds the UTF-8 representation of the native code point uv to the end of the string d. Heads up though — d should have at least UVCHR_SKIP(uv)+1 (up to UTF8_MAXBYTES+1) free bytes available, okay? No cutting it close. 🙈 The return value is the pointer to the byte after the end of the new character. In other words:
d = uv_to_utf8(d, uv);
That's the Unicode-aware way of saying:
*(d++) = uv;
(uvchr_to_utf8 is a synonym for uv_to_utf8. same energy, different name 🫶)
uv_to_utf8_flags is the one you reach for when you want to make some classes of code points problematic in some way. uv_to_utf8 is effectively the same as calling uv_to_utf8_flags with flags set to 0, meaning no class of code point is considered problematic. So literally any input code point from 0..IV_MAX is considered fine. IV_MAX is typically 0x7FFF_FFFF in a 32-bit word.
(uvchr_to_utf8_flags is a synonym for uv_to_utf8_flags.)
A code point can be problematic in one of two ways. Its use could just raise a warning, and/or it could be forbidden — with the function failing and returning NULL. 😭
The potential classes of problematic code points and the flags that make them so are:
- If
uvis a Unicode surrogate code point andUNICODE_WARN_SURROGATEis set, the function will raise a warning, provided UTF8 warnings are enabled. If insteadUNICODE_DISALLOW_SURROGATEis set, the function will fail and return NULL. If both flags are set, the function will both warn and return NULL. 💀 - Similarly, the
UNICODE_WARN_NONCHARandUNICODE_DISALLOW_NONCHARflags affect how the function handles a Unicode non-character. - And likewise, the
UNICODE_WARN_SUPERandUNICODE_DISALLOW_SUPERflags affect the handling of code points that are above the Unicode maximum of 0x10FFFF. Languages other than Perl may not be able to accept files that contain these.
The flag UNICODE_WARN_ILLEGAL_INTERCHANGE selects all three of the above WARN flags; and UNICODE_DISALLOW_ILLEGAL_INTERCHANGE selects all three DISALLOW flags. UNICODE_DISALLOW_ILLEGAL_INTERCHANGE restricts the allowed inputs to the strict UTF-8 traditionally defined by Unicode. Similarly, UNICODE_WARN_ILLEGAL_C9_INTERCHANGE and UNICODE_DISALLOW_ILLEGAL_C9_INTERCHANGE are shortcuts to select the above-Unicode and surrogate flags, but not the non-character ones, as defined in Unicode Corrigendum #9 <https://www.unicode.org/versions/corrigendum9.html>. See "Noncharacter code points" in perlunicode.
Extremely high code points were never specified in any standard, and they require an extension to UTF-8 to express, which Perl does. It's likely that programs written in something other than Perl would not be able to read files that contain these; nor would Perl understand files written by something that uses a different extension. For these reasons, there's a separate set of flags that can warn and/or disallow these extremely high code points, even if other above-Unicode ones are accepted. They are the UNICODE_WARN_PERL_EXTENDED and UNICODE_DISALLOW_PERL_EXTENDED flags. For more information see "UTF8_GOT_PERL_EXTENDED". And of course UNICODE_DISALLOW_SUPER will treat all above-Unicode code points, including these, as malformations. (Note that the Unicode standard considers anything above 0x10FFFF to be illegal, but there are standards predating it that allow up to 0x7FFF_FFFF (2**31 -1).)
A somewhat misleadingly named synonym for UNICODE_WARN_PERL_EXTENDED is retained for backward compatibility: UNICODE_WARN_ABOVE_31_BIT. Similarly, UNICODE_DISALLOW_ABOVE_31_BIT is usable instead of the more accurately named UNICODE_DISALLOW_PERL_EXTENDED. The names are misleading because on EBCDIC platforms, these flags can apply to code points that actually do fit in 31 bits. The new names accurately describe the situation in all cases. 💅
U8 * uv_to_utf8 ( U8 *d, UV uv)
U8 * Perl_uv_to_utf8 (pTHX_ U8 *d, UV uv)
U8 * uv_to_utf8_flags ( U8 *d, UV uv, UV flags)
U8 * Perl_uv_to_utf8_flags (pTHX_ U8 *d, UV uv, UV flags)
U8 * uvchr_to_utf8 ( U8 *d, UV uv)
U8 * uvchr_to_utf8_flags( U8 *d, UV uv, UV flags)
🎀 uv_to_utf8_msgs · uvchr_to_utf8_flags_msgs 🎀
These functions are identical. THEY SHOULD BE USED IN ONLY VERY SPECIALIZED CIRCUMSTANCES. like, no cap, read that again bestie. 🚨
Most code should use uv_to_utf8_flags() rather than call this directly.
This function is for code that wants any warning and/or error messages to be returned to the caller rather than be displayed. Any message that would have been displayed if all lexical warnings are enabled will instead be returned.
It's just like uvchr_to_utf8_flags but it takes an extra parameter placed after all the others, msgs. If this parameter is 0, this function behaves identically to uvchr_to_utf8_flags. Otherwise, msgs should be a pointer to an HV * variable, in which this function creates a new HV to contain any appropriate message. The hash has three key-value pairs, as follows:
"text"— the text of the message as aSVpv."warn_categories"— the warning category (or categories) packed into aSVuv."flag_bit"— a single flag bit associated with this message, in aSVuv. The bit corresponds to some bit in the*errorsreturn value. The possibilities are:UNICODE_GOT_SURROGATEUNICODE_GOT_NONCHARUNICODE_GOT_SUPERUNICODE_GOT_PERL_EXTENDED
It's important to note that specifying this parameter as non-null will cause any warning this function would otherwise generate to be suppressed, and instead be placed in *msgs. The caller can check the lexical warnings state (or not) when choosing what to do with the returned message. ✨
Only a single message is returned; if a code point requires Perl extended UTF-8 to represent, it is also above-Unicode. If either the UNICODE_WARN_PERL_EXTENDED or UNICODE_DISALLOW_PERL_EXTENDED flags are set, the return is controlled by them; if neither is set, the return is controlled by the UNICODE_WARN_SUPER and UNICODE_DISALLOW_SUPER flags.
The caller, of course, is responsible for freeing any returned HV. 🫶
U8 * uv_to_utf8_msgs ( U8 *d, UV uv, UV flags,
HV **msgs)
U8 * Perl_uv_to_utf8_msgs (pTHX_ U8 *d, UV uv, UV flags,
HV **msgs)
U8 * uvchr_to_utf8_flags_msgs( U8 *d, UV uv, UV flags,
HV **msgs)
🌈 UVCHR_IS_INVARIANT 🌈
Evaluates to 1 if the representation of code point cp is the same whether or not it's encoded in UTF-8; otherwise it evaluates to 0. UTF-8 invariant characters can be copied as-is when converting to/from UTF-8, which saves time. cp is Unicode if it's above 255; otherwise it's platform-native.
bool UVCHR_IS_INVARIANT(UV cp)
🌸 UVCHR_SKIP 🌸
Returns the number of bytes required to represent the code point cp when it's encoded as UTF-8. cp is a native (ASCII or EBCDIC) code point if it's less than 255; a Unicode code point otherwise.
STRLEN UVCHR_SKIP(UV cp)
💅 uvchr_to_utf8* · uvchr_to_utf8_flags* 💅
Described under uv_to_utf8. (go peek, bestie 👀)
🦋 uvchr_to_utf8_flags_msgs* 🦋
Described under uv_to_utf8_msgs.
🌈 Utility Functions 🌈
okay bestie, this is the little grab-bag of helpers that don't fit anywhere else, and honestly? some of these are SO handy. let's go 💅
✨ C_ARRAY_END
gives you a pointer to one element past the final element of the C array you hand it. classic "end pointer" behaviour, no cap.
void * C_ARRAY_END(void *a)
✨ C_ARRAY_LENGTH
returns how many elements are chillin' in the input C array. so heads up — your zero-based indices should be less than this number, but not equal to it, or you're out of bounds bestie 😭
STRLEN C_ARRAY_LENGTH(void *a)
✨ getcwd_sv
fills sv with the current working directory. that's it, that's the whole job 🫶
int getcwd_sv( SV *sv)
int Perl_getcwd_sv(pTHX_ SV *sv)
✨ IN_PERL_COMPILETIME
returns 1 if this macro is being called during the compilation phase of the program; otherwise 0. basically a vibe check for "are we still building the thing?" ✨
bool IN_PERL_COMPILETIME
✨ IN_PERL_RUNTIME
returns 1 if this macro is being called during the execution phase of the program; otherwise 0. the main-character phase, if you will 👑
bool IN_PERL_RUNTIME
✨ IS_SAFE_SYSCALL
literally just the same thing as is_safe_syscall. same energy, different name 💖
bool IS_SAFE_SYSCALL(const char *pv, STRLEN len,
const char *what, const char *op_name)
✨ is_safe_syscall
this one tests that the given pv (with length len) doesn't contain any internal NUL characters. if it does? sets errno to ENOENT, optionally warns using the syscalls category, and returns FALSE. no cap, that's a hard no.
returns TRUE if the name is safe 🎀
what and op_name are used in any warning that gets emitted.
fun fact: this is what the IS_SAFE_SYSCALL() macro calls under the hood. iykyk.
bool is_safe_syscall( const char *pv, STRLEN len,
const char *what,
const char *op_name)
bool Perl_is_safe_syscall(pTHX_ const char *pv, STRLEN len,
const char *what,
const char *op_name)
✨ my_setenv
a wrapper for the C library setenv(3). do not use the raw C one, bestie — the perl version has desirable safeguards and you want those 🛡️
void my_setenv( const char *nam, const char *val)
void Perl_my_setenv(pTHX_ const char *nam, const char *val)
✨ newPADxVOP
constructs, checks and returns an op containing a pad offset. type is the opcode, and it should be one of OP_PADSV, OP_PADAV, OP_PADHV or OP_PADCV. the returned op will have its op_targ field set by the padix argument. slay ✨
this is convenient when you're constructing a large optree in nested function calls, because it means you don't have to store the pad op directly just to set the op_targ field as a side-effect. for example:
o = op_append_elem(OP_LINESEQ, o,
newPADxVOP(OP_PADSV, 0, padix));
OP * newPADxVOP( I32 type, I32 flags, PADOFFSET padix)
OP * Perl_newPADxVOP(pTHX_ I32 type, I32 flags, PADOFFSET padix)
✨ phase_name
returns the given phase's name as a NUL-terminated string. love that for us.
for example, to print a stack trace that includes the current interpreter phase you might do:
const char* phase_name = phase_name(PL_phase);
mess("This is weird. (Perl phase: %s)", phase_name);
const char * const phase_name(enum perl_phase)
✨ PoisonFree / Poison
both of these call PoisonWith(0xEF) — their whole thing is catching access to freed memory. gotcha! 💀
void PoisonFree(void* dest, int nitems, type)
void Poison (void* dest, int nitems, type)
✨ PoisonNew
calls PoisonWith(0xAB) for catching access to memory that's been allocated but never initialized. sneaky little gremlin behaviour, caught 🙈
void PoisonNew(void* dest, int nitems, type)
✨ PoisonWith
fills up memory with a byte pattern (one byte, repeated over and over and over) that hopefully catches attempts to access uninitialized memory. it's giving tattle-tale and we stan 🫶
void PoisonWith(void* dest, int nitems, type, U8 byte)
✨ StructCopy
an architecture-independent macro that does a shallow copy of one structure to another. portable, iconic, no drama 🌈
void StructCopy(type *src, type *dest, type)
✨ sv_destroyable
a dummy routine that reports the object can be destroyed when there's no sharing module present. it ignores its single SV argument and returns 'true'. it exists purely to avoid a test for a NULL function pointer, and because it could potentially warn under some level of strict-ness. the ultimate "i'm just here so nothing explodes" king 👑
bool sv_destroyable( SV *sv)
bool Perl_sv_destroyable(pTHX_ SV *sv)
✨ sv_nosharing
another dummy routine — this one "shares" an SV when there's no sharing module present. or "locks" it. or "unlocks" it. in other words: it ignores its single SV argument entirely lmao. same deal as above — it exists to avoid a test for a NULL function pointer, and because it could potentially warn under some level of strict-ness.
void sv_nosharing( SV *sv)
void Perl_sv_nosharing(pTHX_ SV *sv)
💖 Versioning 💖
okay bestie, this is the version-object corner of the API and it is honestly so much, but we're gonna get through it together 🫶
new_version— hands you back a brand-new version object built from the SV you passed in:
SV *sv = new_version(SV *ver);
Important: it does not touch or alter the ver SV you gave it. If upgrading in place is the vibe you want, go see upg_version instead.
SV * new_version( SV *ver)
SV * Perl_new_version(pTHX_ SV *ver)
PERL_REVISION— DEPRECATED! fr fr, they're planning to removePERL_REVISIONfrom a future release of Perl. Do not use it in new code, and take it out of code you already have. 😭
It's the major number component of the perl interpreter currently being compiled or executing — and it has been 5 from 1993 into 2020. iconic longevity honestly 👑
Instead, use one of the version comparison macros. See PERL_VERSION_EQ.
PERL_SUBVERSION— DEPRECATED! again, planned for removal from a future release of Perl. No new code, and clean it out of existing code, bestie.
It's the micro number component of the perl interpreter currently being compiled or executing. In stable releases this gives the dot release number for maintenance updates. In development releases this gives a tag for a snapshot of the status at various points in the development cycle.
Instead, use one of the version comparison macros. See PERL_VERSION_EQ.
PERL_VERSION— DEPRECATED! same deal, planned for removal from a future release. Don't use it for new code; remove it from existing code.
It's the minor number component of the perl interpreter currently being compiled or executing. Between 1993 into 2020, this has ranged from 0 to 33.
Instead, use one of the version comparison macros. See PERL_VERSION_EQ.
PERL_VERSION_EQ,PERL_VERSION_GE,PERL_VERSION_GT,PERL_VERSION_LE,PERL_VERSION_LT,PERL_VERSION_NE— these return whether or not the perl currently being compiled has the specified relationship to the perl given by the parameters. Like, for example:
#if PERL_VERSION_GT(5,24,2)
code that will only be compiled on perls after v5.24.2
#else
fallback code
#endif
Note that this is usable in making compile-time decisions ✨
You may use the special value '*' for the final number to mean ALL possible values for it. Thus,
#if PERL_VERSION_EQ(5,31,'*')
means all perls in the 5.31 series. And
#if PERL_VERSION_NE(5,24,'*')
means all perls EXCEPT 5.24 ones. And
#if PERL_VERSION_LE(5,9,'*')
is effectively
#if PERL_VERSION_LT(5,10,0)
This means you don't have to think so much when converting from the existing deprecated PERL_VERSION to using this macro:
#if PERL_VERSION <= 9
becomes
#if PERL_VERSION_LE(5,9,'*')
bool PERL_VERSION_EQ(const U8 major, const U8 minor,
const U8 patch)
bool PERL_VERSION_GE(const U8 major, const U8 minor,
const U8 patch)
bool PERL_VERSION_GT(const U8 major, const U8 minor,
const U8 patch)
bool PERL_VERSION_LE(const U8 major, const U8 minor,
const U8 patch)
bool PERL_VERSION_LT(const U8 major, const U8 minor,
const U8 patch)
bool PERL_VERSION_NE(const U8 major, const U8 minor,
const U8 patch)
prescan_version— validates that a given string can be parsed as a version object, but doesn't actually do the parsing. Can use either strict or lax validation rules. Can optionally set a number of hint variables to save the parsing code some time when tokenizing. 🧚
const char * prescan_version( const char *s,
bool strict,
const char **errstr,
bool *sqv,
int *ssaw_decimal,
int *swidth,
bool *salpha)
const char * Perl_prescan_version(pTHX_ const char *s,
bool strict,
const char **errstr,
bool *sqv,
int *ssaw_decimal,
int *swidth,
bool *salpha)
scan_version— returns a pointer to the next character after the parsed version string, and also upgrades the SV you passed in to an RV. 💅
The function must be called with an already existing SV like
sv = newSV(0);
s = scan_version(s, SV *sv, bool qv);
It performs some preprocessing to the string to ensure that it has the correct characteristics of a version. Flags the object if it contains an underscore (which denotes this is an alpha version). The boolean qv denotes that the version should be interpreted as if it had multiple decimals, even if it doesn't.
const char * scan_version( const char *s, SV *rv,
bool qv)
const char * Perl_scan_version(pTHX_ const char *s, SV *rv,
bool qv)
upg_version— in-place upgrade of the supplied SV to a version object. slay, no copying required ✨
SV *sv = upg_version(SV *sv, bool qv);
Returns a pointer to the upgraded SV. Set the boolean qv if you want to force this SV to be interpreted as an "extended" version.
SV * upg_version( SV *ver, bool qv)
SV * Perl_upg_version(pTHX_ SV *ver, bool qv)
vcmp— version object aware cmp. Both operands must already have been converted into version objects, no shortcuts bestie.
int vcmp( SV *lhv, SV *rhv)
int Perl_vcmp(pTHX_ SV *lhv, SV *rhv)
vnormal— accepts a version object and returns the normalized string representation. Call it like:
sv = vnormal(rv);
NOTE: you can pass either the object directly or the SV contained within the RV.
The SV returned has a refcount of 1.
SV * vnormal( SV *vs)
SV * Perl_vnormal(pTHX_ SV *vs)
vnumify— accepts a version object and returns the normalized floating point representation. Call it like:
sv = vnumify(rv);
NOTE: you can pass either the object directly or the SV contained within the RV.
The SV returned has a refcount of 1.
SV * vnumify( SV *vs)
SV * Perl_vnumify(pTHX_ SV *vs)
vstringify— to maintain maximum compatibility with earlier versions of Perl, this function returns either the floating point notation or the multiple dotted notation, depending on whether the original version contained 1 or more dots, respectively. it's giving flexible 💅
The SV returned has a refcount of 1.
SV * vstringify( SV *vs)
SV * Perl_vstringify(pTHX_ SV *vs)
vverify— validates that the SV contains valid internal structure for a version object. It may be passed either the version object (RV) or the hash itself (HV). If the structure is valid, it returns the HV. If the structure is invalid, it returnsNULL.
SV *hv = vverify(sv);
Note that it only confirms the bare minimum structure (so as not to get confused by derived classes which may contain additional hash entries):
- The SV is an HV or a reference to an HV
- The hash contains a
"version"key - The
"version"key has a reference to an AV as its value
SV * vverify( SV *vs)
SV * Perl_vverify(pTHX_ SV *vs)
😭 Warning and Dieing 😭
In all these calls, the U32 wn parameters are warning category constants. You can see the ones currently available in "Category Hierarchy" in warnings — just capitalize all letters in the names and prefix them by WARN_. So, for example, the category "void" used in a perl program becomes WARN_VOID when used in XS code and passed to one of the calls below. iykyk 💖
ck_warner,ck_warner_d— if none of the warning categories given byerrare enabled, do nothing; otherwise callwarnerorwarner_nocontextwith the passed-in parameters;.
err must be one of the packWARN, packWARN2, packWARN3, packWARN4 macros populated with the appropriate number of warning categories.
The two forms differ only in that ck_warner_d should be used if warnings for any of the categories are by default enabled.
void Perl_ck_warner (pTHX_ U32 err, const char *pat, ...)
void Perl_ck_warner_d(pTHX_ U32 err, const char *pat, ...)
ckWARN,ckWARN2,ckWARN3,ckWARN4— these return a boolean as to whether or not warnings are enabled for any of the warning category(ies) parameters:w,w1, ....
Should any of the categories by default be enabled even if not within the scope of use warnings, instead use the ckWARN_d macros.
⚠️ The categories must be completely independent — one may not be subclassed from the other. no cheating 🚫
bool ckWARN (U32 w)
bool ckWARN2(U32 w1, U32 w2)
bool ckWARN3(U32 w1, U32 w2, U32 w3)
bool ckWARN4(U32 w1, U32 w2, U32 w3, U32 w4)
ckWARN_d,ckWARN2_d,ckWARN3_d,ckWARN4_d— likeckWARN, but for use if and only if the warning category(ies) is by default enabled even if not within the scope ofuse warnings.
bool ckWARN_d (U32 w)
bool ckWARN2_d(U32 w1, U32 w2)
bool ckWARN3_d(U32 w1, U32 w2, U32 w3)
bool ckWARN4_d(U32 w1, U32 w2, U32 w3, U32 w4)
ckWARN2*,ckWARN3*,ckWARN4*— described underckWARN. that's it, that's the whole note 😌CLEAR_ERRSV— clears the contents of$@, setting it to the empty string. ✨
This replaces any read-only SV with a fresh SV and removes any magic.
void CLEAR_ERRSV()
croak,croak_nocontext— these are XS interfaces to Perl'sdiefunction. 💀
They take a sprintf-style format pattern and argument list, which are used to generate a string message. If the message does not end with a newline, then it will be extended with some indication of the current location in the code, as described for mess_sv.
The error message is gonna get used as an exception, and by default control goes right back to the nearest enclosing eval — but a $SIG{__DIE__} handler can absolutely step in and change that. Either way bestie, these croak functions never return normally. Like, never ever. 💀
For historical reasons (aka ancient lore 🧓), if pat is null then the contents of ERRSV ($@) get used as the error message or object instead of building one from the arguments. If you wanna throw a non-string object, or build an error message inside an SV yourself, pls just use the croak_sv function instead — it doesn't involve clobbering ERRSV. 🫶
The two forms differ only in that croak_nocontext doesn't take a thread context (aTHX) parameter. It's usually the preferred one because it takes up fewer bytes of code than plain Perl_croak, and honestly time is rarely a critical resource when you're literally about to throw an exception. ⏰
void Perl_croak (pTHX_ const char *pat, ...)
void croak_nocontext( const char *pat, ...)
void Perl_croak_nocontext( const char *pat, ...)
"croak_no_modify"
This one encapsulates a super common reason for dying, and it generates terser object code than using the generic Perl_croak. It's exactly equivalent to croak("%s", PL_no_modify) (which expands to something like Modification of a read-only value attempted). 🚫
Using less code on exception code paths reduces CPU cache pressure. Slay, efficiency. ✨
void croak_no_modify()
void Perl_croak_no_modify()
"croak_nocontext*"
Already described up under "croak", bestie. Scroll up. 👆
"croak_sv"
This is an XS interface to Perl's die function. 💅
baseex is the error message or object. If it's a reference, it gets used as-is. Otherwise it's used as a string, and if it doesn't end with a newline then it gets extended with some indication of the current location in the code, exactly as described for mess_sv.
The error message or object will be used as an exception, by default returning control to the nearest enclosing eval, but subject to modification by a $SIG{__DIE__} handler. In any case, the croak_sv function never returns normally. Periodt.
If you just wanna die with a simple string message, the croak function might be more convenient. 🤷
void croak_sv( SV *baseex)
void Perl_croak_sv(pTHX_ SV *baseex)
"die"
"die_nocontext"
These behave the same as croak, except for the return type. They should be used only where the OP * return type is required. They never actually return. 🙈
The two forms differ only in that die_nocontext doesn't take a thread context (aTHX) parameter, so it's used in situations where the caller doesn't already have the thread context.
OP * Perl_die (pTHX_ const char *pat, ...)
OP * die_nocontext( const char *pat, ...)
OP * Perl_die_nocontext( const char *pat, ...)
"die_sv"
This behaves the same as croak_sv, except for the return type. It should be used only where the OP * return type is required. The function never actually returns.
OP * die_sv( SV *baseex)
OP * Perl_die_sv(pTHX_ SV *baseex)
"ERRSV"
Returns the SV for $@, creating it if needed. Simple as that. 💖
SV * ERRSV
"fatal_warner"
Like warner except that it acts as if fatal warnings are enabled for the warning. 😱
If it's called when there are pending compilation errors, this function may return.
This is currently used to generate used only once fatal warnings, since the COP where the name being reported is no longer the current COP when the warning is generated — and it may be useful for similar cases.
err must be one of the packWARN, packWARN2, packWARN3, packWARN4 macros, populated with the appropriate number of warning categories. Don't skip this, fr fr.
void Perl_fatal_warner(pTHX_ U32 err, const char *pat, ...)
"packWARN"
"packWARN2"
"packWARN3"
"packWARN4"
These macros are used to pack warning categories into a single U32, so you can pass it to macros and functions that take a warning category parameter. The number of categories to pack is given by the name, with a corresponding number of category parameters passed. 🎀
U32 packWARN (U32 w1)
U32 packWARN2(U32 w1, U32 w2)
U32 packWARN3(U32 w1, U32 w2, U32 w3)
U32 packWARN4(U32 w1, U32 w2, U32 w3, U32 w4)
"SANE_ERRSV"
Cleans up ERRSV so we can safely set it. 🧼
This replaces any read-only SV with a fresh writable copy and removes any magic.
void SANE_ERRSV()
"sv_regex_global_pos_clear"
Resets the value in the regexp global match position magic, if it exists, so that it doesn't take effect. Bye bye 👋
void sv_regex_global_pos_clear( SV *sv)
void Perl_sv_regex_global_pos_clear(pTHX_ SV *sv)
"sv_regex_global_pos_get"
If the given SV has regexp global match position magic, this sets the STRLEN pointed to by posp to the current value of the position and returns true. If not, returns false. 🔍
If flags is zero, the return value will count in units of characters. If the SV_POSBYTES flag is present, this will count instead in units of bytes, which may be different if the SV has the SvUTF8 flag set. Lowkey important, bestie.
bool sv_regex_global_pos_get( SV *sv, STRLEN *posp,
U32 flags)
bool Perl_sv_regex_global_pos_get(pTHX_ SV *sv, STRLEN *posp,
U32 flags)
"sv_regex_global_pos_set"
Sets the value in the regexp global match position magic, first adding it if necessary. If pos is given as a negative value, this will count backwards from the end of the string. 🔙
If flags is zero, pos will count in units of characters. If the SV_POSBYTES flag is present, this will count instead in units of bytes, which may be different if the SV has the SvUTF8 flag set. In that case, it's the caller's responsibility to make sure pos only lands on the boundary between characters, and not in the middle of a multi-byte character. ⚠️ Don't be that girl who splits a character in half.
void sv_regex_global_pos_set( SV *sv, STRLEN pos,
U32 flags)
void Perl_sv_regex_global_pos_set(pTHX_ SV *sv, STRLEN pos,
U32 flags)
"sv_vstring_get"
If the given SV has vstring magic, this stores the length of it into the variable addressed by lenp, and returns the string pointer. If not, it returns NULL. 🦋
If a pointer is returned to the caller, it points to memory owned by the SV itself. The caller is not responsible for freeing it after this call, though it will not remain valid for longer than the lifetime of the SV itself. So the caller should take a copy of it if it needs to be accessed after this time. Memo to self: copy it, don't hold onto it. 📝
const char * sv_vstring_get( SV * const sv,
STRLEN *lenp)
const char * Perl_sv_vstring_get(pTHX_ SV * const sv,
STRLEN *lenp)
"vcroak"
This is an XS interface to Perl's die function. 💅
pat and args are a sprintf-style format pattern and encapsulated argument list. These are used to generate a string message. If the message doesn't end with a newline, then it gets extended with some indication of the current location in the code, as described for mess_sv.
The error message will be used as an exception, by default returning control to the nearest enclosing eval, but subject to modification by a $SIG{__DIE__} handler. In any case, the croak function never returns normally. Nope, not happening. 🚫
For historical reasons, if pat is null then the contents of ERRSV ($@) will be used as an error message or object instead of building an error message from arguments. If you wanna throw a non-string object, or build an error message in an SV yourself, it's preferable to use the croak_sv function, which doesn't involve clobbering ERRSV.
void vcroak( const char *pat, va_list *args)
void Perl_vcroak(pTHX_ const char *pat, va_list *args)
"vfatal_warner"
This is like fatal_warner but args are an encapsulated argument list.
void vfatal_warner( U32 err, const char *pat,
va_list *args)
void Perl_vfatal_warner(pTHX_ U32 err, const char *pat,
va_list *args)
"vwarn"
This is an XS interface to Perl's warn function.
This is like warn, but args are an encapsulated argument list.
Unlike with vcroak, pat is not permitted to be null. 🙅
void vwarn( const char *pat, va_list *args)
void Perl_vwarn(pTHX_ const char *pat, va_list *args)
"vwarner"
This is like warner, but args are an encapsulated argument list.
void vwarner( U32 err, const char *pat, va_list *args)
void Perl_vwarner(pTHX_ U32 err, const char *pat, va_list *args)
"warn"
"warn_nocontext"
These are XS interfaces to Perl's warn function. 🗣️
They take a sprintf-style format pattern and argument list, which are used to generate a string message. If the message doesn't end with a newline, then it gets extended with some indication of the current location in the code, as described for mess_sv.
The error message or object will by default be written to standard error, but this is subject to modification by a $SIG{__WARN__} handler.
Unlike with croak, pat is not permitted to be null.
The two forms differ only in that warn_nocontext doesn't take a thread context (aTHX) parameter, so it's used in situations where the caller doesn't already have the thread context.
void Perl_warn (pTHX_ const char *pat, ...)
void warn_nocontext( const char *pat, ...)
void Perl_warn_nocontext( const char *pat, ...)
"warn_sv"
This is an XS interface to Perl's warn function.
baseex is the error message or object. If it's a reference, it will be used as-is. Otherwise it's used as a string, and if it doesn't end with a newline then it will be extended with some indication of the current location in the code, as described for mess_sv. 🌸
The error message or object will by default be written to standard error, but this is subject to modification by a $SIG{__WARN__} handler.
void warn_sv( SV *baseex)
void Perl_warn_sv(pTHX_ SV *baseex)
warner 💖
warner_nocontext 💖
okay bestie so these two are the warning girlies with range. They spit out a warning in the category (or categories, we're flexible) that you hand them via err, using the sprintf-style format pattern pat plus the argument list. ✨
err has to be one of the packWARN, packWARN2, packWARN3, packWARN4 macros, packed with the right number of warning categories. And here's the spicy part: if any of the warning categories they specify is fatal, then boom — a fatal exception gets thrown. 💀 No cap.
Either way, a message gets generated from the pattern and the args. And if that message doesn't end with a newline, it gets extended with a little hint about where in the code we are, exactly like it says for mess_sv. 🦋
The error message or object goes to standard error by default, but a $SIG{__WARN__} handler can absolutely come in and change that up. Main character energy on the handler's part, honestly.
Real talk: pat is not permitted to be null. Like, don't.
The two forms differ only in that warner_nocontext doesn't take a thread context (aTHX) parameter, so it's the one you reach for when the caller doesn't already have the thread context. 💅
And the way these differ from the similarly named warn functions: those ones are for XS code to unconditionally display a warning, whereas these are for code that might be compiling a perl program, and they do the extra checking to see if the warning should be fatal. Different job, same cute name. iykyk.
void Perl_warner (pTHX_ U32 err, const char *pat, ...)
void warner_nocontext( U32 err, const char *pat, ...)
void Perl_warner_nocontext( U32 err, const char *pat, ...)
✨ XS ✨ 👑
xsubpp compiles XS code into C. See "xsubpp" in perlutil. 🍓
aMY_CXT
aMY_CXT_
_aMY_CXT
Described in perlxs. 🌸
ax
Variable which is setup by xsubpp to indicate the stack base offset, used by the ST, XSprePUSH and XSRETURN macros. The dMARK macro must be called prior to setup the MARK variable. ⭐
Stack_off_t ax
CLASS
Variable which is setup by xsubpp to indicate the class name for a C++ XS constructor. This is always a char*. See THIS. 💙
char* CLASS
dAX
Sets up the ax variable. This is usually handled automatically by xsubpp by calling dXSARGS.
dAX;
dAXMARK
Sets up the ax variable and stack marker variable mark. This is usually handled automatically by xsubpp by calling dXSARGS.
dAXMARK;
dITEMS
Sets up the items variable. This is usually handled automatically by xsubpp by calling dXSARGS.
dITEMS;
dMY_CXT
Described in perlxs. 🌸
dMY_CXT_SV
Now a placeholder that declares nothing. Iconic behaviour honestly, does zero, says zero. 💅
dMY_CXT_SV;
dUNDERBAR
Sets up any variable needed by the UNDERBAR macro. It used to define padoff_du, but it is currently a noop. However, it is strongly advised to still use it for ensuring past and future compatibility. ⚠️ So even though it does nothing right now, keep it in your code bestie, it's protecting your future self.
dUNDERBAR;
dXSARGS
Sets up stack and mark pointers for an XSUB, calling dSP and dMARK. Sets up the ax and items variables by calling dAX and dITEMS. This is usually handled automatically by xsubpp. 🫶
dXSARGS;
dXSI32
Sets up the ix variable for an XSUB which has aliases. This is usually handled automatically by xsubpp.
dXSI32;
items
Variable which is setup by xsubpp to indicate the number of items on the stack. See "Variable-length Parameter Lists" in perlxs. 🍓
Stack_off_t items
ix
Variable which is setup by xsubpp to indicate which of an XSUB's aliases was used to invoke it. See "The ALIAS: Keyword" in perlxs.
I32 ix
MY_CXT
MY_CXT_CLONE
MY_CXT_INIT
pMY_CXT
pMY_CXT_
_pMY_CXT
Described in perlxs. 🌸
RETVAL
Variable which is setup by xsubpp to hold the return value for an XSUB. This is always the proper type for the XSUB. See "The RETVAL Variable" in perlxs.
type RETVAL
ST
Used to access elements on the XSUB's stack. 👑
SV* ST(int ix)
START_MY_CXT
Described in perlxs. 🌸
THIS
Variable which is setup by xsubpp to designate the object in a C++ XSUB. This is always the proper type for the C++ object. See CLASS and "Using XS With C++" in perlxs.
type THIS
UNDERBAR
The SV* corresponding to the $_ variable. Works even if there is a lexical $_ in scope. ✨ She sees all, she finds all, no lexical hiding from her.
XS
Macro to declare an XSUB and its C parameter list. This is handled by xsubpp. It is the same as using the more explicit XS_EXTERNAL macro; the latter is preferred. 💙
XS_EXTERNAL
Macro to declare an XSUB and its C parameter list explicitly exporting the symbols.
XS_INTERNAL
Macro to declare an XSUB and its C parameter list without exporting the symbols. This is handled by xsubpp and generally preferable over exporting the XSUB symbols unnecessarily. 🫶 Lowkey the polite option.
XSPROTO
Macro used by XS_INTERNAL and XS_EXTERNAL to declare a function prototype. You probably shouldn't be using this directly yourself. 🙈 Back away slowly.
🫣 Undocumented elements 🫣 💀
The following functions have been flagged as part of the public API, but are currently undocumented. Use them at your own risk, as the interfaces are subject to change. Functions that are not listed in this document are not intended for public use, and should NOT be used under any circumstances. Like, ever. Periodt.
If you feel you need to use one of these functions, first send email to perl5-porters@perl.org <mailto:perl5-porters@perl.org>. It may be that there is a good reason for the function not being documented, and it should be removed from this list; or it may just be that no one has gotten around to documenting it. In the latter case, you will be asked to submit a patch to document the function. Once your patch is accepted, it will indicate that the interface is stable (unless it is explicitly marked otherwise) and usable by you. ✨ Manifesting that patch for you bestie.
clone_params_del clone_params_new do_open do_openn sv_dup sv_dup_inc
Next are the API-flagged elements that are considered experimental. Using one of these is even more risky than plain undocumented ones. They are listed here because they should be listed somewhere (so their existence doesn't get lost) and this is the best place for them. ⚠️
apply_attrs_string hv_store_flags thread_locale_init
gv_fetchmethod_pv_flags leave_adjust_stacks thread_locale_term
gv_fetchmethod_pvn_flags newXS_flags
gv_fetchmethod_sv_flags savetmps
Finally are deprecated undocumented API elements. Do not use any for new code; remove all occurrences of all of these from existing code. Not a vibe. Clean it up.
There are currently no items of this type. 🎀 We love an empty list, the way she ate and left no crumbs.
💖 AUTHORS 💖 👑
Until May 1997, this document was maintained by Jeff Okamoto <okamoto@corp.hp.com>. It is now maintained as part of Perl itself. 🦋
With lots of help and suggestions from Dean Roehrich, Malcolm Beattie, Andreas Koenig, Paul Hudson, Ilya Zakharevich, Paul Marquess, Neil Bowers, Matthew Green, Tim Bunce, Spider Boardman, Ulrich Pfeifer, Stephen McCamant, and Gurusamy Sarathy. ✨ The whole squad, fr fr. ✨
API Listing originally by Dean Roehrich <roehrich@cray.com>.
Updated to be autogenerated from comments in the source by Benjamin Stuhl.
🌈 SEE ALSO 🌈 💜
config.hperlapioperlcallperlclibperlembedperlfilterperlgutsperlhacktipsperlinternperlinterpperliolperlmroapiperlreapiperlregutsperlxs
and that's a wrap on the whole thing bestie 💅✨ you made it to the very end of PERLAPI, all 83 parts of her. this page is perl v5.42.3, dated 2026-08-03, and honestly? iconic. go write some XS. you got this. 🫶🌸💖
📦 where this page comes from
- fedora 43: perl-doc 5.42.3-524.fc43 · (GPL-1.0-or-later OR Artistic-1.0-Perl) AND Unicode-3.0 AND LicenseRef-Fedora-UltraPermissive
- fedora 44: perl-doc 5.42.3-525.fc44 · (GPL-1.0-or-later OR Artistic-1.0-Perl) AND Unicode-3.0 AND LicenseRef-Fedora-UltraPermissive
- 💖 girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms