#! perl -slw
use strict;
use threads;
use threads::shared;
$|=1;
my $data : shared;
my $flag : shared = 0;
sub watchit {
my $file = shift;
while( sleep 1 ) {
if( -e $file ) {
lock $data;
open IN, '<', $file or die $!;
$data = do{ local $/; <IN> };
close IN;
unlink $file;
lock $flag;
$flag = 1;
}
}
}
threads->create( \&watchit, $ARGV[ 0 ] )->detach;
my $n = 0;
while( 1 ) {
if( $flag ) {
lock $data;
print "\nNew file\n'$data'\n\n";
lock $flag;
$flag = 0;
$n = 0;
}
else {
printf "\rNothing happened : %d", $n++;
}
select( undef, undef, undef, .1 );
}
__END__
P:\test>396897 junk
Nothing happened : 355
New file
'the file 'junk' contains this junk
'
Nothing happened : 4103
New file
'the file 'junk' contains this junk
'
Nothing happened : 62Terminating on signal SIGINT(2)
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
| [reply] [d/l] |
It is not clear why you need threads to do this, more detail might be in order. perlthrtut would be a good point to start for info on threads.
my $file = '/path/to/some/file';
while(1) {
if ( -e $file ) {
# read file
# do stuff
# unlink $file; # otherwise we will keep finding it
}
sleep 1;
}
If you want to use simple code like this you will want to daemonize it. There is a example of daemonizing on my scratchpad
| [reply] [d/l] |
sub check_file {
my ($fpath, $arrp) = @_;
my $max = 15;
my $handle;
while (!-e $fpath || !open($handle, $fpath)) {
return if (!$max--); sleep(1);
}
$$arrp = join('', <$handle>);
close($handle);
}
| [reply] [d/l] |
Neither have I, and my version of Perl doesn't support threading, but here's some code that might work:
use threads;
use strict;
my @fpaths = ('fpath1', 'fpath2');
my %fdata;
foreach (@fpaths) {
my $data;
my $child = threads->new(\&check_file, $_, $fdata{$_} = \$data);
}
sub check_file {
my ($fpath, $arrp) = @_;
my $handle;
while (!-e $fpath || !open($handle, $fpath)) { sleep(1); }
$$arrp = join('', <$handle>);
close($handle);
}
I got my info on threading from:
http://migo.sixbit.org/papers/Perl_Threads/slide-index.html | [reply] [d/l] |
Totally unrelated, how did my first post manage to show up underneath my second post?
| [reply] |
It didn't. This is called a threaded view (parent/child). By default its chronological. Example
perlquestion
- 1st reply to perlquestion
- 1st reply to 1st reply to perlquestion
- 2nd reply to perlquestion
- 1st reply to 2nd reply to perlquestion
- reply 3
- reply 4
It doesn't matter if 1st reply to 1st reply to perlquestion got created after 2nd reply to perlquestion, its not a reply to perlquestion.
| MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!" | | I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README). | | ** The third rule of perl club is a statement of fact: pod is sexy. |
| [reply] [d/l] |