Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How do I determine the type of a reference?

by Anonymous Monk
on Jun 19, 2002 at 13:08 UTC ( #175651=categorized question: print w/replies, xml ) Need Help??
Contributed by Anonymous Monk on Jun 19, 2002 at 13:08 UTC
Q&A  > references


Description:

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 { . . . }

Answer: How do I determine the type of a reference?
contributed by hippo

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.

Answer: How do I determine the type of a reference?
contributed by choocroot

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".

Answer: How do I determine the type of a reference?
contributed by tye

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 }
Answer: How do I determine the type of a reference?
contributed by Mr. Muskrat

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 } # ...
Answer: How do I determine the type of a reference?
contributed by particle

See tye's excellent References quick reference.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (4)
    As of 2017-06-28 22:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How many monitors do you use while coding?















      Results (649 votes). Check out past polls.