Help with sleep and file open

B

Bob

I would have expected the code below to sleep for 10 seconds, up to 15
tries, in attempting to open the data file before continuing. Instead
it *always* seems sleep 15 tries before continuing. Can someone please
tell what what I did wrong?

Thanks



..
..
..
$trycount=0;
main:
open(SAV, ">>$filename") || die (&wait_and_try_again);
..
..
..
..
print "Complete";
exit;

sub wait_and_try_again {
$trycount++;
if($trycount<16) { sleep (1); print "Slept: $trycount\n"; goto main;
}
exit;
}
 
B

Brian McCauley

I would have expected the code below to sleep for 10 seconds,

Please post real minimal complete code.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
B

Brad Baxter

I would have expected the code below to sleep for 10 seconds, up to 15
tries, in attempting to open the data file before continuing. Instead
it *always* seems sleep 15 tries before continuing. Can someone please
tell what what I did wrong?

I suspect you do not have permission to write to $filename. With that
code, if you can't open it the first time, waiting is unlikely to help.

Regards,

Brad
 
T

Tad McClellan

Bob said:
I would have expected the code below to sleep for 10 seconds,


If you call sleep() with an argument of 1, it will not sleep for 10 seconds.

up to 15
tries, in attempting to open the data file before continuing. Instead
it *always* seems sleep 15 tries before continuing.


It works for me...

Can someone please
tell what what I did wrong?


Didn't post a short and complete program that we can run that
illustrates your problem.

Didn't indent your code.

Called die() when you didn't want to die().

Used a goto.

Didn't report the value of $!.



Maybe this is what you wanted?

---------------------------------------
my $SAV = my_open($filename) or die "could not open '$filename' $!";
# ...
sub my_open {
my($fname) = @_;
foreach my $trycount ( 1 .. 15 ) {
if ( open my $fh, ">>$fname" ) {
return $fh;
}
sleep (1);
print "Slept: $trycount\n";
}
return undef;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,087
Members
48,773
Latest member
Kaybee

Latest Threads

Top