matthias krull
matthias krull

Reputation: 4429

how to update meta information of inherited moose classes?

I dont know if i asked that question the right way but i will try to explain.

I have a base class MyClass.pm:

use MooseX::Declare;

class MyClass {
    method test_it {
        for (__PACKAGE__->meta->get_all_methods){
            print $_->name . "\n";
        }
    }
}

And another class MyOtherClass.pm:

use MooseX::Declare;

class MyOtherClass extends MyClass {
    method one {
        return 1;
    }

    method two {
        return 1;
    }

    method three {
        return 1;
    }
}

And a script use_it.pl:

#!/usr/bin/perl

use strict;
use warnings;

use MyClass;
use MyOtherClass;

my $class = MyOtherClass->new;
my $otherclass = MyOtherClass->new;

print "MyClass can:\n";
$class->test_it;

print "MyOtherClass can:\n";
$otherclass->test_it;

exit 0;

Output is:

MyClass can:
dump
DEMOLISHALL
meta
does
new
DESTROY
BUILDALL
BUILDARGS
test_it
DOES
MyOtherClass can:
dump
DEMOLISHALL
meta
does
new
DESTROY
BUILDALL
BUILDARGS
test_it
DOES

So if i call test_it on MyClass the output contains as expected "test_it" alongside with some build in methods. Calling test_it on MyOtherClass produces the same output with one, two and three missing.

How can i get a list of the methods that contains all methods of the inheriting class?

Upvotes: 1

Views: 123

Answers (1)

Stevan Little
Stevan Little

Reputation: 389

You want $self->meta->get_all_methods, not __PACKAGE__->meta->get_all_methods. __PACKAGE__ is bound by Perl at compile time, so it will always be MyClass.

Upvotes: 4

Related Questions