Help with sleep and file open

Discussion in 'Perl Misc' started by Bob, Feb 2, 2004.

  1. Bob

    Bob Guest

    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;
    }
    Bob, Feb 2, 2004
    #1
    1. Advertising

  2. (Bob) writes:

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


    Please post real minimal complete code.

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley, Feb 2, 2004
    #2
    1. Advertising

  3. Bob

    Brad Baxter Guest

    On Mon, 2 Feb 2004, Bob wrote:

    > 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
    Brad Baxter, Feb 2, 2004
    #3
  4. Bob <> wrote:

    > 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;
    }
    ---------------------------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Feb 2, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Meir Rotfleisch
    Replies:
    1
    Views:
    391
    Rocky Moore
    Nov 14, 2003
  2. Gonzalo Moreno
    Replies:
    2
    Views:
    12,370
  3. Sam Roberts
    Replies:
    16
    Views:
    360
    Daniel Berger
    Feb 18, 2005
  4. Schüle Daniel

    File::open and File.open

    Schüle Daniel, Oct 23, 2006, in forum: Ruby
    Replies:
    4
    Views:
    178
    Rick DeNatale
    Oct 24, 2006
  5. Richard
    Replies:
    7
    Views:
    212
    Richard
    May 22, 2007
Loading...

Share This Page