[SP-pm] Melhor forma de escrever
Pedro Anisio
pedroanisio at gmail.com
Sun Feb 8 14:18:58 PST 2009
Senhores,
Qual a forma mais otimizada de escrever isso?
minha apliccao passa por varios diretorios de maneira recursiva
for my $eachFile (glob($source_str.'*')) {
...
}
e eu desejo armazenar apenas o ultimo diretorio em uma hash de controle,
para tanto estou criando em cada iteracao um array temporario e dando um
split e pegando o ultimo elemento, acho meio ineficiente
my @tmp = split("/",$eachFile);
$result_data_hash_pointer->{$dir_count_int}->{name_str} = $tmp[-1];
Alguem tem alguma dica?
estou enviando o script anexo.
--
Abs,
Pedro Anisio
-------------- Pr?xima Parte ----------
Um anexo em HTML foi limpo...
URL: <http://mail.pm.org/pipermail/saopaulo-pm/attachments/20090208/506961c0/attachment.html>
-------------- Pr?xima Parte ----------
#!/usr/bin/perl -w
use strict;
#### META INFO
# Autor: Pedro Anisio
# Email: pedroanisio em gmail.com
# Data: 04/Fev/2009
# Description: SyncFiles between two folders
# Tags: Sync, WebDav, Directory
#
####
use Data::Dumper;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use File::Util;
use Data::Serializer;
$| = 1;
sub createDirHash {
my($source_str) = @_;
my $deep_count_int = 0;
my ($result_hash, $obj, $serialized);
$result_hash = recurse($source_str,$deep_count_int);
use Data::Serializer;
$obj = Data::Serializer->new();
$obj = Data::Serializer->new(
serializer => 'Storable',
digester => 'MD5',
cipher => 'DES',
secret => 'my secret',
compress => 1,
);
$serialized = $obj->serialize($result_hash);
#print Dumper($result_hash);
print "DIR: $result_hash->{control}->{dir_count}\n";
#print "FILE: $result_hash->{control}->{file_count}\n";
#print "TOTAL_SIZE: ".$result_hash->{control}->{total_size_sum}/1024;
for(0..$result_hash->{control}->{dir_count}) {
#print $_."\n";
print "[$_][$result_hash->{raw_data}->{$_}->{name_str}]\n";
}
}
sub recurse {
#### META INFO
# $source_str -> String contendo o nome do diretório que será varrido;
# $deep_count_int -> Interiro, indica qual a profundidade desse diretório em relação ao root
# $my_parent_str -> MD5 do diretório "PAI" do diretório atual
# $result_hash_pointer -> referência para a HASH de resultados, só é chamado dessa forma quando é recursivo
# $result_data_hash_pointer -> referência para a HASH de dados
# $current_dir_id_str -> String com o MD5 do dire´torio atual;
# %result_hash -> HASH que dá origem ao ponteiro do conteiner do resultado;
# %result_data -> HASH que dá origem ao ponteiro do conteiner de dados;
#### META INFO
# $result_hash{control}{dir_count} = int
# $result_hash{control}{file_count} = int
# $result_hash{control}{error_stack} = str
# $result_hash{raw_data} = data
# $result_data->{$current_dir_id_str}->{name_str} = $source_str;
# $result_data->{$current_dir_id_str}->{deep_int} = $deep_count_int;
# $result_data->{$current_dir_id_str}->{parent} = $my_parent_str ;
# $result_data->{$current_dir_id_str}->{dir_count} = $dir_count_int ;
# $result_data->{$current_dir_id_str}->{file_count}->{$dir_count_int} = $eachFile;
# $result_data->{$current_dir_id_str}->{file_count}->{$dir_count_int}-{create_time}
my($source_str,$deep_count_int,$my_parent_int,$result_hash_pointer) = @_;
my($current_dir_id_str);
my(%result_hash,%result_data,$result_data_hash_pointer,$dir_count_int);
$deep_count_int++;
#$current_dir_id_str = md5_hex($source_str);
$my_parent_int = $my_parent_int || "1";
$result_hash{control}{dir_count} = 1;
$result_hash_pointer = $result_hash_pointer || \%result_hash;
$result_data_hash_pointer = \%{$result_hash_pointer->{raw_data}} || \%result_data;
my($f) = File::Util->new();
#$result_hash_pointer->{$current_dir_id_str}->{name_str} = $source_str;
#$result_hash_pointer->{$current_dir_id_str}->{deep_int} = $deep_count_int;
#$result_hash_pointer->{$current_dir_id_str}->{parent} = $my_parent_str ;
#$result_data_hash_pointer->{$current_dir_id_str}->{name_str} = $source_str;
#$result_data_hash_pointer->{$current_dir_id_str}->{deep_int} = $deep_count_int;
#$result_data_hash_pointer->{$current_dir_id_str}->{parent} = $my_parent_str ;
## append a trailing / if it's not there
$source_str .= '/' if($source_str !~ /\/$/);
unless (defined($result_data_hash_pointer->{0}->{name_str})) {
#print "OI";
$result_data_hash_pointer->{0}->{name_str} = $source_str;
#sleep(4);
}
## loop through the files contained in the directory
for my $eachFile (glob($source_str.'*')) {
## if the file is a directory
if( -d $eachFile) {
## pass the directory to the routine ( recursion )
$dir_count_int = $result_hash_pointer->{control}->{dir_count}++;
#$result_data{$current_dir_id_str}{directory_str}{$dir_count_int} = $eachFile;
#$result_data_hash_pointer->{$current_dir_id_str}->{dir_count} = $dir_count_int ;
#$result_data_hash_pointer->{$dir_count_int}->{md5_hash} = $current_dir_id_str;
my @tmp = split("/",$eachFile);
$result_data_hash_pointer->{$dir_count_int}->{name_str} = $tmp[-1];
$result_data_hash_pointer->{$dir_count_int}->{deep_int} = $deep_count_int;
$result_data_hash_pointer->{$dir_count_int}->{parent} = $my_parent_int;
recurse($eachFile,$deep_count_int,$dir_count_int,$result_hash_pointer);
} else {
#my $file_count_int = $result_hash_pointer->{control}->{file_count}++;
#$result_data_hash_pointer->{$current_dir_id_str}->{file_count}->{$file_count_int} = $eachFile;
#$result_data_hash_pointer->{$current_dir_id_str}->{file_count}->{$file_count_int}->{create_time} = $f->created($eachFile);
#print "Created ".$f->created($eachFile)."\n";
#print "Modified ".$f->last_modified($eachFile)."\n";
#print "Size ".$f->size($eachFile)."\n";
#my $file_size_int = $f->size($eachFile) || 0;
#$result_hash_pointer->{control}->{total_size_sum}+=$file_size_int;
#print "Created ".$f->created($eachFile)."\n";
#$result_data{$current_dir_id_str}{file_str}{$file_count_int} = $eachFile;
}
}
$result_hash_pointer->{raw_data} = $result_data_hash_pointer;
return ($result_hash_pointer);
}
#createDirHash("E:\\");
createDirHash("/Volumes/ESCUDEIRO");
####METAINFO
sub SyncDir {
#
# source hash
# destination hash
#
#
# syncronization type
# -> left right
# -> right left
# control hash (resume)
}
#$result_data->{0}->{md5_hash} = $current_dir_id_str;
#$result_data->{0}->{name_str} = "E:/"
#$result_data->{0}->{deep_int} = 0
#$result_data->{0}->{parent} = root
#$result_data->{0}->{}
More information about the SaoPaulo-pm
mailing list