Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

How do I determine the type of a reference?

by Anonymous Monk
on Jun 19, 2002 at 13:08 UTC ( [id://175651]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (references)

Is there a way to determine whether a variable ($x) is a reference, and if so, what type of thing it refers to?

For example, I want to do something like:

if ( is_not_a_reference($x) ) { print $x; } elsif ( is_a_hash_ref($x) ) { process_hash( %$x ); } else { . . . }

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I determine the type of a reference?
by hippo (Bishop) on May 24, 2017 at 14:43 UTC

    There is now also Ref::Util which avoids the need to compare the result against a string (which itself might contain a typo). So, instead of

    use Scalar::Util 'reftype'; if (reftype ($foo) eq 'ARAY') { # spot the typo do_something (); }

    which would silently fail to do anything even if $foo were an arrayref, we can now say

    use Ref::Util 'is_arrayref'; if (is_arrayref ($foo)) { do_something (); }

    which would throw a compile-time error if we had a similar typo in the function name.

    Ref::Util also uses the XS implementation (if installed) which makes it fast. It has no dependencies beyond Exporter. There are plenty of other goodies in this module which you can find in the documentation.

Re: How do I determine the type of a reference?
by choocroot (Friar) on Jun 19, 2002 at 13:33 UTC

    Use the ref function. It returns false if its argument is not a reference, and otherwise returns a string describing the referenced object. For example, in the case of an unblessed hash, it returns the string "HASH".

Re: How do I determine the type of a reference?
by tye (Sage) on Sep 18, 2003 at 17:03 UTC
    if ( ! ref($x) ) { # Not a reference } elsif ( UNIVERSAL::isa($x,'HASH') ) { # Reference to a hash } elsif ( UNIVERSAL::isa($x,'ARRAY') ) { # Reference to an array } elseif ( UNIVERSAL::isa($x,'SCALAR') || UNIVERSAL::isa($x,'REF') ) { # Reference to a scalar } elsif ( UNIVERSAL::isa($x,'CODE') ) { # Reference to a subroutine }
Re: How do I determine the type of a reference?
by Mr. Muskrat (Canon) on Sep 18, 2003 at 16:39 UTC

    The Scalar::Util module provides a function reftype for this. It's somewhat more robust than the built-in ref function.

    use Scalar::Util 'reftype'; my $reftype = reftype $x; if ( !defined $reftype ) { print "\$x is not a reference.\n"; } elsif ( $reftype eq 'HASH' ) { # do something with %$x } elsif ( $reftype eq 'ARRAY' ) { # do something with @$x } elsif ( $reftype eq 'SCALAR' ) { # do something with $$x } else { # do something else } # ...
      Note that Scalar::Util::reftype behaves almost like the built-in ref, except when an object is passed.
      use Scalar::Util 'reftype'; $a=\$b; print reftype($a),"\n"; #SCALAR print ref($a),"\n"; #SCALAR bless $a, "BOGUS"; print reftype($a),"\n"; #SCALAR print ref($a),"\n"; #BOGUS
        And that is exactly why I chose to use it!
Re: How do I determine the type of a reference?
by particle (Vicar) on Jun 19, 2002 at 13:26 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://175651]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-18 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found