builtin(3perl)

๐Ÿ’– NAME ๐Ÿ’–

builtin(3perl) is the Perl pragma to import built-in utility functions. Periodt. โœจ

๐Ÿ“œ SYNOPSIS ๐Ÿ“œ

use builtin qw(
    true false is_bool
    inf nan
    weaken unweaken is_weak
    blessed refaddr reftype
    created_as_string created_as_number
    stringify
    ceil floor
    indexed
    trim
    is_tainted
    export_lexically
    load_module
);

use builtin ':5.40';  # most of the above

๐ŸŒธ DESCRIPTION ๐ŸŒธ

okay so Perl provides several utility functions in the builtin package. These are plain functions, bestie โ€” they look and behave just like regular user-defined functions do. They do not provide new syntax or require special parsing. These functions are always present in the interpreter and can be called at any time by their fully-qualified names. By default they are not available as short names, but can be requested for convenience. ๐Ÿ’…

Individual named functions can be imported by listing them as import parameters on the use statement for this pragma.

The builtin::compat module from CPAN provides versions of many of these functions that can be used on Perl versions where builtin or specific functions are not yet available. ๐Ÿซถ

โš ๏ธ Warning: At present, many of the functions in the builtin namespace are experimental. Calling them will trigger warnings of the experimental::builtin category. You've been warned. ๐Ÿ’€

โœจ Lexical Import โœจ

This pragma module creates lexical aliases in the currently-compiling scope to these builtin functions. This is similar to the lexical effect of other pragmas such as strict and feature. It's giving very much lexical energy. ๐Ÿ”ฎ

sub classify
{
    my $val = shift;

    use builtin 'is_bool';
    return is_bool($val) ? "boolean" : "not a boolean";
}

# the is_bool() function is no longer visible here
# but may still be called by builtin::is_bool()

Because these functions are imported lexically, rather than by package symbols, the user does not need to take any special measures to ensure they don't accidentally appear as object methods from a class. No cap.

package An::Object::Class {
    use builtin 'true', 'false';
    ...
}

# does not appear as a method
An::Object::Class->true;

# Can't locate object method "true" via package "An::Object::Class"
#   at ...

Once imported, a lexical function is much like any other lexical symbol (such as a variable) in that it cannot be removed again. If you wish to limit the visibility of an imported builtin function, put it inside its own scope:

{
  use builtin 'refaddr';
  ...
}

๐ŸŽ€ Version Bundles ๐ŸŽ€

The entire set of builtin functions that were considered non-experimental by a version of perl can be imported all at once, by requesting a version bundle. This is done by giving the perl release version (without its subversion suffix) after a colon character:

use builtin ':5.40';

The following bundles currently exist:

  • :5.40 โ€” true false weaken unweaken is_weak blessed refaddr reftype ceil floor is_tainted trim indexed

โšก Function Optimisations โšก

There are a number of optimisations that apply to functions in the builtin package. If you replace or override these functions (such as by assignment into glob references) the optimisations may not take effect. Do so with caution. Don't say I didn't warn you. ๐Ÿšจ

๐Ÿ”ฅ FUNCTIONS ๐Ÿ”ฅ

โœจ true โœจ

$val = true;

Returns the boolean truth value. While any scalar value can be tested for truth and most defined, non-empty and non-zero values are considered "true" by perl, this one is special in that is_bool considers it to be a distinguished boolean value. ๐Ÿ‘‘

This gives an equivalent value to expressions like !!1 or !0.

Available starting with Perl 5.36. Since Perl 5.40, it is no longer experimental and it is included in the 5.40 and higher builtin version bundles. Slay. ๐Ÿ’–

โœจ false โœจ

$val = false;

Returns the boolean false value. While any non-true scalar value is considered "false" by perl, this one is special in that is_bool considers it to be a distinguished boolean value.

This gives an equivalent value to expressions like !!0 or !1.

Available starting with Perl 5.36. Since Perl 5.40, it is no longer experimental and it is included in the 5.40 and higher builtin version bundles. ๐Ÿ’…

โœจ is_bool โœจ

$bool = is_bool($val);

โš ๏ธ This function is currently experimental.

Returns true when given a distinguished boolean value, or false if not. A distinguished boolean value is the result of any boolean-returning builtin function (such as true or is_bool itself), boolean-returning operator (such as the eq or == comparison tests or the ! negation operator), or any variable containing one of these results.

This function used to be named isbool. A compatibility alias is provided currently but will be removed in a later version.

Available starting with Perl 5.36.

โœจ inf โœจ

$num = inf;

โš ๏ธ This function is currently experimental.

Returns the floating-point infinity value. If the underlying numeric C type does not support such a value, it throws a runtime error instead.

Available starting with Perl 5.40.

