thanks, all useful.
I originally did have the close handler inside the main loop but changed it to see if that helped things. I have also experimented with sleeping for a second before reading again and does sort it out. It has helped me to have you talk me through it though. With regards Update 2, just a typo from a version change. Update 1, I'll check into that, thanks.
| [reply] |
No problem, I like to have one exit point also, so I put this little bit together to illustrate the point... This is UNTESTED though..
#! /usr/bin/perl
use strict;
use warnings;
my $rc = 0;
while (1) {
if (opendir DH, "/tmp/store") {
if (readdir(DH) ) {
next if $_ eq "." or $_ eq "..";
my @data = split /_/;
my $mobile = $data[1];
undef @data;
chomp $mobile;
s/^\s+//, s/\s+$//, s/\s\s+/ / for $mobile;
if (open FH,"/tmp/store/$_") {
undef $/;
my $data = <FH>;
chomp $data;
my @fields = split;
my $appcode = $fields[0];
my $message = $data;
#Assuming you want to clean up the data that is being
+sent to sendtolive()
s/^\s+//, s/\s+$//, s/\s\s+/ / for $message;
close FH;
# Sends data through socket connection assuming the se
+ndtolive returns a success or fail
# you can test for it and only delete if it is success
+ful. Do you want to delete the file if it cant send the sms..?
if (sendtolive($mobile,$message)) {
# remove file once read
unlink("/tmp/store/$_");
}
else {
print "Count NOT sent $message: sendtolive failed"
+;
}
}
else {
print "could not open: $_ $!";
$rc = 3;
last;
}
}
else {
print "Cannot read dir /tmp/store: $!\n";
$rc = 2;
last;
}
#You have finished with this file loop onto the next.
closedir (DH);
sleep(1);
}
else {
print "Faile to open dir /tmp/store: $!\n";
$rc = 1;
last;
}
}
exit ($rc);
-----
Of all the things I've lost in my life, its my mind I miss the most.
| [reply] [d/l] |
looks good to me, thanks a lot.
what are the conventions for returning values from subroutines in Perl? For example if I want to indicate all is not well should I return 1 or 0 or false or undef ??
Jon
| [reply] |