How can I perform introspection in Perl?

后端 未结 2 1222
别跟我提以往
别跟我提以往 2021-01-13 19:08

In the Perl OOP, how can I dump all methods / fields in this class and its parent class.

my ($self) = @_;

I saw a lot of constructors as a

2条回答
  •  温柔的废话
    2021-01-13 19:37

    1. What is @_? See perldoc -v @_

    2. perldoc perlobj

    mjd has an interesting article on introspection in Perl.

    In addition, How do I list available methods on a given object or package in Perl? answers part of your question.

    My preferred answer to that question uses Class::Inspector:

    #!/usr/bin/env perl
    
    use strict; use warnings;
    
    use Class::Inspector;
    use HTML::TokeParser::Simple;
    
    my $methods = Class::Inspector->methods(
        'HTML::TokeParser::Simple', 'full', 'public'
    );
    
    print "Methods:\n";
    print "$_\n" for @$methods;
    
    print "Superclasses\n";
    use Class::ISA;
    print join(", ", Class::ISA::super_path('HTML::TokeParser::Simple')), "\n";
    

提交回复
热议问题