Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How do I determine the type of a reference?

( #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 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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (10)
    As of 2014-10-01 07:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (389 votes), past polls