13

Is there any function available in Perl to check the reference type:

my $ref=\@array;

I need to get the reference type as array by the function.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
karthi_ms
  • 5,568
  • 11
  • 38
  • 39
  • 2
    possible duplicate of [How do I tell what type of value is in a Perl variable?](http://stackoverflow.com/questions/1731333/how-do-i-tell-what-type-of-value-is-in-a-perl-variable) – Ether Aug 04 '10 at 15:07

1 Answers1

29

Use function ref:

$ref_type = ref $ref;

The return value is the one of: SCALAR, ARRAY, HASH, CODE (reference to subprogram), GLOB (reference to typeglob) and REF (reference to reference).

Actually, ref function may return more values and in case of reference to object returns package name instead of type: http://perldoc.perl.org/functions/ref.html.

pmod
  • 10,450
  • 1
  • 37
  • 50
  • 7
    Note the reference has been blessed, ref() returns the package name, not the underlying data-type. In such cases, you might consider using Scalar::Util's reftype and blessed methods. – dwarring Aug 04 '10 at 06:30
  • 1
    @snoopy Though, most of the time it's more useful to know the package name (class) of the object. And you shouldn't be messing with the object internals manually anyway. – slebetman Aug 04 '10 at 06:34