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

Discussion in 'Perl Misc' started by Peng Yu, Jun 6, 2010.

  1. Peng Yu

    Peng Yu Guest

    Suppose I new an object. Is there a way to get the class name from the
    reference to the object?
     
    Peng Yu, Jun 6, 2010
    #1
    1. Advertising

  2. Peng Yu

    sreservoir Guest

    On 6/6/2010 6:12 PM, Peng Yu wrote:
    > Suppose I new an object. Is there a way to get the class name from the
    > reference to the object?


    perldoc -fref

    --

    "Six by nine. Forty two."
    "That's it. That's all there is."
    "I always thought something was fundamentally wrong with the universe."
     
    sreservoir, Jun 6, 2010
    #2
    1. Advertising

  3. Peng Yu <> wrote:
    > 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
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Jun 6, 2010
    #3
  4. Peng Yu

    Dr.Ruud Guest

    Peng Yu wrote:

    > 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__


    --
    Ruud
     
    Dr.Ruud, Jun 6, 2010
    #4
  5. Peng Yu

    chad Guest

    On Jun 6, 3:35 pm, (Jens Thoms Toerring) wrote:
    > Peng Yu <> wrote:
    > > 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".
    >


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

    Chad
     
    chad, Jun 9, 2010
    #5
  6. chad <> wrote:
    > On Jun 6, 3:35 pm, (Jens Thoms Toerring) wrote:
    > > Peng Yu <> wrote:
    > > > 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".


    > 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
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Jun 9, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ding feng
    Replies:
    2
    Views:
    2,904
    ding feng
    Jun 25, 2003
  2. Replies:
    10
    Views:
    550
    Jim Langston
    May 25, 2006
  3. Gary Wessle

    object and class name reference

    Gary Wessle, Nov 12, 2006, in forum: C++
    Replies:
    7
    Views:
    343
  4. Replies:
    1
    Views:
    514
    Mark Rae [MVP]
    Sep 20, 2007
  5. tenxian
    Replies:
    7
    Views:
    366
    RedGrittyBrick
    Apr 23, 2008
Loading...

Share This Page