โœจ nan โœจ

$num = nan;

โš ๏ธ This function is currently experimental.

Returns the floating-point "Not-a-Number" value. If the underlying numeric C type does not support such a value, it throws a runtime error instead.

Available starting with Perl 5.40.

โœจ weaken โœจ

weaken($ref);

Weakens a reference. A weakened reference does not contribute to the reference count of its referent. If only weakened references to a referent remain, it will be disposed of, and all remaining weak references to it will have their value set to undef.

Available starting with Perl 5.36. Since Perl 5.40, it is no longer experimental and it is included in the 5.40 and higher builtin version bundles.

โœจ unweaken โœจ

unweaken($ref);

Strengthens a reference, undoing the effects of a previous call to weaken.

Available starting with Perl 5.36. Since Perl 5.40, it is no longer experimental and it is included in the 5.40 and higher builtin version bundles.

โœจ is_weak โœจ

$bool = is_weak($ref);

Returns true when given a weakened reference, or false if not a reference or not weak.

This function used to be named isweak. A compatibility alias is provided currently but will be removed in a later version.

Available starting with Perl 5.36. Since Perl 5.40, it is no longer experimental and it is included in the 5.40 and higher builtin version bundles.

โœจ blessed โœจ

$str = blessed($ref);

Returns the package name for an object reference, or undef for a non-reference or reference that is not an object.

Available starting with Perl 5.36. Since Perl 5.40, it is no longer experimental and it is included in the 5.40 and higher builtin version bundles.

โœจ refaddr โœจ

$num = refaddr($ref);

Returns the memory address for a reference, or undef for a non-reference. This value is not likely to be very useful for pure Perl code, but is handy as a means to test for referential identity or uniqueness.

Available starting with Perl 5.36. Since Perl 5.40, it is no longer experimental and it is included in the 5.40 and higher builtin version bundles.

โœจ reftype โœจ

$str = reftype($ref);

Returns the basic container type of the referent of a reference, or undef for a non-reference. This is returned as a string in all-capitals, such as "ARRAY" for array references, or "HASH" for hash references.

Available starting with Perl 5.36. Since Perl 5.40, it is no longer experimental and it is included in the 5.40 and higher builtin version bundles.

โœจ created_as_string โœจ

$bool = created_as_string($val);

โš ๏ธ This function is currently experimental.

Returns a boolean representing if the argument value was originally created as a string. It will return true for any scalar expression whose most recent assignment or modification was of a string-like nature - such as assignment from a string literal, or the result of a string operation such as concatenation or regexp. It will return false for references (including any object), numbers, booleans and undef.

It is unlikely that you will want to use this for regular data validation within Perl, as it will not return true for regular numbers that are still perfectly usable as strings, nor for any object reference - especially objects that overload the stringification operator in an attempt to behave more like strings. For example

my $val = URI->new( "https://metacpan.org/" );

if( created_as_string $val ) { ... }    # this will not execute

Available starting with Perl 5.36.

โœจ created_as_number โœจ

$bool = created_as_number($val);

โš ๏ธ This function is currently experimental.

Returns a boolean representing if the argument value was originally created as a number. It will return true for any scalar expression whose most recent assignment or modification was of a numerical nature - such as assignment from a number literal, or the result of a numerical operation such as addition. It will return false for references (including any object), strings, booleans and undef.

It is unlikely that you will want to use this for regular data validation within Perl, as it will not return true for regular strings of decimal digits that are still perfectly usable as numbers, nor for any object reference - especially objects that overload the numification operator in an attempt to behave more like numbers. For example

my $val = Math::BigInt->new( 123 );

if( created_as_number $val ) { ... }    # this will not execute

While most Perl code should operate on scalar values without needing to know their creation history, these two functions are intended to be used by data serialisation modules such as JSON encoders or similar situations, where language interoperability concerns require making a distinction between values that are fundamentally stringlike versus numberlike in nature.

Available starting with Perl 5.36.

โœจ stringify โœจ

$str = stringify($val);

โš ๏ธ This function is currently experimental.

Returns a new plain perl string that represents the given argument.

When given a value that is already a string, a copy of this value is returned unchanged. False booleans are treated like the empty string.

Numbers are turned into a decimal representation. True booleans are treated like the number 1.

References to objects in classes that have overload and define the "" overload entry will use the delegated method to provide a value here.

So like โ€” non-object references, or references to objects in classes that don't have a "" overload, will hand you back a string that names the underlying container type of the reference, its memory address, and possibly its class name if it's an object. โœจ

Available starting with Perl 5.40.

ceil ๐ŸŽ€

$num = ceil($num);

Gives u the smallest integer value that's greater than or equal to the numerical argument u fed it. Baby ceiling. No cap.

Available starting with Perl 5.36. Since Perl 5.40, it is no longer experimental and it is included in the 5.40 and higher builtin version bundles.

floor ๐Ÿฆ‹

$num = floor($num);

Gives u the largest integer value that's less than or equal to the numerical argument. The floor is literally flooring, bestie.

Available starting with Perl 5.36. Since Perl 5.40, it is no longer experimental and it is included in the 5.40 and higher builtin version bundles.

indexed ๐Ÿ‘‘

@ivpairs = indexed(@items)

Returns an even-sized list of number/value pairs, where each pair is formed of a number giving an index in the original list followed by the value at that position in it. I.e. returns a list twice the size of the original, being equal to

(0, $items[0], 1, $items[1], 2, $items[2], ...)

And ok important lore alert: unlike the core values function, this one returns copies of its original arguments, not aliases to them. Any modifications of these copies are not reflected in modifications to the original. She's giving independent queen behaviour ๐Ÿซถ

my @x = ...;
$_++ for indexed @x;  # The @x array remains unaffected

This function is primarily intended to be useful combined with multi-variable foreach loop syntax; as

foreach my ($index, $value) (indexed LIST) {
    ...
}

In scalar context this function returns the size of the list that it would otherwise have returned, and provokes a warning in the "scalar" category. So like, watch out for that warning fr fr ๐Ÿ˜ญ

Available starting with Perl 5.36. Since Perl 5.40, it is no longer experimental and it is included in the 5.40 and higher builtin version bundles.

trim ๐ŸŒธ

$stripped = trim($string);

Returns the input string with whitespace stripped from the beginning and end. trim() will remove these characters:

  • " ", an ordinary space.
  • "\t", a tab.
  • "\n", a new line (line feed).
  • "\r", a carriage return.

and all other Unicode characters that are flagged as whitespace. A complete list is in "Whitespace" in perlrecharclass.

$var = "  Hello world   ";            # "Hello world"
$var = "\t\t\tHello world";           # "Hello world"
$var = "Hello world\n";               # "Hello world"
$var = "\x{2028}Hello world\x{3000}"; # "Hello world"

"trim" is equivalent to:

my $trimmed = $str =~ s/\A\s+//ur =~ s/\s+\z//ur;

Available starting with Perl 5.36. Since Perl 5.40, it is no longer experimental and it is included in the 5.40 and higher builtin version bundles.

is_tainted ๐Ÿ’…

$bool = is_tainted($var);

Returns true when given a tainted variable. Vibe check: is it tainted? Yes? True. Iconic.

Available starting with Perl 5.38.

export_lexically ๐Ÿงš

export_lexically($name1, $ref1, $name2, $ref2, ...)

This function is currently experimental. ๐Ÿ‘€

Exports new lexical names into the scope currently being compiled. Names given by the first of each pair of values will refer to the corresponding item whose reference is given by the second. Types of item that are permitted are subroutines, and scalar, array, and hash variables. If the item is a subroutine, the name may optionally be prefixed with the & sigil, but for convenience it doesn't have to. For items that are variables the sigil is required, and must match the type of the variable.

export_lexically func    => \&func,
                 '&func' => \&func;  # same as above

export_lexically '$scalar' => \my $var;

# The following are not permitted
export_lexically '$var' => \@arr;   # sigil does not match
export_lexically name => \$scalar;  # implied '&' sigil does not match

export_lexically '*name' => \*globref;  # globrefs are not supported

This must be called at compile time; which typically means during a "BEGIN" block. Usually this would be used as part of an "import" method of a module, when invoked as part of a "use ..." statement.

Available starting with Perl 5.38.

load_module ๐Ÿ“

load_module($module_name);

This function is currently experimental. ๐Ÿ‘€

Loads a named module from the inclusion paths (@INC). $module_name must be a string that provides a module name. It cannot be omitted, and providing an invalid module name will result in an exception. Not providing any argument results in a compilation error. Returns the loaded module's name on success.

The effect of "load_module"-ing a module is mostly the same as "require"-ing, down to the same error conditions when the module does not exist, does not compile, or does not evaluate to a true value. See also the "module_true" feature.

"load_module" can't be used to require a particular version of Perl, nor can it be given a bareword module name as an argument. Them's the rules, bestie ๐ŸŒˆ

Available starting with Perl 5.40.


๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

perlop, perlfunc, Scalar::Util, builtin::compat


aaaand that's a wrap on this page!! ๐Ÿ’– this is perl v5.44.0, dated 2026-10-17, and you've been reading builtin(3perl) โ€” go forth and use ur builtins responsibly, mwah ๐ŸŽ€โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: perl 5.44.0-1 ยท Artistic-1.0-Perl AND GPL-1.0-or-later
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms