How can I dynamically get a list of methods in the Perl library (non Moose)?

How can I dynamically get a list of methods in the Perl library (non Moose)?

I am trying to wrap a non Moose class in a Moose class using delegation. I am currently hard-coding the list of methods that I am delegating. It doesn’t matter, but I was wondering if there is an even more elegant way.

+3
source share
4 answers

impossible. It is impossible to distinguish whether the method is sub or not. If you are fine with the list of subscribers, you can use

grep exists(&{ 'Foo::Bar::'.$_ }), keys %Foo::Bar::

Of course, this does not scan the inheritance tree. If you want to do this, you can get a list of packages to scan using

mro::get_linear_isa('Foo::Bar')
+5
source

Well, strictly speaking, it is impossible to distinguish the subtypes and methods that Ikegov mentioned.

, "PadWalker" peek_sub closed_over, , , , , :

use strict;
use warnings;

use PadWalker;

my $self;

sub test_sub
{
    my $x = shift;
    print $self;
}

sub test_method
{
    my $self = shift;
}

sub is_method
{
    my $coderef = shift;
    my $all = PadWalker::peek_sub($coderef);
    my $closed = PadWalker::closed_over($coderef);
    for my $k (qw($self $me $class)) {
        return 1 if $all->{$k} && !$closed->{$k};
    }
    return 0;
}

print "test_sub: ", is_method(\&test_sub) ?
    "is a method\n" : "is a sub\n";
print "test_method: ", is_method(\&test_method) ?
    "is a method\n" : "is a sub\n";

coderefs .

+4

ikegami Package::Stash. list_all_symbols . :

perl -MPackage::Stash -E "say for Package::Stash->new('Package::Stash')->list_all_symbols('CODE')"
+4

Moose, Class:: MOP. , :: MOP , / . , , .

use Class::MOP;
use Test::More;

my $class = Class::MOP::Class->initialize('Test::More');
my @methods     = $class->get_method_list(); # Only Methods in this Class
my @all_methods = $class->get_all_methods(); # Include all inherited methods

printf "%d functions in Test::More\n", scalar @methods;
printf "%d functions including inheritance\n", scalar @all_methods;

print "methods:     @methods\n\n";
print "all methods: ", join(' ', map({ $_->fully_qualified_name } @all_methods)), "\n";

:

39 functions in Test::More
49 functions including inheritance
methods:     subtest _whoa cmp_ok isa_ok use_ok isnt _format_stack like _eq_array plan _eval fail _carp done_testing _type diag _is_module_name import_extra eq_array require_ok BAIL_OUT _dne is ok _eq_hash explain todo todo_skip unlike new_ok eq_set pass can_ok eq_hash is_deeply note _equal_nonrefs _deep_check skip
all methods: Exporter::export_fail Test::More::_whoa Test::More::subtest Exporter::export_tags Test::More::cmp_ok Test::More::use_ok Test::More::isa_ok Test::More::_format_stack Test::More::isnt Test::More::like Test::More::_eq_array Test::More::plan Exporter::export Test::More::_eval Test::More::_carp Test::More::fail Test::More::done_testing Test::More::_type Test::More::diag Test::Builder::Module::_strip_imports Test::More::_is_module_name Test::More::import_extra Exporter::as_heavy Test::More::eq_array Test::More::require_ok Exporter::export_to_level Test::More::BAIL_OUT Test::More::_dne Test::More::is Test::More::ok Exporter::export_ok_tags Test::More::explain Test::More::_eq_hash Test::More::todo_skip Test::More::todo Test::More::new_ok Test::More::unlike Test::More::pass Test::More::eq_set Test::More::can_ok Test::More::eq_hash Test::More::is_deeply Test::More::note Test::Builder::Module::import Test::More::_equal_nonrefs Test::More::skip Test::More::_deep_check Exporter::require_version Test::Builder::Module::builder
+1

All Articles