MKaama
MKaama

Reputation: 1939

How to explore an object in perl?

I have this Perl code:

use HTTP::Daemon;
use Data::Printer;
my $d = HTTP::Daemon->new( 
    LocalHost => "localhost",
    LocalPort => 8080
) || die;
while (my $c = $d->accept) {
    print ref $c;
    print $c;
    print %{*$c};
    p $c;
    print $c->sockhost . "\n"
}

The returned object $c is "HTTP::Daemon::ClientConn=GLOB(0x85543d8)". Original code indicates, there is a sockhost member, but I wonder what other members it might have? None of my printing efforts helped. Even meta::CPAN page is silent, but I want a general solution in code to reveal what $c is. For reference, I have Perl v5.12.4.

Upvotes: 3

Views: 328

Answers (2)

elcaro
elcaro

Reputation: 2307

Data::Printer is much more useful than Data::Dumper. It shows the internal structure of objects including all methods. You will need to install it from CPAN.

use Data::Printer; # or just "use DDP;" for short

my $obj = SomeClass->new;
p($obj);

Which might give you something like:

\ SomeClass  {
    Parents       Moose::Object
    Linear @ISA   SomeClass, Moose::Object
    public methods (3) : bar, foo, meta
    private methods (0)
    internals: {
       _something => 42,
    }
}

Upvotes: 3

Q the Platypus
Q the Platypus

Reputation: 845

HTTP::Daemon documents the methods it supports http://search.cpan.org/~gaas/HTTP-Daemon-6.01/lib/HTTP/Daemon.pm . It also supports all the IO::Socket::INET methods via inheritance.

However on the more general question of how you can examine in general to see what methods a Perl class exposes the answer is you can't. In Perl methods can be dynamically generated at runtime so there is no tool that can examine an object and tell you what methods are supported.

Upvotes: 2

Related Questions