#!/usr/bin/perl -Tw use strict; use Term::ReadKey; # Directory to check my $data_dir='/path/to/directory'; # Seconds between directory checks my $poll_wait='5'; TEST:{ # Process any incoming files if (check_directory()){ process_files(); redo TEST; } else{ # Otherwise check for any key press while waiting my $counter=0; ReadMode('cbreak'); CHECK:{ my ($char); # Check if the user pressed a key if (defined ($char = ReadKey(-1))){ ReadMode('normal'); exit(); } # Is it time to check the incoming directory? elsif($counter >= $poll_wait){ $counter=0; ReadMode('normal'); redo TEST; } # Sleep a bit before checking the kyb again. else{ sleep(1); $counter++; redo CHECK; } } } } # We should never get here, but this restarts the loop if we do redo TEST; sub check_directory{ print "\nChecking directory...\n"; opendir DIR, "$data_dir" or die "Problems opening $data_dir: $!"; while ( my $dir = readdir DIR ) { next if ( $dir =~ m/^\.\.?$/ ); closedir DIR; return 1; } closedir DIR; print "Directory Empty\nPress A Key to Stop\n"; return 0; } sub process_files{ # do somthing like... print "The directory has something!\n"; exit(); }