2

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.

MKaama
  • 1,732
  • 2
  • 19
  • 28
  • http://perldoc.perl.org/UNIVERSAL.html has some interesting stuff. Like the `can` method. Might be worth looking at autoload too though, which might give an idea why this is hard. – Sobrique Feb 21 '16 at 09:52

2 Answers2

2

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.

Q the Platypus
  • 825
  • 1
  • 7
  • 13
  • Since Perl methods can be dynamically generated, that is exactly why I want a run-time tool. I thought Perl does support introspection, e.g. http://stackoverflow.com/questions/10142530/how-can-i-perform-introspection-in-perl . use Help 'Modulename'; also seems promising. – MKaama Feb 19 '16 at 06:45
  • @MKaama If a method isn't documented then it's not part of the module's public API and it could change at any time. The documentation is the only tool you should be using for this. – ThisSuitIsBlackNot Feb 19 '16 at 14:45
2

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,
    }
}
elcaro
  • 2,227
  • 13
  • 15
  • Closely inspecting my code, you might notice, that I am already using Data::Printer, but it did not reveal any meaningful details. However, Class::Inspector did! You might want to write an answer about that. – MKaama Feb 20 '16 at 00:22