如何在Perl中进行自省? [英] How can I perform introspection in Perl?

查看:66
本文介绍了如何在Perl中进行自省?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Perl OOP中,如何转储此类及其父类中的所有方法/字段.

my ($self) = @_;

我看到了很多如上所述的构造函数.那么,@_是什么?

我有Java/OOP背景.如果您的解释可以遵循Java领域,对我来说将更容易理解.

解决方案

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

这篇关于如何在Perl中进行自省?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