moen has asked for the wisdom of the Perl Monks concerning the following question:

I got bored..really bored, so i made this little thingy to look at. perl -e 'for ($i = 0; print $i, " seconds bored..\n"; $i++, sleep(1)){}' ok, that's fine. It give me 1 2 3 and so one seconds bored. Then i got even a bit more bored, so i move sleep(1) further in: perl -e 'for ($i = 0; print $i, " seconds bored..\n", sleep(1); $i++){}' and it gives me: 0 is the number.. 11 is the number.. 12 is the number.. 13 is the number.. etc.. 19 is the number.. 110 is the number.. 111 is the number.. 112 is the number. etc.. Why?? I can't seem to figure that out. Of course am not bored any more, but there's a limit even there :)

Replies are listed 'Best First'.
(jcwren) Re: Can't seem to figure this out
by jcwren (Prior) on Sep 14, 2000 at 02:20 UTC
    sleep() returns the number of seconds slept. print "hello", "test", "is", "a", "test" will print "hellothisisatest". So, since sleep() is returning a value, it's getting printed as part of the print statement, since print supports comma separated arguments. What you're seeing is the '1' that sleep(1) is returning being printed after the '\n' from the preceeding line.

    --Chris

    e-mail jcwren
      ahh..thank you a million. You just made my day :)
RE: Can't seem to figure this out
by Anonymous Monk on Sep 14, 2000 at 23:13 UTC
    'sleep' becomes part of the 'print' returning the number of seconds delayed (which gets 'printed'). Try 'sleep(2)' ... 0, 21, 22, ... 8-)