⭐ 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 | |
by Mr. Muskrat (Canon) on Sep 18, 2003 at 17:57 UTC |