Data::Util(3pm) | User Contributed Perl Documentation | Data::Util(3pm) |
Data::Util - A selection of utilities for data and data types
This document describes Data::Util version 0.67
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
This module provides utility functions for data and data types, including functions for subroutines and symbol table hashes (stashes).
This module makes for a pure Perl and XS implementation.
However, if you want to use the full capacity of it, we recommend you to opt for the XS backend.
There are many benchmarks in the DIST-DIR/benchmark/ directory.
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.
It is equivalent to the following statement: "Scalar::Util::blessed($value) && $value->isa($class)".
If value is a valid class name but does not exist, it will return false.
This function has no counterpart for validation.
This function has no counterpart for validation.
It is similar to Scalar::Util::looks_like_number() but refuses "infinity", "not a number" and "0 but true". Note that "9**9**9" makes "infinity" and "9**9**9 - 9**9**9" makes "not a number".
This function has no counterpart for validation.
This function has no counterpart for validation.
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.
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.
There are some other utility functions you can import from this module.
It is equivalent to "do{ my $tmp = $value; \$tmp; }".
This is a smart version of "<do{ defined($value) ? qq{"$value"} : 'undef' }">.
It is similar to "do{ 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);
It is similar to Sub::Delete::delete_sub(), but uninstall multiple subroutines at a time.
If you want to specify deleted subroutines, you can supply "name => \&subr" pairs.
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 });
It is similar to Sub::Identify::get_code_info(), but it returns the fully qualified name in scalar context.
if flag is a string "-create", it returns &package::name regardless of its existence. That is, it is equivalent to "do{ 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}
This is also considered as lightweight closures.
See also Data::Util::Curry.
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 and subroutine_modifier() is property accessors, but it does not bless the modified subroutines.
Valid properties are: "before", "around", "after".
Valid modifiers are: "before", "around", "after".
It is compatible with Data::OptList::mkopt(). In addition to it, must_be can be a HASH reference with "name => type" pairs.
For example:
my $optlist = mkopt(['foo', bar => [42]], $moniker, $uniq, { bar => 'ARRAY' }); # $optlist == [[foo => undef], [bar => [42]]
It is compatible with Data::OptList::mkopt_hash(). In addition to it, must_be can be a HASH reference with "name => type" pairs.
For example:
my $optlist = mkopt(['foo', bar => [42]], $moniker, { bar => 'ARRAY' }); # $optlist == {foo => undef, bar => [42]}
If true, "Data::Util" uses the pure Perl implementation.
Perl 5.10 or later.
If you have a C compiler, you can use the XS backend.
A pure Perl backend/implementation is also made available in case you have no C compiler handy (unlikely!).
No bugs have been reported.
Please report any bugs or feature requests to the author.
Scalar::Util.
overload.
Params::Util.
Sub::Install.
Sub::Identify.
Sub::Delete.
Sub::Curry.
Class::MOP.
Class::Method::Modifiers.
Data::OptList.
Mouse
Goro Fuji(gfx) <gfuji(at)cpan.org>.
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.
2024-04-10 | perl v5.38.2 |