Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
go ahead... be a heretic
 
PerlMonks  

perl's sleep function

by jesuashok (Curate)
on Nov 08, 2005 at 09:01 UTC ( #506658=perlquestion: print w/ replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

could any one explain me how the sleep function workes as follows :-

#!/usr/local/bin/perl + sleep 86_400;
how sleep understands 86_400.

Need your valuable assistance. Thanks a lot
"Keep pouring your ideas"

Comment on perl's sleep function
Download Code
Re: perl's sleep function
by l.frankline (Hermit) on Nov 08, 2005 at 09:16 UTC
    Hi,

    sleep EXPR

    sleep causes the program to sleep for EXPR seconds, or if EXPR is not specified, to sleep indefinitely. sleep can be interrupted using the SIGALRM signal. It returns the number of seconds actually slept.

    regards

    Franklin

    Edit: g0n - corrected code tags

Re: perl's sleep function
by McDarren (Abbot) on Nov 08, 2005 at 09:16 UTC
    perldoc -f sleep

    Have you tried that?
Re: perl's sleep function
by Zaxo (Archbishop) on Nov 08, 2005 at 09:19 UTC

    sleep sets a timer for its argument's number of seconds. It returns the number of seconds slept because it may be interrupted by another signal. To be careful, do,

    my $timeout = 86400; $timeout -= sleep $timeout while $timeout > 0;
    Unsafe to mix with alarm on some platforms.

    After Compline,
    Zaxo

Re: perl's sleep function
by tirwhan (Abbot) on Nov 08, 2005 at 09:22 UTC

    I think perhaps your confusion was with the number format. You can use the underscore as a noop separator for numbers. So while 86400 might be written as 86.400 in some locales and 86,400 in others, in a perl script you can write 86_400. This is supposed to make really long numbers easier to parse for humans.


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Re: perl's sleep function
by muntfish (Chaplain) on Nov 08, 2005 at 09:24 UTC

    sleep sleeps for that many seconds (although it can be interrupted). 86400 is the number of seconds in a day. So it sleeps for a day.


    s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&
Re: perl's sleep function
by virtualsue (Vicar) on Nov 08, 2005 at 10:22 UTC
    As tirwhan says, 86_400 is the same as 86400. Perl allows '_' in numbers, and it can appear anywhere in the number. Perl strips out the '_' characters in a number before using it. It is purely cosmetic for ease of reading.
    perl -e 'print 123_0_33_876,"\n"'
    Copy and paste the above line for an instant demonstration. (If you're on Windows, you may need to change the quoting slightly)
Re: perl's sleep function
by blazar (Canon) on Nov 08, 2005 at 10:40 UTC
    It understands it precisely as perldoc -f sleep explains. Now, is there anything you couldn't understand in it?!? Or is it the underscore, what that bothers you? If so, then it has nothing to do with sleep itself. It's just a number literal, and you can put underscores in them to improve readability at your will.
    $ perl -le 'print for 1_000_000, 0x1_000_000' 1000000 16777216
      What is all the ill-natured yelling about? Maybe you need to ease up on the caffeine. I think that our new friend jesuashok deserves patient encouragement rather than abuse. If you can't reply in a reasonable way, please don't reply at all.

        Well, if you ask my opinion, you're overestimating the ill-nature-yellingness of my post. OTOH judging from the way it was downvoted it may be that I'm underestimating it instead. This may well have to be with the fact that I'm not a native English speaker, and my English is far from being perfect. Whatever, the fact that the XP of my reply is going up and down and is currently zero may suggest that approximately 50% of the people reading it agree with me.

        Indeed I cannot negate that I wanted to be slightly harsh in jesuashok's respects, since I consider this to be a valid educational aid - that is: I did not only yell at him, but also gave actual pieces of information pointing him to the relevant portions of the documentation. But in no way did I want to be disrespectful to him by being overly harsh, and if I conveyed that impression I apologize to him directly.

      why are so many of the responses here ill-tempered and unhelpful?


      I started out programming in Perl a dozen years ago. At the time I thought I just wasn't getting "it" because I couldn't understand a lot of the documentation.


      And the comments on the board were always
      "WTF??!?!RTFM?!?!?!"



      I've grown, learned, matured. And the process, I've had a lot of experience with different languages and different open source programming communities.


      Still, I love perl.


      Yet whenever I encounter comments like this, with all the extraneous punctuation and the condescending phrasing -- well it makes me first of all glad that I studied women in college instead of CompSci and secondly, ashamed to be associated with such backbiting geeks.


      Now please, you people, you know who you are, GTFU.
        Its from Nov 08, 2005, don't bring back old bull.

        I personally believe that the negative overall reputation of this very old node of mine clearly shows that by general consensus it actually was too harsh. In some sense, I apologize for that, and I believe my manners got better since. I still consider the actual question to be both overly stupid and overly stupidly asked, proof which can be the even more negative reputation. Incidentally I did give some actual advice in my reply as well. This, and the person who posted the question, later turned out to be an especially bad member of this community: while it is true that (as the sig of one of our fellow monks claims) one should "examine what is said, not who speaks," knowing who the OP was sheds some light on the annoying carelessness of his question.

        If it is of any comfort to you, clpmisc is generally considered to be an even more ill mannered place than PM. Yet I've learnt Perl more there than from any other resource. The subject matter could be discussed ad nauseam, and it often is: to be blindly gentle, no matter what, or to occasionally slap in the face the arrogant newbie, as an educational aid? Who knows...

        BTW: I can't understand what you mean with "extraneous punctuation," what is it supposed to be?

        --
        If you can't understand the incipit, then please check the IPB Campaign.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://506658]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2014-04-20 17:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls