You are viewing the version of this documentation from Perl 5.35.7. This is a development release of Perl.

CONTENTS

NAME

builtin - Perl pragma to import built-in utility functions

SYNOPSIS

use builtin qw(
    true false isbool
    weaken unweaken isweak
    blessed refaddr reftype
);

DESCRIPTION

Perl provides several utility functions in the builtin package. These are plain functions, and 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 overall builtin mechanism, as well as every individual function it provides, are currently experimental.

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.

sub classify
{
    my $sv = shift;

    use builtin 'isbool';
    return isbool($sv) ? "boolean" : "not a boolean";
}

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

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.

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 ...

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 "isbool" considers it to be a distinguished boolean value.

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

false

$val = false;

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

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

isbool

$bool = isbool($val);

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 isbool itself), boolean-returning operator (such as the eq or == comparison tests or the ! negation operator), or any variable containing one of these results.

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.

unweaken

unweaken($ref);

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

isweak

$bool = isweak($ref);

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

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.

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.

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.

SEE ALSO

perlop, perlfunc, Scalar::Util