padawan_linuxero has asked for the wisdom of the Perl Monks concerning the following question:
#!Perl $^W = 1; use strict; use File::Copy; use Date::Parse; use Date::Format; use Tk::DialogBox; use Tk::BrowseEntry; use Tk::ResizeButton; use Tk::ProgressBar; use Tk::LabFrame; use Tk::LabEntry; use Tk::ROText; use Tk::HList; use Cwd; use Tk; #Optional Modules# if ($^O eq 'MSWin32') { eval { require Win32::Console; Win32::Console::Free() }; if ($@) { warn "Win32::Console is not installed.\n$@"; } } #Declarations# my $VERSION = 2.7; my $loadhistory = 0; my $sort_cnt = 3; my ($ftp, $port, $after_id,); my $cwd = cwd; #Main# open STDERR, ">PFTPc.log" or warn "Cannot create PFTPc.log\a\n$!"; my $mw = MainWindow->new(-relief => 'raised', -bd => 2,); $mw->title("Asociacion de Agentes Aduanales de Matamoros --- Valida +cion Automatica"); $mw->geometry("1024x764+4+25"); &list_dir_enviar($mw); &list_dir_enviados($mw); &list_dir_respuesta($mw); $mw->Button(-text => "Salir", -command => sub { exit })->pack(-side => 'bottom'); &Tk::MainLoop(); ############################################################## sub list_dir_enviar { # my $mw = MainWindow->new; my $box = $mw->Listbox( -relief => 'sunken', -height => 5, -setgrid => 0, ); ########## Inicio de Directory Listen ############################ my $pollingInterval = 1; my $directoryToMonitor = "C:/test"; my %oldFileList = (); my %currentFileList = (); # we start by getting the file list, considered to be the "old" list # getFileList($directoryToMonitor, \%oldFileList); for (;;) { sleep($pollingInterval); # get the current file list and compare it to the old file list # if the lists differ, the current file list becomes the old file +list # and we FTP the files # getFileList($directoryToMonitor, \%currentFileList); if ( fileListsDiffer(\%oldFileList, \%currentFileList) ) { copyFileList(\%oldFileList, \%currentFileList); #dumpFileList(\%oldFileList); my @items = glob "c:/test/m*.*"; foreach (@items) { $box->insert('end', $_); } my $scroll = $mw->Scrollbar(-command => ['yview', $box]); $box->configure(-yscrollcommand => ['set', $scroll]); $box->pack(-side => 'left', -fill => 'both', -expand => 1); $scroll->pack(-side => 'right', -fill => 'y'); } # the FTP commands should go here instead of the dumpFileList() +function above } } # Basically prints the contents of a hash. # sub dumpFileList { my $list = shift; my @k = sort(keys(%$list)); for (my $i=0; $i<=$#k; $i++) { print $k[$i] . " " . $list->{$k[$i]} . "\n"; } print "\n"; } # Get a list of the files along with there modification times # from the specified directory. The results are place in the # specified hash where the key is the file name and the value # is that file's modification time. # sub getFileList { my $directory = shift; my $hash = shift; %$hash = (); opendir(DIR,$directory) || die "Can not open $directory"; my @files = grep(!/^\.\.?$/, readdir(DIR)); closedir(DIR); for (my $i=0; $i<=$#files; $i++) { $hash->{$files[$i]} = (stat("$directory/$files[$i]"))[9]; } } # See if two hashes of files differ. The hashes are considered differ +ent if one of the # following occurrs: # # 1. The hashes have different number of elements. # 2. The new hash is missing a key that is in the old hash. # 3. For a given key, the value in the old hash differs from the val +ue in the new hash. # sub fileListsDiffer { my $oldList = shift; my $newList = shift; my @oldKeys = (keys(%$oldList)); my @newKeys = (keys(%$newList)); if ($#newKeys != $#oldKeys) { return 1; } foreach my $key (@oldKeys) { if ( ! defined($newList->{$key}) || $newList->{$key} ne $oldList-> +{$key} ) { return 1; } } return 0; } # Copies one hash to another. The destination has ($to below) is empt +ied first. # sub copyFileList { my $to = shift; my $from = shift; %$to = (); foreach my $key (keys(%$from)) { $to->{$key} = $from->{$key}; } } ############ Final de Directory Listen ############################ ################################################################## sub list_dir_enviados { # my $mw = MainWindow->new; my $box1 = $mw->Listbox( -relief => 'sunken', -height => 5, -setgrid => 0, ); my @items1 = glob "c:/AAAvalida/valida/enviados/*.*"; #my @items = qw(One Two Three Four Five Six Seven Eight Nine Ten Eleve +n Twelve); foreach (@items1) { $box1->insert('end', $_); } my $scroll1 = $mw->Scrollbar(-command => ['yview', $box1]); $box1->configure(-yscrollcommand => ['set', $scroll1]); $box1->pack(-side => 'left', -fill => 'both', -expand => 1); $scroll1->pack(-side => 'right', -fill => 'y'); } ################################################################### sub list_dir_respuesta { # my $mw = MainWindow->new; my $box2 = $mw->Listbox( -relief => 'sunken', -height => 5, -setgrid => 0, ); my @items2 = glob "c:/AAAvalida/valida/respuestas/*.*"; #my @items = qw(One Two Three Four Five Six Seven Eight Nine Ten Eleve +n Twelve); foreach (@items2) { $box2->insert('end', $_); } my $scroll2 = $mw->Scrollbar(-command => ['yview', $box2]); $box2->configure(-yscrollcommand => ['set', $scroll2]); $box2->pack(-side => 'left', -fill => 'both', -expand => 1); $scroll2->pack(-side => 'right', -fill => 'y'); } ###################################################################### +###
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Need help with program using Perl Tk
by TGI (Parson) on May 05, 2008 at 19:50 UTC | |
|
Re: Need help with program using Perl Tk
by zentara (Cardinal) on May 05, 2008 at 20:02 UTC | |
|
Re: Need help with program using Perl Tk
by thundergnat (Deacon) on May 06, 2008 at 19:30 UTC |