end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

sleep() , select() , Time::HiRes::sleep() による perlのsleep処理

perlのsleepでは、1秒単位のsleepは組込み関数のsleep()、 1秒未満単位のsleepは Time::HiResの sleep() を使用しますが、 select(undef, undef, undef, $sleep_time)でも、Time::HiRes::sleep() と同様の動作をできるらしい。

#!/usr/local/bin/perl
use strict;
use warnings;
use Time::HiRes;


main();

sub main {

    for my $interval (1.5 , 1.0, 0.5){
        my $start_time = Time::HiRes::time();
        # http://perldoc.perl.org/functions/sleep.html
        sleep($interval);
        my $end_time = Time::HiRes::time();
        my $slept_time = $end_time - $start_time;

        print "sleep( $interval )->slept time $slept_time sec\n";
    }
    print "\n";

    for my $interval (1.5 , 1.0, 0.5){
        ## http://perldoc.perl.org/functions/select.html
        ## http://perldoc.jp/func/select
        my $start_time = Time::HiRes::time();
        select(undef, undef, undef, $interval); ## = sleep

        my $end_time = Time::HiRes::time();
        my $slept_time = $end_time - $start_time;

        print
            "select(undef,undef,undef,$interval)->slept time $slept_time sec\n";
    }
    print "\n";

    for my $interval (1.5 , 1.0, 0.5){
        my $start_time = Time::HiRes::time();
        ## http://search.cpan.org/perldoc?Time%3A%3AHiRes
        Time::HiRes::sleep($interval);
        my $end_time = Time::HiRes::time();
        my $slept_time = $end_time - $start_time;

        print
            "Time::HiRes::sleep($interval)->slept time $slept_time sec\n";
    }
}

つまり↑こう書くと、↓こう表示されます

$ ./foo.pl 
sleep( 1.5 )->slept time 1.00030517578125 sec
sleep( 1 )->slept time 1.00028395652771 sec
sleep( 0.5 )->slept time 9.05990600585938e-06 sec

select(undef,undef,undef,1.5)->slept time 1.50168013572693 sec
select(undef,undef,undef,1)->slept time 1.00239300727844 sec
select(undef,undef,undef,0.5)->slept time 0.500741958618164 sec

Time::HiRes::sleep(1.5)->slept time 1.50391101837158 sec
Time::HiRes::sleep(1)->slept time 1.00040507316589 sec
Time::HiRes::sleep(0.5)->slept time 0.500246047973633 sec

が、perl best practice (PBP)では、お作法違反

らしく、↓このように怒られます

$ perlcritic foo.pl 
"select" used to emulate "sleep" at line 26, column 9.  See page 168 of PBP.  (Severity: 5)