且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Perl中进行自省?

更新时间:2023-11-17 14:06:47

  1. 什么是@_?请参见 perldoc -v @_

  2. perldoc perlobj

mjd撰写了一篇有关Perl内省的有趣文章.. /p>

此外, 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";

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 above. Then, what is @_?

I have Java / OOP background. If your explanation can follow Java domain, it will be much easier for me to understand.

  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";