Re: Calling class methods from a metaclass
[email protected] (Stevan Little)
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <[email protected]> |
Dan,
Moose does not attempt to "fix" the way Perl handles class methods.
Since $myclass is an instance of the metaclass for the MyClass
package, you can simple call:
$myclass->name->hello_class()
Where ->name returns the package name string, which is what Perl 5
considers the "class".
- Stevan
On Jun 13, 2008, at 12:45 PM, Dan Harbin wrote:
> The following:
>
> #!/usr/bin/env perl
> use strict;
> use warnings;
>
> package MyClass;
> {
> use Moose;
>
> sub hello_class {
> my $class = shift;
> return "Hello $class\n";
> }
> }
>
> print "1: ", MyClass->hello_class(), "\n";
>
> my $myclass = Class::MOP::get_metaclass_by_name('MyClass');
>
> print "2: ", $myclass->hello_class(), "\n";
>
>
> Gives:
>
> 1: Hello MyClass
>
> Can't locate object method "hello_class" via package
> "Moose::Meta::Class" at ./example.pl line 19.
>
>
> Is there any way to call a class method when I don't know the package
> name at runtime?
>
> Thanks,
> Dan