Skip to content

Commit cf3d0fc

Browse files
committed
Time::HiRes::sleep: use (;$) prototype, like CORE::sleep
Time::HiRes::sleep is supposed to be a drop-in replacement for CORE::sleep, but before this patch Time::HiRes::sleep had no prototype, leading to the following incompatibilities: CORE::sleep(1, "foo", "bar"); # syntax error Time::HiRes::sleep(1, "foo", "bar"); # OK, ignores all but 1st argument my @t = 42; CORE::sleep @t; # sleeps for one second (number of elements in @t) Time::HiRes::sleep @t; # sleeps for 42 seconds (@t in list context) CORE::sleep 1, next if $foo; # if $foo, then sleep for one second and start next loop iteration Time::HiRes::sleep 1, next if $foo; # parses as: Time::HiRes::sleep(1, next) if $foo; # if $foo, then start next loop iteration; no delay Fixes #23628.
1 parent b017f8c commit cf3d0fc

File tree

6 files changed

+27
-19
lines changed

6 files changed

+27
-19
lines changed

dist/Time-HiRes/Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Revision history for the Perl extension Time::HiRes.
1919
build failures with MSVC.
2020
- don't try to suppress C++ compatibility warnings in C++ builds, since
2121
that warns.
22+
- Fix sleep's prototype to match CORE::sleep (;$).
2223

2324
1.9764 [2020-08-10]
2425
- Fix a bunch of repeated-word typos

dist/Time-HiRes/HiRes.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ our @EXPORT_OK = qw (usleep sleep ualarm alarm gettimeofday time tv_interval
5050
stat lstat utime
5151
);
5252

53-
our $VERSION = '1.9778';
53+
our $VERSION = '1.9779';
5454
our $XS_VERSION = $VERSION;
5555
$VERSION = eval $VERSION;
5656

dist/Time-HiRes/HiRes.xs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,7 @@ nanosleep(nsec)
10191019

10201020
NV
10211021
sleep(...)
1022+
PROTOTYPE: ;$
10221023
PREINIT:
10231024
struct timeval Ta, Tb;
10241025
CODE:

dist/Time-HiRes/t/alarm.t

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use Config;
1111
my $limit = 0.25; # 25% is acceptable slosh for testing timers
1212

1313
my $xdefine = '';
14-
if (open(XDEFINE, "<", "xdefine")) {
15-
chomp($xdefine = <XDEFINE> || "");
16-
close(XDEFINE);
14+
if (open(my $fh, "<", "xdefine")) {
15+
chomp($xdefine = <$fh> || "");
16+
close($fh);
1717
}
1818

1919
my $can_subsecond_alarm =
@@ -224,5 +224,3 @@ SKIP: {
224224
ok $got == 0 or print("# $got\n");
225225
}
226226
}
227-
228-
1;

dist/Time-HiRes/t/sleep.t

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
use strict;
22

3-
use Test::More tests => 4;
3+
use Test::More tests => 5;
44
BEGIN { push @INC, '.' }
55
use t::Watchdog;
66

77
BEGIN { require_ok "Time::HiRes"; }
88

99
use Config;
1010

11+
SKIP: {
12+
skip "no hi-res sleep", 1 unless defined &Time::HiRes::sleep;
13+
is prototype(\&Time::HiRes::sleep), prototype('CORE::sleep'),
14+
"Time::HiRes::sleep's prototype matches CORE::sleep's";
15+
}
16+
17+
eval { Time::HiRes::sleep(-1) };
18+
like $@, qr/::sleep\(-1\): negative time not invented yet/,
19+
"negative time error";
20+
1121
my $xdefine = '';
12-
if (open(XDEFINE, "<", "xdefine")) {
13-
chomp($xdefine = <XDEFINE> || "");
14-
close(XDEFINE);
22+
if (open(my $fh, "<", "xdefine")) {
23+
chomp($xdefine = <$fh> || "");
24+
close($fh);
1525
}
1626

1727
my $can_subsecond_alarm =
@@ -20,10 +30,6 @@ my $can_subsecond_alarm =
2030
defined &Time::HiRes::usleep &&
2131
($Config{d_ualarm} || $xdefine =~ /-DHAS_UALARM/);
2232

23-
eval { Time::HiRes::sleep(-1) };
24-
like $@, qr/::sleep\(-1\): negative time not invented yet/,
25-
"negative time error";
26-
2733
SKIP: {
2834
skip "no subsecond alarm", 2 unless $can_subsecond_alarm;
2935
my $f = Time::HiRes::time;
@@ -35,5 +41,3 @@ SKIP: {
3541
printf("# sleep...%s\n", Time::HiRes::tv_interval($r));
3642
ok 1;
3743
}
38-
39-
1;

dist/Time-HiRes/t/time.t

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
use strict;
22

3-
use Test::More tests => 2;
3+
use Test::More tests => 3;
44
BEGIN { push @INC, '.' }
55
use t::Watchdog;
66

77
BEGIN { require_ok "Time::HiRes"; }
88

9+
SKIP: {
10+
skip "no hi-res time", 1 unless defined &Time::HiRes::time;
11+
is prototype(\&Time::HiRes::time), prototype('CORE::time'),
12+
"Time::HiRes::time's prototype matches CORE::time's";
13+
}
14+
915
SKIP: {
1016
skip "no gettimeofday", 1 unless &Time::HiRes::d_gettimeofday;
1117
my ($s, $n, $i) = (0);
@@ -20,5 +26,3 @@ SKIP: {
2026
or print("# Time::HiRes::time() not close to CORE::time()\n");
2127
printf("# s = $s, n = $n, s/n = %s\n", abs($s)/$n);
2228
}
23-
24-
1;

0 commit comments

Comments
 (0)