fmogavero has asked for the wisdom of the Perl Monks concerning the following question:

If I am working with an object and need to display all the properties and methods, how do I do this?

Replies are listed 'Best First'.
Re: Object properties and Methods
by davorg (Chancellor) on Mar 23, 2001 at 19:55 UTC

    It depends how the object is implemented (which is, of course, something that, as a user of the object, you shouldn't be interested in).

    Assuming that the object is a blessed hash, then you can get to all of its properties using standard hash lookup techniques.

    my $obj = SomeObject->new; foreach (keys %$obj) { print "$_ : $obj->{$_}\n"; }

    Of course, some of the properties may be complex data structures that you'd need to unravel yourself. You might be better off just using Data::Dumper.

    As for methods, you can get a list of the defined methods by examining the object packages symbol table (the Devel::Symdump module is useful for this), but bear in mind that not all methods may be defined. A common trick is to use AUTOLOAD to simulate accessor methods.

    Or perhaps you should just read the published interface for the object :)

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

(tye)Re: Object properties and Methods
by tye (Sage) on Mar 23, 2001 at 19:50 UTC

    You can use the Perl debugger to get to the point where you have the object created and enter the command "m $object" to see the list of methods and what package provides them. Then, if the object is typical, enter the command "x keys %$object" to get the list of properties. Unless the object is wicked (such as those blessed file handles that have become so popular), the command "x $object" will dump the whole object data structure.

    I hope that helps.

            - tye (but my friends call me "Tye")
      Thanks. Can I put "x keys %$object" into a hash. I am creating an object that has properties populated from a database, file or other source. My initial thought was that if I could return a list of properties, I could cycle through it and obtain the data values for each. Listing the methods would be for troubleshooting.
        Perhaps you should have a look at Data::Dumper, to see how to get all the properties.