Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

list out all methods of object

by vetrivel (Sexton)
on Jan 25, 2011 at 05:03 UTC ( [id://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 (Saint) 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 )."::" } +) ;
    Output:
    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)."::" }) ; }

      Prints:

      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 (Prior) on Jan 25, 2011 at 11:47 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://884054]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found