perlquestion
Anonymous Monk
Hi
<p>I am new to Perl and have just written a script that reads text files from a directory and then sends the data on through a socket connection. It's a very simple process but is very cpu intensive so I suspect my loop and file handling is very flawed. Could someone take a little look at this main loop for me and comment. thanks a lot</p>
<p>the process is 'daemonized' first before entering main loop</p>
<code>
opendir DH, "/tmp/store" or die "could not open directory: $!";
while (1) {
while($_ = readdir(DH) ) {
next if $_ eq "." or $_ eq "..";
my @data = split /_/;
$mobile = $data[1];
undef @data;
chomp $mobile;
s/^\s+//, s/\s+$//, s/\s\s+/ / for $mobile;
open FH,"/tmp/sms/$_" or die "could not open: $_ $!";
undef $/;
my $data = <FH>;
chomp $data;
my @fields = split;
my $appcode = $fields[0];
my $message = $data;
s/^\s+//, s/\s+$//, s/\s\s+/ / for $data;
close FH;
sendtolive($mobile,$message); # Sends data through socket connection
unlink("/tmp/sms/$_"); # remove file once read
}
}
closedir DH;</code>
<p><small>20031118 Edit by [Corion]: Added CODE tags, changed title from "cpu intensive"</small></p>
19