[Cascavel-pm] Reflexão em Perl

Daniel de Oliveira Mantovani mantovani em perl.org.br
Terça Dezembro 7 10:02:47 PST 2010


Você tem um arquivo chamado Foo.pm no mesmo diretório do script.pl que
está fazendo o require ?

2010/12/7 Tom Mostard <capmostarda em gmail.com>:
>
> Daniel,
> Obrigado pela ajuda! Mas aqui continuar sem solução.
>
> package Foo;
>
> sub new { return bless {}, shift }
> sub method { my $self = shift; print "Work!\n" }
>
> 1;
>
>
> package main;
>
> my ( $class, $method ) = ( 'Foo', 'method' );
> # require $class;
> $class->new->$method;
>
>
> Quando uso o require acima, tenho a seguinte mensagem:
> Can't locate Foo in @INC (@INC contains: /etc/perl
> /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5
> /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10
> /usr/local/lib/site_perl .) at ./main.pl line 6.
>
> Se eu comento o "require", tenho a seguinte mensagem:
> Can't locate object method "new" via package "Foo" (perhaps you forgot to
> load "Foo"?) at ./main.pl line 7.
>
> Será que preciso instalar algum módulo? Ainda não tentei o Class::MOP nem o
> Moose. Queria resolver isso sem usá-los, se for possível.
> Se uso as duas classes num único arquivo, funciona, mas minha intenção é
> usar uma classe externa, que pode variar conforme a necessidade.
>
> Mais uma vez, obrigado.
>
> Tom
>
> Em 7 de dezembro de 2010 02:38, Daniel de Oliveira Mantovani
> <mantovani em perl.org.br> escreveu:
>>
>> Abusando da lista, e mandando 1 milhão de e-mail na mesma thread, vou
>> deixar só a última recomendação,
>> http://en.wikipedia.org/wiki/Metaprogramming
>>
>> Eu gostaria de falar muito mais sobre reflexão e introspecção, mas
>> preciso trabalhar.
>>
>> Boa noite.
>>
>> 2010/12/7 Daniel de Oliveira Mantovani <mantovani em perl.org.br>:
>> > 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."
>> >
>>
>>
>>
>> --
>> "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."
>> _______________________________________________
>> Cascavel-pm mailing list
>> Cascavel-pm em pm.org
>> http://mail.pm.org/mailman/listinfo/cascavel-pm
>
>
> _______________________________________________
> 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."


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