Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I perform introspection in Perl?

Tags:

oop

perl

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.

like image 387
user595234 Avatar asked Dec 04 '22 04:12

user595234


2 Answers

  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";
like image 60
Sinan Ünür Avatar answered Jan 05 '23 04:01

Sinan Ünür


Data::Printer is a quick way to get a list of available methods in the current class:

#!/usr/bin/env perl

use strict;
use warnings;
use DateTime;
use Data::Printer;

p( DateTime->now );

This will print something like:

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
    }   
}   
like image 41
oalders Avatar answered Jan 05 '23 02:01

oalders