在 Perl OOP 中,如何转储此类及其父类中的所有方法/字段。
my ($self) = @_;
我看到了很多上面的构造函数。那么,什么是@_
?
我有 Java / OOP 背景。如果您的解释可以遵循Java领域,我会更容易理解。
是什么@_
?见perldoc -v @_
此外,如何在 Perl 中列出给定对象或包的可用方法?回答你的部分问题。
我对该问题的首选答案使用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";
Data::Printer 是一种快速获取当前类中可用方法列表的方法:
#!/usr/bin/env perl
use strict;
use warnings;
use DateTime;
use Data::Printer;
p( DateTime->now );
这将打印如下内容:
DateTime {
public methods (134) : add, add_duration, am_or_pm, bootstrap, ce_year, christian_era, clone, compare, compare_ignore_floating, date, datetime, day, day_abbr, day_name, day_of_month, day_of_month_0, day_of_quarter, day_of_quarter_0, day_of_week, day_of_week_0, day_of_year, day_of_year_0, day_0, DefaultLanguage, DefaultLocale, delta_days, delta_md, delta_ms, dmy, doq, doq_0, dow, dow_0, doy, doy_0, duration_class, epoch, era, era_abbr, era_name, format_cldr, formatter, fractional_second, from_day_of_year, from_epoch, from_object, hires_epoch, hms, hour, hour_1, hour_12, hour_12_0, INFINITY, is_dst, is_finite, is_infinite, is_leap_year, iso8601, jd, language, last_day_of_month, leap_seconds, local_day_of_week, local_rd_as_seconds, local_rd_values, locale, MAX_NANOSECONDS, mday, mday_0, mdy, microsecond, millisecond, min, minute, mjd, mon, mon_0, month, month_abbr, month_name, month_0, NAN, nanosecond, NEG_INFINITY, new, now, offset, quarter, quarter_abbr, quarter_name, quarter_0, sec, second, SECONDS_PER_DAY, secular_era, set, set_day, set_formatter, set_hour, set_locale, set_minute, set_month, set_nanosecond, set_second, set_time_zone, set_year, STORABLE_freeze, STORABLE_thaw, strftime, subtract, subtract_datetime, subtract_datetime_absolute, subtract_duration, time, time_zone, time_zone_long_name, time_zone_short_name, today, truncate, utc_rd_as_seconds, utc_rd_values, utc_year, wday, wday_0, week, week_number, week_of_month, week_year, weekday_of_month, year, year_with_christian_era, year_with_era, year_with_secular_era, ymd
private methods (38) : _accumulated_leap_seconds, _add_overload, _adjust_for_positive_difference, _calc_local_components, _calc_local_rd, _calc_utc_components, _calc_utc_rd, _cldr_pattern, _compare, _compare_overload, _day_has_leap_second, _day_length, _era_index, _format_nanosecs, _handle_offset_modifier, _is_leap_year, _month_length, _new, _new_from_self, _normalize_leap_seconds, _normalize_nanoseconds, _normalize_seconds, _normalize_tai_seconds, _offset_for_local_datetime, _rd2ymd, _seconds_as_components, _space_padded_string, _string_compare_overload, _string_equals_overload, _string_not_equals_overload, _stringify, _subtract_overload, _time_as_seconds, _utc_hms, _utc_ymd, _weeks_in_year, _ymd2rd, _zero_padded_number
internals: {
formatter undef,
local_c {
day 13,
day_of_quarter 13,
day_of_week 5,
day_of_year 104,
hour 15,
minute 5,
month 4,
quarter 2,
second 16,
year 2012
},
local_rd_days 734606,
local_rd_secs 54316,
locale DateTime::Locale::en_US,
offset_modifier 0,
rd_nanosecs 0,
tz DateTime::TimeZone::UTC,
utc_rd_days 734606,
utc_rd_secs 54316,
utc_year 2013
}
}