Data::Util(3pm)
π Data::Util(3pm) π
okay bestie, this manual is for Data::Util β a selection of utilities for data and data types. π
β¨ NAME β¨
Data::Util β A selection of utilities for data and data types
π¦ VERSION π¦
This document describes Data::Util version 0.67. That's the version, no cap. β¨
π SYNOPSIS π
Here's the vibe check on how you'd actually use it:
use Data::Util qw(:validate);
sub foo{
# they will die if invalid values are supplied
my $sref = scalar_ref(shift);
my $aref = array_ref(shift);
my $href = hash_ref(shift);
my $cref = code_ref(shift);
my $gref = glob_ref(shift);
my $rx = rx(shift); # regular expression
my $obj = instance(shift, 'Foo');
# ...
}
use Data::Util qw(:check);
sub bar{
my $x = shift;
if(is_scalar_ref $x){
# $x is an array reference
}
# ...
elsif(is_instance $x, 'Foo'){
# $x is an instance of Foo
}
# ...
}
# miscelaneous
use Data::Util qw(:all);
my $x = anon_scalar();
$x = anon_scalar($x); # OK
my $stash = get_stash('Foo');
install_subroutine('Foo',
hello => sub{ "Hello!\n" },
goodby => sub{ "Goodby!\n" },
);
print Foo::hello(); # Hello!
my($pkg, $name) = get_code_info(\&Foo::hello); # => ('Foo', 'hello')
my $fqn = get_code_info(\&Foo::hello); # => 'Foo::hello'
my $code = get_code_ref('Foo', 'hello'); # => \&Foo::hello
uninstall_subroutine('Foo', qw(hello goodby));
# simple format for errro messages (not the same as Data::Dumper)
print neat("Hello!\n"); # => "Hello!\n"
print neat(3.14); # => 3.14
print neat(undef); # => undef
π DESCRIPTION π
This module is giving you utility functions for data and data types, including stuff for subroutines and symbol table hashes (aka stashes). π
It ships as both a pure Perl implementation and an XS implementation.
But bestie, if you want to use the full capacity of it, we recommend you opt for the XS backend. That's where the magic is.
There are many benchmarks in the DIST-DIR/benchmark/ directory if you want to see the receipts. π₯
π INTERFACE π
π Check functions π
Check functions are introduced by the :check import tag, which check the argument type and return a bool.
These functions also check for overloading magic, e.g. ${} corresponds to a SCALAR reference.
is_scalar_ref(value)β Checks for a SCALAR reference.is_array_ref(value)β Checks for an ARRAY reference.is_hash_ref(value)β Checks for a HASH reference.is_code_ref(value)β Checks for a CODE reference.is_glob_ref(value)β Checks for a GLOB reference.is_rx(value)β Checks for a regular expression reference generated by theqr//operator.is_instance(value, class)β Checks for an instance of class. It is equivalent to the following statement:Scalar::Util::blessed($value) && $value->isa($class).is_invocant(value)β Checks for an invocant, i.e. a blessed reference or existent package name. If value is a valid class name but does not exist, it will return false.is_value(value)β Checks whether value is a primitive value, i.e. a defined, non-ref, and non-type-glob value. This function has no counterpart for validation.is_string(value)β Checks whether value is a string with non-zero-length contents, equivalent tois_value($value) && length($value) > 0. This function has no counterpart for validation.is_number(value)β Checks whether value is a number. Here, a number means that the perl parser can understand it and that the perl numeric converter (e.g. invoked bysprintf '%g', $value) doesn't complain about it. It is similar toScalar::Util::looks_like_number()but refusesinfinity,not a numberand0 but true. Note that9**9**9makesinfinityand9**9**9 - 9**9**9makesnot a number. This function has no counterpart for validation.is_integer(value)β Checks whether value is an integer. An integer is also a number, so this function refusesinfinityandnot a number. See alsois_number(). This function has no counterpart for validation.
β¨ Validating functions β¨
Validating functions are introduced by the :validate tag which checks for the argument and returns the first argument. These are like the :check functions but dies if the argument type is invalid.
These functions also checks overloading magic, e.g. ${} for a SCALAR reference.
scalar_ref(value)β Validates a SCALAR reference.array_ref(value)β Validates an ARRAY reference.hash_ref(value)β Validates a HASH reference.code_ref(value)β Validates a CODE reference.glob_ref(value)β Validates a GLOB reference.rx(value)β Validates a regular expression reference.instance(value, class)β Validates an instance of class.invocant(value)β Validates an invocant, i.e. a blessed reference or existent package name.
If value is a valid class name and the class exists, then it returns the canonical class name, which is logically cleaned up. That is, it runs $value =~ s/^::(?:main::)*//; before returning it.
NOTE: Canonization is done so due to an inconsistency between Perl versions. For instance:
package ::Foo; # OK
my $x = bless {}, '::Foo'; # OK
ref($x)->isa('Foo'); # Fatal
The last code snippet causes a fatal error: Can't call method "isa" without package or object reference. However, invocant(ref $x)->isa('Foo') is always OK.
π Miscellaneous utilities π
There are some other utility functions you can import from this module.
anon_scalar()β Generates an anonymous scalar reference toundef.anon_scalar(value)β Generates an anonymous scalar reference to the copy of value. It is equivalent todo{ my $tmp = $value; \$tmp; }.neat(value)β Returns a neat string that is suitable to display. This is a smart version of"<do{ defined($value) ? qq{"$value"} : 'undef' }>".
Wait, that's a lot of quotes. But it's exactly what the original says. π
get_stash(invocant)β Returns the symbol table hash (also known as stash) of invocant if the stash exists.install_subroutine(package, name => subr [, ...])β Installs subr into package as name. It is similar todo{ no strict 'refs'; *{$package.'::'.$name} = \&subr; }. In addition, if subr is an anonymous subroutine, it is located into package as a named subroutine&package::name. For example:
install_subroutine($pkg, say => sub{ print @_, "\n" });
install_subroutine($pkg,
one => \&_one,
two => \&_two,
);
# accepts a HASH reference
install_subroutine($pkg, { say => sub{ print @_, "\n" }); #
To re-install subr, use no warnings 'redefine' directive:
no warnings 'redefine';
install_subroutine($package, $name => $subr);
uninstall_subroutine(package, names...)β Uninstalls names from package. It is similar toSub::Delete::delete_sub(), but uninstall multiple subroutines at a time. If you want to specify deleted subroutines, you can supplyname => \&subrpairs. For example:
uninstall_subroutine('Foo', 'hello');
uninstall_subroutine('Foo', hello => \&Bar::hello);
uninstall_subroutine($pkg,
one => \&_one,
two => \&_two,
);
# accepts a HASH reference
uninstall_subroutine(\$pkg, { hello => \&Bar::hello });
get_code_info(subr)β Returns a pair of elements, the package name and the subroutine name of subr. It is similar toSub::Identify::get_code_info(), but it returns the fully qualified name in scalar context.get_code_ref(package, name, flag?)β Returns&package::nameif it exists, not touching the symbol in the stash. if flag is a string-create, it returns&package::nameregardless of its existence. That is, it is equivalent todo{ no strict 'refs'; \&{package . '::' . $name} }. For example:
$code = get_code_ref($pkg, $name); # like *{$pkg.'::'.$name}{CODE}
$code = get_code_ref($pkg, $name, -create); # like \&{$pkg.'::'.$name}
curry(subr, args and/or placeholders)β Makes subr curried and returns the curried subroutine. This is also considered as lightweight closures. See alsoData::Util::Curry.modify_subroutine(subr, ...)β Modifies subr with subroutine modifiers and returns the modified subroutine. This is also considered as lightweight closures. subr must be a code reference or callable object. Optional arguments:before => [subroutine(s)]called before subr.around => [subroutine(s)]called around subr.after => [subroutine(s)]called after subr. This seems a constructor of modified subroutines andsubroutine_modifier()is property accessors, but it does not bless the modified subroutines.subroutine_modifier(subr)β Returns whether subr is a modified subroutine.subroutine_modifier(modified_subr, property)β Gets property from modified. Valid properties are:before,around,after.subroutine_modifier(modified_subr, modifier => [subroutine(s)])β Adds subroutine modifier to modified_subr. Valid modifiers are:before,around,after.mkopt(input, moniker, require_unique, must_be)β Produces an array of an array reference from input. It is compatible withData::OptList::mkopt(). In addition to it, must_be can be a HASH reference withname => typepairs. For example:
my $optlist = mkopt(['foo', bar => [42]], $moniker, $uniq, { bar => 'ARRAY' });
# $optlist == [[foo => undef], [bar => [42]]
mkopt_hash(input, moniker, must_be)β Produces a hash reference from input. It is compatible withData::OptList::mkopt_hash(). In addition to it, must_be can be a HASH reference withname => typepairs. For example:
my $optlist = mkopt(['foo', bar => [42]], $moniker, { bar => 'ARRAY' });
# $optlist == {foo => undef, bar => [42]}
π ENVIRONMENT VARIABLES π
π§ DATA_UTIL_PUREPERL π§
If true, Data::Util uses the pure Perl implementation.
π DEPENDENCIES π
okay bestie, before you get too excited, here's the vibe check on what you need:
- Perl 5.10 or later. that's it, that's the floor. anything older and we can't hang π
- πΈ if you have a C compiler? slay. you get to use the XS backend and it's giving speed, it's giving performance, it's giving main character energy.
- π and if you don't have a C compiler handy β which, ngl, is unlikely! β there's still a pure Perl backend/implementation made available for you. so nobody's left out of the group chat. periodt.
π BUGS AND LIMITATIONS π
okay so here's the tea:
- No bugs have been reported. β¨ literally iconic. flawless. no cap.
- if you do find something? or you're just sitting there like "bestie I want a feature"? please report any bugs or feature requests to the author. they want to hear from you fr fr π«Ά
π₯ SEE ALSO π₯
if you liked this, you're gonna LOVE these. the whole friend group, no gatekeeping:
Scalar::UtiloverloadParams::UtilSub::InstallSub::IdentifySub::DeleteSub::CurryClass::MOPClass::Method::ModifiersData::OptListMouse
it's giving β¨ecosystemβ¨. iykyk.
π AUTHOR π
this masterpiece was brought to you by:
Goro Fuji(gfx) <gfuji(at)cpan.org>.
genuinely obsessed with them. thank you for your service π«‘π
π LICENSE AND COPYRIGHT π
Copyright (c) 2008-2010, Goro Fuji <gfuji(at)cpan.org>. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. π
so yeah. that's the whole page. you made it to the end, bestie, and honestly? slay. π
and hey, the manpage footer says this all came from perl v5.42.3, dated 2026-08-19, and lives at Data::Util(3pm) β so if anyone asks where you learned all this, you know exactly where to send them π¦π
mwah. go write some clean Perl. β¨π
π¦ where this page comes from
- ubuntu stonking: libdata-util-perl 0.67-1build7 Β· Artistic-1.0-Perl OR 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