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

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

my $not_a_hash = bless [], 'HASH'; print type($not_a_hash), "\n";

Replies are listed 'Best First'.
Re: Re: Re: How do I determine the underlying type of an Object?
by PodMaster (Abbot) on Apr 20, 2003 at 09:28 UTC
    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.

Re: Re: Re: How do I determine the underlying type of an Object?
by Juerd (Abbot) on Apr 20, 2003 at 11:21 UTC

    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.

        If somebody wants to use them they are free to do so.

        Yes, and that somebody would be a fool for doing so.

        but it's legal.

        Legal, but stupid. Just like using symbolic references instead of real ones is legal, but stupid. And many other things that are both possible and to be avoided.

        there is a good way of finding out the underlying type (reftype)

        Scalar::Util is quite heavy. I know Perl hackers are supposed to not care about memory usage, but I do think pulling in 400 kB is a bit too much to work around potential problems that in practice don't exist.

        I'm willing to sacrifice memory for speed. Not to work around unlikely, theoretical problems :)

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

Re^3: How do I determine the underlying type of an Object?
by Coruscate (Sexton) on Apr 19, 2003 at 21:49 UTC

    /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.