Skip to content

Instantly share code, notes, and snippets.

@rjattrill
Last active December 20, 2015 10:59
Show Gist options
  • Save rjattrill/6119205 to your computer and use it in GitHub Desktop.
Save rjattrill/6119205 to your computer and use it in GitHub Desktop.
Get methods from Moose object using meta. But doesn't work as expected! See http://stackoverflow.com/questions/910430/how-do-i-list-available-methods-on-a-given-object-or-package-in-perl
use strict;
use warnings;
use v5.10;
use Test::More;
use Try::Tiny;
use Data::Dumper;
use Carp;
use List::Util qw(first);
use Class::Inspector;
use FindBin::libs;
package MyClass {
use Moose;
sub my_method {
print "Hello world";
}
sub my_ok {
return 'ok';
}
}
{
my $test = 'Test';
try {
my $my_class = MyClass->new;
isa_ok($my_class, 'MyClass');
my $expected = 'ok';
my $got = $my_class->my_ok();
is($got, $expected, "Something is working");
# Using Class Inspector
my $ci_methods = Class::Inspector->methods( 'MyClass', 'full', 'public' );
# say "ci_methods: " . Dumper $ci_methods;
ok(defined(first{$_ =~ /my_method/} @$ci_methods), 'Found using Class::Inspector');
my $method_dump = MyClass->meta->dump;
# say "method_dump: $method_dump";
like($method_dump, qr/my_method/, "Found method name in Moose Meta Dump");
# Inspecting the symbol table
no strict 'refs';
my @methods = grep { defined &{$_} } keys %MyClass::;
use strict 'refs';
# say "methods: " . Dumper \@methods;
ok(defined(first {$_ =~ /my_method/} @methods), 'Found using symbol table');
# my $meta = MyClass->meta;
# say "meta: " . Dumper $meta;
# my $method_map = MyClass->meta->get_method_map;
# say "method_map: " . Dumper $method_map;
} catch {
carp($_);
fail("Exection caught in test: $test");
}
}
done_testing();
@rjattrill
Copy link
Author

The only way that I have found that works is this:

use Class::Inspector;
my @ci_methods =   Class::Inspector->methods( 'MyClass', 'full', 'public' );
say "ci_methods: " . Dumper \@ci_methods;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment