Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

list out all methods of object

by vetrivel (Sexton)
on Jan 25, 2011 at 05:03 UTC ( #884054=perlquestion: print w/replies, xml ) Need Help??
vetrivel has asked for the wisdom of the Perl Monks concerning the following question:

Hai , How to list out all methods of particular object . I am able to list out class methods, not able to list out object methods

Example to list out class methods
use Time::localtime; $current_Time = localtime ; print "CLASS METHOD IS " . Dumper( \%Time::localtime:: ) ;
is there any way to list out method using object ( $current_Time )

Replies are listed 'Best First'.
Re: list out all methods of object
by GrandFather (Sage) on Jan 25, 2011 at 05:30 UTC

    Out of the box Perl doesn't do object introspection. Class wrappers like Moose provide introspection as part of their implementation, but Perl's built in object support is much more primitive than that.

    It may be that whatever you want to use introspection for can be done in another way - maybe you could tell us why you want to be able to do this?

    True laziness is hard work
Re: list out all methods of object
by NetWallah (Abbot) on Jan 25, 2011 at 05:37 UTC
    How about:
    print "Instance METHOD IS " . Dumper( \%{ref ($current_Time )."::" } +) ;
    Instance METHOD IS $VAR1 = { 'hour' => *Time::tm::hour, 'AUTOLOAD' => *Time::tm::AUTOLOAD, 'struct' => *Time::tm::struct, 'yday' => *Time::tm::yday, 'mon' => *Time::tm::mon, 'BEGIN' => *Time::tm::BEGIN, 'new' => *Time::tm::new, 'mday' => *Time::tm::mday, 'wday' => *Time::tm::wday, 'min' => *Time::tm::min, 'isdst' => *Time::tm::isdst, 'DESTROY' => *Time::tm::DESTROY, 'croak' => *Time::tm::croak, 'import' => *Time::tm::import, 'confess' => *Time::tm::confess, 'sec' => *Time::tm::sec, 'carp' => *Time::tm::carp, 'ISA' => *Time::tm::ISA, 'VERSION' => *Time::tm::VERSION, 'year' => *Time::tm::year };

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

      That may not help the OP much as it doesn't actually provide a list of methods in the sense that the word 'method' is usually used (a 'function' called with reference to an object instance). Consider the following trivial class with no methods in what I am claiming is the conventional sense:

      use warnings; use strict; use Data::Dumper; package test; sub new { my ($class) = @_; return bless {}, $class; } sub Package { return __PACKAGE__; } package main; my $obj = test->new (); { no strict 'refs'; print "Instance METHOD IS " . Dumper( \%{ref ($obj)."::" }) ; }


      Instance METHOD IS $VAR1 = { 'AUTOLOAD' => *test::AUTOLOAD, 'DESTROY' => *test::DESTROY, 'new' => *test::new, 'Package' => *test::Package };

      'AUTOLOAD' and 'DESTROY' are inherited methods. 'new' is a class method and 'Package' is a function in the 'test' package. If you add base classes the results are even more confusing with the ref trick only showing 'methods' in the base classes that have been called already.

      True laziness is hard work
        Your argument applies equally to the OP's original example, so I believe my solution is up to the OP's expectations.

        Thanks for the insight on inheritance (++). Always interesting when you learn from doing things wrong.

             Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Re: list out all methods of object
by Arunbear (Parson) on Jan 25, 2011 at 11:47 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://884054]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2017-08-18 01:21 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (294 votes). Check out past polls.