This module is DEPRECATED. Please use Ref::Util instead.
Scalar::Util::Reftype - Alternate reftype() interface
use Scalar::Util::Reftype; foo() if reftype( "string" )->hash; # foo() will never be called bar() if reftype( \$var )->scalar; # bar() will be called baz() if reftype( [] )->array; # baz() will be called xyz() if reftype( sub {} )->array; # xyz() will never be called $obj = bless {}, "Foo"; my $rt = reftype( $obj ); $rt->hash; # false $rt->hash_object; # true $rt->class; # "Foo"
This is an alternate interface to Scalar::Util's reftype function. Instead of manual type checking you can just call methods on the result to see if matches the desired type.
Scalar::Util
reftype
Exported by default. EXPR can be any value (even undef).
EXPR
undef
Returns an object with which you can call various test methods. Unless specified otherwise, all of the test methods return either zero (false) or one (true) based on the EXPR you have specified.
Return values of reftype() can not be used in boolean contexts. If you do, it'll die with a verbose error message.
my $r = reftype( $foo ) || 'something'; # dies bar() if reftype( $foo ); # dies
Always call the test methods on the return value:
bar() if reftype( $foo )->array;
Or, if you want to have multiple tests, without executing reftype multiple times:
my $r = reftype( $foo ); bar() if $r->array; baz() if $r->array_object; die "ooooh! scaaaary..." if $r->format_object;
The available test methods are listed below.
Tests if EXPR is a SCALAR reference or not.
Tests if EXPR is an ARRAY reference or not.
Tests if EXPR is a HASH reference or not.
Tests if EXPR is a CODE reference or not.
Tests if EXPR is a GLOB reference or not.
Tests if EXPR is a LVALUE reference or not.
Tests if EXPR is a FORMAT reference or not.
Tests if EXPR is a reference to a reference or not.
Tests if EXPR is a IO reference or not.
CAVEAT: reftype(EXPR)->io_object is also true since there is no way to distinguish them (i.e.: IO refs are already implemented as objects).
reftype(EXPR)->io_object
Tests if EXPR is a Regexp reference or not.
Tests if EXPR is a SCALAR reference based object or not.
Tests if EXPR is an ARRAY reference based object or not.
Tests if EXPR is a HASH reference based object or not.
Tests if EXPR is a CODE reference based object or not.
Tests if EXPR is a GLOB reference based object or not.
Tests if EXPR is a LVALUE reference based object or not.
Tests if EXPR is a FORMAT reference based object or not.
Tests if EXPR is a reference to a reference based object or not.
Tests if EXPR is a IO reference based object or not.
CAVEAT: reftype(EXPR)->io is also true since there is no way to distinguish them (i.e.: IO refs are already implemented as objects).
reftype(EXPR)->io
Tests if EXPR is a Regexp reference based object or not.
Returns the name of the class the object based on if EXPR is an object. Returns an empty string otherwise.
perl versions 5.10 and newer includes the function re::is_regexp to detect if a reference is a regex or not. While it is possible to detect normal regexen in older perls, there is no simple way to detect blessed regexen. Blessing a regex hides it from normal probes. If you are under perl 5.8.x or older, you'll need to install (in fact, it's in the prerequisities list so any automated tool --like cpan shell-- will install it automatically) Data::Dump::Streamer which provides the regex function similar to re::is_regexp.
re::is_regexp
bless
5.8.x
Data::Dump::Streamer
regex
IO refs are already implemented as objects, so both reftype(EXPR)->io and reftype(EXPR)->io_object will return true if EXPR is either an IO reference or an IO reference based object.
VSTRING references are not supported and not implemented.
VSTRING
FORMAT references can be detected under perl 5.8 and newer. Under older perls, even the accessors are not defined for FORMAT.
FORMAT
reftype in Scalar::Util
re
http://perlmonks.org/?node_id=665339
t/op/ref.t in perl source
t/op/ref.t
ref in perlfunc.
ref
To install Scalar::Util::Reftype, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Scalar::Util::Reftype
CPAN shell
perl -MCPAN -e shell install Scalar::Util::Reftype
For more information on module installation, please visit the detailed CPAN module installation guide.