[Cascavel-pm] Reflexão em Perl

Daniel de Oliveira Mantovani mantovani em perl.org.br
Segunda Dezembro 6 21:36:10 PST 2010


Ops, esqueci de colocar o link do manual do Moose,
http://search.cpan.org/~drolsky/Moose-1.21/lib/Moose/Manual.pod

Só para ficar mais claro, você pode usar toda a magia do Moose e ainda
abusar de reflexão e introspecção, com Class::Mop.

2010/12/7 Daniel de Oliveira Mantovani <mantovani em perl.org.br>:
> Perl é arte, você pode usar toda a magia do Moose e abusar de reflexão
> e introspecção:
>
> use Some::Class;
> use Class::MOP;
>
> my $meta = Class::MOP::Class->initialize('Some::Class');
>
> for my $meth ( $meta->get_all_methods ) {
>    print $meth->fully_qualified_name, "\n":
> }
>
>
> 2010/12/7 Daniel de Oliveira Mantovani <mantovani em perl.org.br>:
>> 2010/12/7 Tom Mostard <capmostarda em gmail.com>:
>>> Colegas,
>>>
>>> Alguém sabe como carregar uma classe que não foi carregada com "use" através
>>> de reflexão computacional?
>>
>> package Foo;
>>
>> sub new { return bless {}, shift }
>> sub method { my $self = shift; print "Work!\n" }
>>
>> 1;
>>
>> package main;
>>
>> my ( $class, $method ) = ( 'Foo', 'method' );
>> $class->new->$method;
>>
>>
>> __END__
>>
>>
>> Outro exemplo, http://nopaste.snit.ch/26735
>>
>>> Sei que Perl suporta, mas não estou conseguindo implementar.
>>>
>>> Exemplo:
>>>
>>> my $class  = "Foo";
>>> my $method = "hello";
>>> my $object = $class->new();
>>> $object->$method(); // que e o mesmo que $object->hello();
>>>
>>> Implementei a classe Foo, e salvei no mesmo diretório como Foo.pm
>>> Preciso usar o EXPORT ou coisa assim?
>>
>> Não, você não precisa do Export. Você pode usar "do" ou  "require".
>>
>> package main;
>> use strict;
>> use warnings;
>>
>> my %cm = (qw/Foo read Bar people Baz should/);
>>
>> foreach my $class ( ( sort keys %cm ) ) {
>>
>>    require $class;
>>    my $method = $cm{$class};
>>
>>    $class->new->$method;
>>
>> }
>>
>> Uma das utilidades do "require", é quando você precisa carregar um
>> módulo em tempo de execução, que é o seu caso.
>> Na verdade, tanto "use" como "require" usam o "do". Ambos são uma abstração.
>> http://perldoc.perl.org/functions/do.html
>>
>>
>>
>>>
>>> O erro que tenho é o seguinte:
>>>
>>> Can't locate object method "new" via package "Foo" (perhaps you forgot to
>>> load "Foo"?) at ./main.pl line 10.
>>>
>>> Mas se eu uso o "use Foo" funciona.
>>>
>>> Abraços,
>>>
>>> Tom
>>> _______________________________________________
>>> Cascavel-pm mailing list
>>> Cascavel-pm em pm.org
>>> http://mail.pm.org/mailman/listinfo/cascavel-pm
>>>
>>
>>
>>
>> --
>> "If you’ve never written anything thoughtful, then you’ve never had
>> any difficult, important, or interesting thoughts. That’s the secret:
>> people who don’t write, are people who don’t think."
>>
>
>
>
> --
> "If you’ve never written anything thoughtful, then you’ve never had
> any difficult, important, or interesting thoughts. That’s the secret:
> people who don’t write, are people who don’t think."
>



-- 
"If you’ve never written anything thoughtful, then you’ve never had
any difficult, important, or interesting thoughts. That’s the secret:
people who don’t write, are people who don’t think."


Mais detalhes sobre a lista de discussão Cascavel-pm