Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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 (Canon) 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
[erix]: hm zipfile-SQLite sounds useful, have you tried it already, Corion?
[erix]: and good morning (sorry :))
[Corion]: erix: Naah, I just saw the announcement and remembered that discussion. Maybe I'll try building a DBD::SQLite with the zip stuff built-in, just for fun
[Corion]: Oh - and a good morning to you too :)

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2018-01-24 08:55 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (256 votes). Check out past polls.