As was previously mentioned, sleep() will be a problem while using Tk unless they are very short. After or repeat is the way to go. You might also want to think about abstracting out some of your repeated code for the various directories you are monitoring.

Loosely based on your script (with large chunks ripped out)

#!usr/bin/perl use strict; use warnings; use Tk; my @directories = ( 'c:/test', 'c:/test/more', 'c:/test/less' ); my ( %object, %files ); my $interval = 5; #seconds my $mw = MainWindow->new( -relief => 'raised', -bd => 2, -title => "Directory Monitor" ); $mw->geometry("800x600+4+25"); my $head_frame = $mw->Frame()->pack; my $main_frame = $mw->Frame()->pack( -fill => 'both', -expand => 1, ); $head_frame->Button( -text => "Salir", -command => sub { exit } )->pack; setup($_) for @directories; $mw->repeat( $interval * 1000, sub { monitor(@directories) } ); MainLoop; sub setup { my $dir = shift; $object{$dir}{frame} = $main_frame->Frame()->pack( -side => 'left', -fill => 'both', -expand => 1, -padx => 2, ); $object{$dir}{message} = $object{$dir}{frame}->Label( -text => ' ' + )->pack; $object{$dir}{label} = $object{$dir}{frame}->Label( -text => $di +r )->pack; $object{$dir}{listbox} = $object{$dir}{frame}->Scrolled( 'Listbox', -scrollbars => 'ose', -relief => 'sunken', -height => 5, -setgrid => 0, )->pack( -fill => 'both', -expand => 1 ); my ( $ok, @files ) = get_file_list($dir); if ($ok) { $files{$dir}{$_} = ( stat("$dir/$_") )[9], $object{$dir}{listbox}->insert( 'end', $_ ) for @files; } else { $object{$dir}{message} ->configure( -text => '*** COULD NOT READ DIRECTORY ***' ); } } sub monitor { my @directories = @_; for my $dir (@directories) { my @differences = file_lists_differ($dir); if (@differences) { $object{$dir}{message}->configure( -text => join "\n", @differences ); } } } sub get_file_list { my $directory = shift; opendir( DIR, $directory ) or return 0; my @files = grep( !/^\.\.?$/, readdir(DIR) ); closedir(DIR); return 1, @files; } sub file_lists_differ { my $dir = shift; my ( %new_files, %old_files, @modified ); $object{$dir}{message}->configure( -text => ' ' ); my ( $ok, @files ) = get_file_list($dir); if ($ok) { $new_files{$_} = ( stat("$dir/$_") )[9] for @files; %old_files = %{ $files{$dir} }; for ( keys %new_files ) { if ( defined $old_files{$_} ) { if ( $new_files{$_} eq $old_files{$_} ) { delete $old_files{$_}; next; } push @modified, "$_ modified"; delete $old_files{$_}; } else { push @modified, "$_ added"; } } for ( keys %old_files ) { push @modified, "$_ deleted"; } %{ $files{$dir} } = %new_files; } else { $object{$dir}{message} ->configure( -text => '*** COULD NOT READ DIRECTORY ***' ); } my @view = $object{$dir}{listbox}->yview; $object{$dir}{listbox}->delete( 0, 'end' ); $object{$dir}{listbox}->insert( 'end', $_ ) for @files; $object{$dir}{listbox}->yviewMoveto( $view[0] ); return @modified; }

In reply to Re: Need help with program using Perl Tk by thundergnat
in thread Need help with program using Perl Tk by padawan_linuxero

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.