Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Sleep - settle an argument?

by ultranerds (Pilgrim)
on Sep 20, 2012 at 11:56 UTC ( #994639=perlquestion: print w/ replies, xml ) Need Help??
ultranerds has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm trying to prove something to someone about how "sleep" worked in browsers many years ago (2000). Am I correct in thinking that in older version of Perl "sleep" didn't actually work in a Browser as it does in SSH? (i.e it would simply be ignored) ... or is my memory playing tricks on me? =)

Cheers

Andy

Comment on Sleep - settle an argument?
Re: Sleep - settle an argument?
by tobyink (Abbot) on Sep 20, 2012 at 12:56 UTC

    The implementation of sleep hasn't changed much over the years. It works almost exactly the same now as it did back in Perl 4.

    Perl doesn't usually run in web browsers, though it runs on many web servers. sleep "works" in scripts serving up web pages, but it's not usually especially useful - it just makes the web server appear to run slower.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      Hi,

      Ah interesting. Maybe it was a case of me being naive when I was learning perl and writing my first scripts :) I always thought that the sleep command was only to be used in SSH (as it had no effect in the web-browser). Thanks for clarifying - although that now means I've lost a bet :D

      I have to admit - I've never had a reason to use sleep to "slow down" a script running in a web-browser - its only really when doing scripts that would need to leave a little gap to help the server cope with larger processes via cron etc

      Cheers

      Andy
Re: Sleep - settle an argument?
by shmem (Canon) on Sep 20, 2012 at 13:02 UTC
    Am I correct in thinking that in older version of Perl "sleep" didn't actually work in a Browser as it does in SSH?

    No. The perl internal function 'sleep' never worked in a browser running without an embedded perl -i.e any browser. But! Years ago I've seen a perlTk browser, which AFAIK didn't execute perl code retrieved from the web embedded in a HTML document, either.

    I'm not aware that JavaScript ever had a builtin sleep() function or method. I don't know whether VBS did have it, since I avoid(ed) VBS like plague, pest & cholera.

Re: Sleep - settle an argument?
by FloydATC (Hermit) on Sep 20, 2012 at 13:09 UTC
    IIRC, most browsers of old would not start rendering the document until it had been fully transmitted from the server and the connection closed, so a sleep() in a CGI would just make the connection appear to "hang" while unnecessarily eating up resources on the web server.

    From the Perl perspective, the default sleep() can only do integer seconds while Time::HiRes lets you use fractions. Other than that, nothing has changed AFAIK.

    -- Time flies when you don't know what you're doing
      >>IIRC, most browsers of old would not start rendering the document until it had been fully transmitted from the server and the connection closed, so a sleep() in a CGI would just make the connection appear to "hang" while unnecessarily eating up resources on the web server. <<<

      Thanks, that was what I was thinking of - although I could swear than when I used it in a browser run script in my very early years, it simply ignored the sleep and run as normal - but my memory isn't as good as it used to be! (not good for a 28 year old to be saying ;))

Re: Sleep - settle an argument?
by sundialsvc4 (Monsignor) on Sep 20, 2012 at 13:10 UTC
      Thanks for the reply. I think it was actually a basic error I made when I first used it 12 years ago. I very rarely use sleep anyway (as there are very few reasons to do so!), but I was just intrigued as to whether that was ever the case (as someone just told me he wanted to add a "sleep" into one of his scripts that would be run from a web-browser, to which I replied that it wouldn't work - only to be proved wrong <G>)

      Andy
Re: Sleep - settle an argument?
by flexvault (Parson) on Sep 20, 2012 at 19:21 UTC

    ultranerds,

    I have used the Javascript 'setTimeout()' function to do something similar to 'sleep()'.

    Works Great.

    "Well done is better than well said." - Benjamin Franklin

      I've used setTimeout to fix "multithreading" bugs in non-web JS using apps. The screen wouldn't draw while the script ran, the GUI elements created, the options they were created with (text field) were all blank on reads even though I passed an initial value when I created the GUI element. Doing a setTimeout allowed the GUI objects to set themselves up.

        bulk88,

        Great! I answered to let other monks know that there was something similar to sleep() in JS.

        I use it to update a digital clock on a CRM package. Twice a second, it updates the clock. Before I used the setTimeout, the user's PC was totally overloaded with calls to the clock update routine and there was no cpu power left for other things (like input) :-)

        Thanks...Ed

        "Well done is better than well said." - Benjamin Franklin

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994639]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (14)
As of 2014-08-27 15:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (242 votes), past polls