This software offers high resolution alarm capabilities, as well as sleep functions and interval timers. It also includes gettimeofday features for precise timekeeping.
If your system lacks gettimeofday() or an emulation of it, gettimeofday() or the one-argument form of tv_interval() won't be available. Similarly, the lack of nanosleep(), usleep(), select(), and poll means that Time::HiRes::usleep(), Time::HiRes::nanosleep(), or Time::HiRes::sleep() won't be supported. If your system lacks both ualarm() and setitimer(), then Time::HiRes::ualarm() and Time::HiRes::alarm() won't be available either.
Attempting to import an unimplemented function in the use statement will result in a compile-time error. If your subsecond sleeping is implemented with nanosleep() instead of usleep(), you can mix subsecond sleeping with signals since nanosleep() does not use signals. However, this is not portable, and you should first verify the truth value of &Time::HiRes::d_nanosleep and carefully read your nanosleep() C API documentation for any peculiarities.
If you plan on using nanosleep for purposes other than mixing sleeping with signals, consider whether Perl is the appropriate tool to use for work requiring nanosecond accuracy. It's important to keep in mind that unless you are working on a hard realtime system, any clocks and timers will be imprecise, particularly in a pre-emptive multiuser system. Additionally, understanding the difference between wallclock time and process time (in UNIX-like systems the sum of user and system times) is critical. Attempting to sleep for a certain amount of time will most likely lead to sleeping more than the specified time, but it's possible to sleep slightly less.
The following are examples of how to use Time::HiRes:
- Import the desired functions: use Time::HiRes qw( usleep ualarm gettimeofday tv_interval nanosleep clock_gettime clock_getres clock_nanosleep clock stat );
- Call usleep: usleep($microseconds);
- Call nanosleep: nanosleep($nanoseconds);
- Call ualarm with two arguments: ualarm($microseconds, $interval_microseconds);
- Get the elapsed time in seconds and microseconds: [$seconds, $microseconds] = gettimeofday;
- Compute the elapsed time between two time values: $elapsed = tv_interval ( $t0, [$seconds, $microseconds]); $elapsed = tv_interval ( $t0, [gettimeofday]); $elapsed = tv_interval ( $t0 );
- Import the following functions: use Time::HiRes qw ( time alarm sleep );
- Get the current time as floating point seconds: $now_fractions = time;
- Sleep for a given number of floating-point seconds: sleep ($floating_seconds);
- Set a SIGALRM signal to be delivered to the calling process after a specified time interval: alarm ($floating_seconds);
- Set a SIGALARM signal to be delivered to the calling process repeatedly after a specific time interval: alarm ($floating_seconds, $floating_interval);
- Import the following
Version 1.9719: N/A