NAME
Data::Util - A selection of utilities for data and data types
VERSION
This document describes Data::Util version 0.02
SYNOPSIS
use Data::Util qw(:validate);
sub foo{
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 $rref = regex_ref(shift);
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
}
# ...
}
# to generate an anonymous scalar reference
use Data::Util qw(anon_scalar)
my $ref_to_undef = anon_scalar();
$x = anon_scalar($x); # OK
DESCRIPTION
This module provides utility subroutines for data and data types.
INTERFACE
Check functions
Check functions are introduced by the :check
tag, which check the argument type.
- is_scalar_ref($x)
-
For a SCALAR reference.
- is_array_ref($x)
-
For an ARRAY reference.
- is_hash_ref($x)
-
For a HASH reference.
- is_code_ref($x)
-
For a CODE reference.
- is_glob_ref($x)
-
For a GLOB reference.
- is_regex_ref($x)
-
For a regular expression reference.
- is_instance($x, $class)
-
For an instance of a class.
It is equivalent to something like
Scalar::Util::blessed($x) && $x-
isa($class) ? $x : undef>, but significantly faster and easy to use.
Validating functions
Validating functions are introduced by the :validate
tag, and returns the first argument $x
. They are like the :check
functions, but they will die if the argument type is not the wanted type.
- scalar_ref($x)
-
For a SCALAR reference.
- array_ref($x)
-
For an ARRAY reference.
- hash_ref($x)
-
For a HASH reference.
- code_ref($x)
-
For a CODE reference.
- glob_ref($x)
-
For a GLOB reference.
- regex_ref($x)
-
For a regular expression reference.
- instance($x, $class)
-
For an instance of a class.
Other utilities
- anon_scalar()
-
Generates an anonymous scalar reference to
undef
. - anon_scalar(expr)
-
Generates an anonymous scalar reference to expr.
- neat(expr)
-
Returns a neat string that is suitable to display.
- get_stash(package_name)
-
Returns the stash of package_name.
It is equivalent to
do{ no strict 'refs'; \%{$package_name . '::'} }
, but does not create the stash if package_name does not exist. - is_method(coderef)
- set_method_attribute(coderef, bool)
-
Gets/sets the
:method
attribute of coderef.
Subdirectives
- -fast_isa subdirective
-
Replaces
UNIVERSAL::isa()
byfast_isa
, which is even faster.This alternative subroutine passes all the PERL-DIST/t/op/universal.t (included as Data-Util/t/10_fast_isa.t).
DEPENDENCIES
Perl 5.8.1 or later, and a C compiler.
BUGS AND LIMITATIONS
No bugs have been reported.
Please report any bugs or feature requests to bug-scalar-util-ref@rt.cpan.org/
, or through the web interface at http://rt.cpan.org/.
SEE ALSO
AUTHOR
Goro Fuji <gfuji(at)cpan.org>
LICENSE AND COPYRIGHT
Copyright (c) 2008, Goro Fuji <gfuji(at)cpan.org>. Some rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.