Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How do I determine the type of a reference?

by Mr. Muskrat (Canon)
on Sep 18, 2003 at 16:39 UTC ( [id://292445]=note: print w/replies, xml ) Need Help??


in reply to How do I determine the type of a reference?

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

Replies are listed 'Best First'.
Re: Answer: How do I tell what type of structure a given reference points to?
by asarih (Hermit) on Sep 18, 2003 at 17:42 UTC
    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!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-04-18 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found