Re: How do I determine the underlying type of an Object?
by adrianh (Chancellor) on Apr 19, 2003 at 21:35 UTC
|
| [reply] [d/l] |
|
I believe you are right! Thank you! 8-))
| [reply] |
Re: How do I determine the underlying type of an Object?
by Coruscate (Sexton) on Apr 19, 2003 at 21:36 UTC
|
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.
| [reply] [d/l] |
|
my $not_a_hash = bless [], 'HASH';
print type($not_a_hash), "\n";
| [reply] [d/l] |
|
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.
|
| [reply] [d/l] |
|
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).
| [reply] |
|
|
|
|
/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.
| [reply] |
Re: How do I determine the underlying type of an Object?
by Abigail-II (Bishop) on Apr 19, 2003 at 23:58 UTC
|
my $obj = Module -> new;
my ($type) = "$obj" =~ /=(\w+)[(]0x[0-9A-Fa-f]+[)]$/;
Abigail | [reply] [d/l] |
|
| [reply] |
|
Oh, but you can always first rebless the object.
Abigail
| [reply] |
|
Re: How do I determine the underlying type of an Object?
by mitd (Curate) on Apr 19, 2003 at 22:12 UTC
|
Your question is a little confusing (at least to me).
my $obj = Some::Class->new();
returns a blessed reference so ref ($obj) returns the name
of the class into which the ref was blessed.
UPDATE: please ignore was having an acid flashback!
Your second examples are incorrect isa() does not answer the question is this object this data type. IT answers the question is the object an instance of class foo
either directly or by way of inheritance.
Assumes the isa() you refer to is UNIVERSAL->isa().
HASH, ARRAY and SCALAR are NOT Classes they are data types.
That being said, is want you really want to determine the 'underlying' data type of the blessed reference ??
If so why not just create a 'type' method in the class.
mitd-Made in the Dark
I've always been astonished by the absurd turns
rivers have to make to flow under every bridge.
| [reply] [d/l] |
Re: How do I determine the underlying type of an Object?
by jaa (Friar) on Apr 20, 2003 at 13:53 UTC
|
Thanks very much folks, I appreciate your responses!
Jeff
| [reply] |