How to get the class name from the reference to an object?

P

Peng Yu

Suppose I new an object. Is there a way to get the class name from the
reference to the object?
 
J

Jens Thoms Toerring

Peng Yu said:
Suppose I new an object. Is there a way to get the class name from the
reference to the object?

The ref() function should return the name of the package of the
object - I guess that's what you mean by "class name".

Regards, Jens
 
D

Dr.Ruud

Peng said:
Suppose I new an object. Is there a way to get the class name from the
reference to the object?


#!/usr/bin/perl -l

use Data::Dumper;

use Scalar::Util qw(
blessed
reftype
);

my $object = bless [], __PACKAGE__;

sub info { Dumper \@_ }

my $i;

print ++$i, ":\t", $_ for
$object->info(),
Dumper ( $object ),
ref ( $object ),
blessed( $object ),
reftype( $object ),
;

__END__
 
C

chad

The ref() function should return the name of the package of the
object - I guess that's what you mean by "class name".

Is there more than one defintion to "class name"?

Chad
 
J

Jens Thoms Toerring

Is there more than one defintion to "class name"?

Well, in Perl I'm more used to the term "package" (and ref()
returns the "package name" for blessed objects according to
the documentation) than "class" - thus I was not 100% sure
what the OP wanted...
Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top