in reply to How do I determine the underlying type of an Object?

I suppose you could do something like this:

sub type { my $obj = shift; return $obj->isa('HASH') ? 'HASH' : $obj->isa('ARRAY') ? 'ARRAY' : $obj->isa('SCALAR') ? 'SCALAR' : die 'Not HASH, ARRAY or SCALAR'; } print type( bless \a ), "\n"; print type( bless [a] ), "\n"; print type( bless {a=>b} ), "\n"; print type( bless \*STDOUT ), "\n\n";


If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

Replies are listed 'Best First'.
Re^2: How do I determine the underlying type of an Object?
by adrianh (Chancellor) on Apr 19, 2003 at 21:46 UTC

    Unfortunately this doesn't work for some edge cases. Consider:

    my $not_a_hash = bless [], 'HASH'; print type($not_a_hash), "\n";
      There are ways around that
      use strict; use warnings; sub better_type { my $obj = shift; my $type = ref $obj; my $orgType = $type; my @ref = qw[ SCALAR ARRAY HASH ] ; #CODE REF GLOB LVALUE ]; if( grep { $_ eq $type } @ref ) { bless $obj, '__schwern'; $type = $obj->isa('HASH') ? 'HASH' : $obj->isa('ARRAY') ? 'ARRAY' : $obj->isa('SCALAR') ? 'SCALAR' : undef; bless $obj, $orgType; # bless it back } return $type; } my @objs = ( bless([],'ary'), bless({},'ash'), bless(sub{'blessed sub HASH'},'HASH'), ); printf "%-30s => %s\n\n", $_ => better_type( $_ ) || 'Not HASH, ARRAY or SCALAR' for @objs; __END__ ary=ARRAY(0x1abf0dc) => ary ash=HASH(0x1ab51b0) => ash HASH=CODE(0x1c0b164) => Not HASH, ARRAY or SCALAR
      But in this version you gotta lookout for read-only variables.


      MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
      I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
      ** The Third rule of perl club is a statement of fact: pod is sexy.

      my $not_a_hash = bless [], 'HASH';

      Anyone blessing something into or using a package named HASH needs to be punished.

      I'll just assume people don't do this. Because they don't.

      Juerd
      - http://juerd.nl/
      - spamcollector_perlmonks@juerd.nl (do not use).
      

        Anyone blessing something into or using a package named HASH needs to be punished.

        Why ;-) The namespaces for packages and Perl's fundamental types are different. If somebody wants to use them they are free to do so. They have sick and strange minds - but it's legal.

        Since there is a good way of finding out the underlying type (reftype) I would prefer to use that instead of something hand rolled that breaks on odd package names, tied variables, packages with overloaded stringification, etc.

      /me nods.

      I noticed your reply with the pointer to the module. ++ for that :)


      If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.