[SP-pm] Dúvida com o módulo File::Find

Thiago Glauco Sanchez thiagoglauco at ticursos.net
Tue Jul 13 13:27:20 PDT 2010


Flávio, acho que você não entendeu direito a documentação do módulo:


    The wanted function
    <http://search.cpan.org/%7Ejesse/perl-5.12.1/lib/File/Find.pm#___top>

The |wanted()| function does whatever verifications you want on each 
file and directory. Note that despite its name, the |wanted()| function 
is a generic callback function, and does *not* tell File::Find if a file 
is "wanted" or not. In fact, its return value is ignored.

The wanted function takes no arguments but rather does its work through 
a collection of variable|:
|

|||File::Find::dir| is the current directory name,


|$_| is the current filename within that director||
|File::Find::name| is the complete pathname to the file.

The above variables have all been localized and may be changed without 
effecting data outside of the wanted function.

For example, when examining the file //some/path/foo.ext/ you will have:

     $File::Find::dir  = /some/path/
     $_                = foo.ext
     $File::Find::name = /some/path/foo.ext


Então... você não vai fazer um loop com $_ e sim
sub busca{
$gpg->decrypt(
ciphertext => " $_ ",
output => " $_ ",
recipient => "0D59E2D1",
passphrase => $secret
);

$_ não é um array então esse bloco não executa se você fizer foreach ($_).

O file::find vai mandar para a sua função todos os arquivos 1 por vez, 
não precisa de loop.

Em 13/07/2010 14:09, Alexei Znamensky escreveu:
> vou ver se implemento um modulo chamado "Portugues" que ira apontador 
> erros crassos na lingua materna, como por exemplo "desemcripta". Como 
> diria a tia da 1a. série: "m só antes de p ou b"
>
> 2010/7/13 Giuliani Sanches <giulianisanches em gmail.com 
> <mailto:giulianisanches em gmail.com>>
>
>     Flávio,
>
>     troca o seu $diretorios por:
>
>     @diretorios = ('meu_diretorio/')
>
>     Dentro da sua sub busca, você não precisa fazer um foreach pois a
>     função find automaticamente percorre o array passado e
>     disponibilizando o path dentro do callback que você passou, na
>     variável $_.
>
>     []'s
>
>     2010/7/13 "Flávio R. Lopes" <flavio.lopes em links.inf.br
>     <mailto:flavio.lopes em links.inf.br>>:
>     > Olá Alexei!
>     > Ok, vou dar uma lida novamente!
>     >
>     > De qualquer forma, abaixo segue o erro:
>     >
>     > #############
>     > Use of uninitialized value $cmd in pattern match (m//) at
>     > /usr/local/lib/perl5/site_perl/5.12.1/GnuPG.pm line 736, <GEN0>
>     line 1.
>     > Use of uninitialized value $cmd in concatenation (.) or string at
>     > /usr/local/lib/perl5/site_perl/5.12.1/GnuPG.pm line 736, <GEN0>
>     line 1.
>     > protocol error: expected ENC_TO got :
>     >  at /usr/local/lib/perl5/site_perl/5.12.1/GnuPG.pm line 154
>     >     GnuPG::abort_gnupg('GnuPG=HASH(0x176b220)', 'protocol error:
>     expected
>     > ENC_TO got : \x{a}') called at
>     > /usr/local/lib/perl5/site_perl/5.12.1/GnuPG.pm line 736
>     >     GnuPG::decrypt_postwrite('GnuPG=HASH(0x176b220)',
>     'ciphertext', ' . ',
>     > 'output', ' . ', 'recipient', '0D59E2D1', 'passphrase',
>     > '123-minha-senha-321', ...) called at
>     > /usr/local/lib/perl5/site_perl/5.12.1/GnuPG.pm line 725
>     >     GnuPG::decrypt('GnuPG=HASH(0x176b220)', 'ciphertext', ' . ',
>     'output', '
>     > . ', 'recipient', '0D59E2D1', 'passphrase',
>     '123-minha-senha-321', ...)
>     > called at /home/flavio/workspace/Ferramentas/Desemcripta.pl line 23
>     >     main::busca() called at
>     /usr/local/lib/perl5/5.12.1/File/Find.pm line
>     > 854
>     >     File::Find::_find_dir('HASH(0x1781b90)',
>     '/home/flavio/teste_cripto', 5)
>     > called at /usr/local/lib/perl5/5.12.1/File/Find.pm line 722
>     >     File::Find::_find_opt('HASH(0x1781b90)',
>     '/home/flavio/teste_cripto')
>     > called at /usr/local/lib/perl5/5.12.1/File/Find.pm line 1297
>     > #############
>     >
>     > Abraço!
>     >
>     > Em 13-07-2010 13:44, Alexei Znamensky escreveu:
>     >
>     > Se você não mostra a mensagem de erro, fica bem difícil de
>     alguém te ajudar.
>     > De cara, apesar de não causar (à primeira vista) um problema na
>     execução,
>     > você ter um laço do tipo foreach($_) { .. } dentro das suas subs
>     é algo
>     > desnecessário e indicativo de que você não entende direito o que
>     está
>     > fazendo.
>     > Sugiro ler novamente, com mais cudado desta vez, a documentação do
>     > File::Find.
>     >
>     > []s,
>     > Russian
>     > 2010/7/13 "Flávio R. Lopes" <flavio.lopes em links.inf.br
>     <mailto:flavio.lopes em links.inf.br>>
>     >>
>     >> Pessoal, ainda não consegui fazer funcionar o programa para
>     >> descriptografar os arquivos!
>     >> Alguém poderia me dar uma ajudar?
>     >>
>     >> Abraço,
>     >> Flávio
>     >>
>     >> Em 12-07-2010 20:40, "Flávio R. Lopes" escreveu:
>     >>>
>     >>> Boa noite pessoal!
>     >>>
>     >>> Fiz um programa (Encripta.pl) para encryptar recursivamente os
>     arquivos
>     >>> que ficam debaixo de uma árvore de um determinado diretório.
>     >>> E o outro programa para desencriptar (Desencripta.pl) os
>     arquivos que
>     >>> foram encriptados pelo programa anterior.
>     >>>
>     >>> O programa para encriptar está funcionando:
>     >>>
>     >>> Encripta.pl:
>     >>>
>     >>> #!/usr/bin/perl
>     >>>
>     >>> use strict;
>     >>> use warnings;
>     >>> use 5.012;
>     >>>
>     >>> use File::Find;
>     >>> use GnuPG qw( :algo );
>     >>>
>     >>> my $gpg = new GnuPG();
>     >>>
>     >>> my $diretorios = "/home/flavio/teste_cripto"; ## Aqui ainda
>     pretendo
>     >>> criar um array para "armazenar" uma lista com mais diretórios
>     >>>
>     >>>
>     >>>
>     >>> my $secret = "123-minha-senha-321"; ## Senha utilizada para
>     criação da
>     >>> Chave
>     >>> my $arquivos;
>     >>>
>     >>> find( \&busca, $diretorios );
>     >>>
>     >>> sub busca {
>     >>>
>     >>> foreach ($_) {
>     >>>
>     >>> $gpg->encrypt(
>     >>> plaintext => "$_",
>     >>> output => "$_.gpg",
>     >>> armor => 1,
>     >>> sign => 1,
>     >>> recipient => "0D59E2D1", ## Minha Chave
>     >>> passphrase => $secret
>     >>> );
>     >>>
>     >>> # say $_;
>     >>>
>     >>> }
>     >>>
>     >>> # unlink $_ if /\.gpg$/;
>     >>>
>     >>> # print "$arquivos\n" if -f $arquivos;
>     >>>
>     >>> }
>     >>>
>     >>>
>     >>> O meu problema é para desencriptar os dados. Eu rodo o
>     programa abaixo
>     >>> (Desencripta.pl) e dá um erro que não estou conseguindo entender:
>     >>>
>     >>> Desencripta.pl:
>     >>>
>     >>> #!/usr/bin/perl
>     >>>
>     >>> use strict;
>     >>> use warnings;
>     >>> use 5.012;
>     >>>
>     >>> use File::Find;
>     >>> use GnuPG qw( :algo );
>     >>>
>     >>> my $gpg = new GnuPG();
>     >>>
>     >>> my $diretorios = "/home/flavio/teste_cripto";
>     >>>
>     >>> my $secret = "123-minha-senha-321";
>     >>> my $arquivos;
>     >>>
>     >>> find( \&busca, $diretorios );
>     >>>
>     >>> sub busca {
>     >>>
>     >>> foreach ($_) {
>     >>> $gpg->decrypt(
>     >>> ciphertext => " $_ ",
>     >>> output => " $_ ",
>     >>> recipient => "0D59E2D1",
>     >>> passphrase => $secret
>     >>> );
>     >>>
>     >>> ## say $_; --> Aqui eu dei um "say" nesta variável para
>     verificar se
>     >>> realmente estava trazendo os nomes dos arquivos e o
>     >>> gozado é que é que ela lista todos os arquivos que foram
>     >>> encriptados, mas mesmo assim dá um erro quando rodo o
>     >>> programa
>     >>>
>     >>>
>     >>>
>     >>>  }
>     >>> }
>     >>>
>     >>> O que está errado no meu programa?
>     >>>
>     >>> Abraço,
>     >>> Flávio
>     >>> _______________________________________________
>     >>> SaoPaulo-pm mailing list
>     >>> SaoPaulo-pm em pm.org <mailto:SaoPaulo-pm em pm.org>
>     >>> http://mail.pm.org/mailman/listinfo/saopaulo-pm
>     >>>
>     >>
>     >> _______________________________________________
>     >> SaoPaulo-pm mailing list
>     >> SaoPaulo-pm em pm.org <mailto:SaoPaulo-pm em pm.org>
>     >> http://mail.pm.org/mailman/listinfo/saopaulo-pm
>     >
>     >
>     >
>     > --
>     > Alexei Znamensky [russoz_gmail_com] [russoz.wordpress.com
>     <http://russoz.wordpress.com>]
>     > [www.flickr.com/photos/alexeiz
>     <http://www.flickr.com/photos/alexeiz>]
>     > "Though we live in trying times, we're the ones who have to try"
>     >
>     > _______________________________________________
>     > SaoPaulo-pm mailing list
>     > SaoPaulo-pm em pm.org <mailto:SaoPaulo-pm em pm.org>
>     > http://mail.pm.org/mailman/listinfo/saopaulo-pm
>     >
>     > _______________________________________________
>     > SaoPaulo-pm mailing list
>     > SaoPaulo-pm em pm.org <mailto:SaoPaulo-pm em pm.org>
>     > http://mail.pm.org/mailman/listinfo/saopaulo-pm
>     >
>
>
>
>     --
>     Giuliani Sanches
>     twitter.com/giulianisanches <http://twitter.com/giulianisanches>
>     giulianisanches.blogspot.com <http://giulianisanches.blogspot.com>
>
>     Como elaborar uma pergunta:
>     http://www.istf.com.br/perguntas/
>     _______________________________________________
>     SaoPaulo-pm mailing list
>     SaoPaulo-pm em pm.org <mailto:SaoPaulo-pm em pm.org>
>     http://mail.pm.org/mailman/listinfo/saopaulo-pm
>
>
>
>
> -- 
> Alexei Znamensky [russoz_gmail_com] [russoz.wordpress.com 
> <http://russoz.wordpress.com>] [www.flickr.com/photos/alexeiz 
> <http://www.flickr.com/photos/alexeiz>]
> "Though we live in trying times, we're the ones who have to try"
>
>
> _______________________________________________
> SaoPaulo-pm mailing list
> SaoPaulo-pm em pm.org
> http://mail.pm.org/mailman/listinfo/saopaulo-pm


-- 
What is the sound of Perl? Is it not the sound of a wall that people have
stopped banging their heads against?
—Larry Wall

Thiago Glauco Sanchez
Intrutor Perl e Redes
www.ticursos.net



More information about the SaoPaulo-pm mailing list