[Oc-pm] Find all methods of a Perl object

Ramana V Mokkapati mvr707 at gmail.com
Thu Jun 8 14:05:34 PDT 2017


Tony brought up this question last meeting. And I bumped into it today.

Notice that both direct and inherited methods are listed....


[SNIP]

#!/usr/bin/env perl


use Modern::Perl;

use Class::MOP;

# Example 1
use Data::Table;
my $dt = Data::Table->new([[11,12,13],[21,22,23],[31,32,33]], [qw/a b c/]);
my $code_ref; # to find number of rows of an object
for my $m (Class::MOP::Class->initialize('Data::Table')->get_all_methods) {
        print "name = ", $m->name, "\n";
        print "package_name = ", $m->package_name, "\n";
        print "fully_qualified_name = ", $m->fully_qualified_name, "\n";
        print "body = ", $m->body, "\n";
        print "===\n";

        if ($m->name eq "nofRow") {
                $code_ref = $m->body;
        }
}
say "Number of rows = ", $code_ref->($dt);


# Example 2
use WWW::Mechanize;
my $agent = WWW::Mechanize->new();
for my $m
(Class::MOP::Class->initialize('WWW::Mechanize')->get_all_methods) {
        print "name = ", $m->name, "\n";
        print "package_name = ", $m->package_name, "\n";
        print "fully_qualified_name = ", $m->fully_qualified_name, "\n";
        print "body = ", $m->body, "\n";
        print "===\n";
}


[/SNIP]


Reference: http://www.perlmonks.org/?node_id=741707
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/oc-pm/attachments/20170608/d03adcb7/attachment.html>


More information about the Oc-pm mailing list