From 99c7b0464e9adada545331b16ffe72b9f2d6c979 Mon Sep 17 00:00:00 2001 From: Lukas Mai Date: Fri, 22 Aug 2025 22:52:01 +0200 Subject: [PATCH 1/3] 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. --- dist/Time-HiRes/Changes | 1 + dist/Time-HiRes/HiRes.pm | 2 +- dist/Time-HiRes/HiRes.xs | 1 + dist/Time-HiRes/t/sleep.t | 10 +++++++--- dist/Time-HiRes/t/time.t | 10 +++++++--- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/dist/Time-HiRes/Changes b/dist/Time-HiRes/Changes index 31f28feebe97..0e991b515a16 100644 --- a/dist/Time-HiRes/Changes +++ b/dist/Time-HiRes/Changes @@ -19,6 +19,7 @@ Revision history for the Perl extension Time::HiRes. build failures with MSVC. - don't try to suppress C++ compatibility warnings in C++ builds, since that warns. + - Fix sleep's prototype to match CORE::sleep (;$). 1.9764 [2020-08-10] - Fix a bunch of repeated-word typos diff --git a/dist/Time-HiRes/HiRes.pm b/dist/Time-HiRes/HiRes.pm index 69436376f1da..158560cab55e 100644 --- a/dist/Time-HiRes/HiRes.pm +++ b/dist/Time-HiRes/HiRes.pm @@ -50,7 +50,7 @@ our @EXPORT_OK = qw (usleep sleep ualarm alarm gettimeofday time tv_interval stat lstat utime ); -our $VERSION = '1.9778'; +our $VERSION = '1.9779'; our $XS_VERSION = $VERSION; $VERSION = eval $VERSION; diff --git a/dist/Time-HiRes/HiRes.xs b/dist/Time-HiRes/HiRes.xs index b879198a30a2..98fd959ef896 100644 --- a/dist/Time-HiRes/HiRes.xs +++ b/dist/Time-HiRes/HiRes.xs @@ -1019,6 +1019,7 @@ nanosleep(nsec) NV sleep(...) +PROTOTYPE: ;$ PREINIT: struct timeval Ta, Tb; CODE: diff --git a/dist/Time-HiRes/t/sleep.t b/dist/Time-HiRes/t/sleep.t index 04516504a297..fd260354672c 100644 --- a/dist/Time-HiRes/t/sleep.t +++ b/dist/Time-HiRes/t/sleep.t @@ -1,6 +1,6 @@ use strict; -use Test::More tests => 4; +use Test::More tests => 5; BEGIN { push @INC, '.' } use t::Watchdog; @@ -8,6 +8,12 @@ BEGIN { require_ok "Time::HiRes"; } use Config; +SKIP: { + skip "no hi-res sleep", 1 unless defined &Time::HiRes::sleep; + is prototype(\&Time::HiRes::sleep), prototype('CORE::sleep'), + "Time::HiRes::sleep's prototype matches CORE::sleep's"; +} + my $xdefine = ''; if (open(XDEFINE, "<", "xdefine")) { chomp($xdefine = || ""); @@ -35,5 +41,3 @@ SKIP: { printf("# sleep...%s\n", Time::HiRes::tv_interval($r)); ok 1; } - -1; diff --git a/dist/Time-HiRes/t/time.t b/dist/Time-HiRes/t/time.t index ad42f47004b4..3843c7741f92 100644 --- a/dist/Time-HiRes/t/time.t +++ b/dist/Time-HiRes/t/time.t @@ -1,11 +1,17 @@ use strict; -use Test::More tests => 2; +use Test::More tests => 3; BEGIN { push @INC, '.' } use t::Watchdog; BEGIN { require_ok "Time::HiRes"; } +SKIP: { + skip "no hi-res time", 1 unless defined &Time::HiRes::time; + is prototype(\&Time::HiRes::time), prototype('CORE::time'), + "Time::HiRes::time's prototype matches CORE::time's"; +} + SKIP: { skip "no gettimeofday", 1 unless &Time::HiRes::d_gettimeofday; my ($s, $n, $i) = (0); @@ -20,5 +26,3 @@ SKIP: { or print("# Time::HiRes::time() not close to CORE::time()\n"); printf("# s = $s, n = $n, s/n = %s\n", abs($s)/$n); } - -1; From e893d2eddb7fba4430f209d8a29305c6db830a41 Mon Sep 17 00:00:00 2001 From: Lukas Mai Date: Sat, 23 Aug 2025 10:11:40 +0200 Subject: [PATCH 2/3] Time::HiRes tests: remove bareword filehandles and '1;' Bareword filehandles have been obsolete since 2000. There is no point in adding '1;' to scripts; only modules have return values. --- dist/Time-HiRes/t/alarm.t | 8 +++----- dist/Time-HiRes/t/clock.t | 2 -- dist/Time-HiRes/t/gettimeofday.t | 2 -- dist/Time-HiRes/t/itimer.t | 2 -- dist/Time-HiRes/t/nanosleep.t | 2 -- dist/Time-HiRes/t/sleep.t | 6 +++--- dist/Time-HiRes/t/stat.t | 20 +++++++++----------- dist/Time-HiRes/t/tv_interval.t | 2 -- dist/Time-HiRes/t/ualarm.t | 2 -- dist/Time-HiRes/t/usleep.t | 2 -- dist/Time-HiRes/t/utime.t | 2 -- 11 files changed, 15 insertions(+), 35 deletions(-) diff --git a/dist/Time-HiRes/t/alarm.t b/dist/Time-HiRes/t/alarm.t index 6ebf380e1ff6..10d7e619a7d7 100644 --- a/dist/Time-HiRes/t/alarm.t +++ b/dist/Time-HiRes/t/alarm.t @@ -11,9 +11,9 @@ use Config; my $limit = 0.25; # 25% is acceptable slosh for testing timers my $xdefine = ''; -if (open(XDEFINE, "<", "xdefine")) { - chomp($xdefine = || ""); - close(XDEFINE); +if (open(my $fh, "<", "xdefine")) { + chomp($xdefine = <$fh> || ""); + close($fh); } my $can_subsecond_alarm = @@ -224,5 +224,3 @@ SKIP: { ok $got == 0 or print("# $got\n"); } } - -1; diff --git a/dist/Time-HiRes/t/clock.t b/dist/Time-HiRes/t/clock.t index 810d63a272fe..1490b9d16d99 100644 --- a/dist/Time-HiRes/t/clock.t +++ b/dist/Time-HiRes/t/clock.t @@ -97,5 +97,3 @@ SKIP: { $clock[2] > $clock[1] && $clock[3] > $clock[2]; } - -1; diff --git a/dist/Time-HiRes/t/gettimeofday.t b/dist/Time-HiRes/t/gettimeofday.t index 05cebbb05549..b71034867f6d 100644 --- a/dist/Time-HiRes/t/gettimeofday.t +++ b/dist/Time-HiRes/t/gettimeofday.t @@ -30,5 +30,3 @@ ok $f - $two[0] < 2 or print("# $f - $two[0] >= 2\n"); my $r = [Time::HiRes::gettimeofday()]; my $g = Time::HiRes::tv_interval $r; ok $g < 2 or print("# $g\n"); - -1; diff --git a/dist/Time-HiRes/t/itimer.t b/dist/Time-HiRes/t/itimer.t index 4e4ce6d10f86..378293996f32 100644 --- a/dist/Time-HiRes/t/itimer.t +++ b/dist/Time-HiRes/t/itimer.t @@ -66,5 +66,3 @@ print("# at end, i=$i\n"); is($virt, 0, "time left should be zero"); $SIG{VTALRM} = 'DEFAULT'; - -1; diff --git a/dist/Time-HiRes/t/nanosleep.t b/dist/Time-HiRes/t/nanosleep.t index ff056379dd9a..0343cec44f28 100644 --- a/dist/Time-HiRes/t/nanosleep.t +++ b/dist/Time-HiRes/t/nanosleep.t @@ -34,5 +34,3 @@ SKIP: { skip "flapping test - more than 0.9 sec could be necessary...", 1 if $ENV{CI}; cmp_ok $d, '<', 0.9 or diag("# slept $d secs $f to $f2\n"); } - -1; diff --git a/dist/Time-HiRes/t/sleep.t b/dist/Time-HiRes/t/sleep.t index fd260354672c..04929a680cfa 100644 --- a/dist/Time-HiRes/t/sleep.t +++ b/dist/Time-HiRes/t/sleep.t @@ -15,9 +15,9 @@ SKIP: { } my $xdefine = ''; -if (open(XDEFINE, "<", "xdefine")) { - chomp($xdefine = || ""); - close(XDEFINE); +if (open(my $fh, "<", "xdefine")) { + chomp($xdefine = <$fh> || ""); + close($fh); } my $can_subsecond_alarm = diff --git a/dist/Time-HiRes/t/stat.t b/dist/Time-HiRes/t/stat.t index 2f72fdc2affd..e82d4806d368 100644 --- a/dist/Time-HiRes/t/stat.t +++ b/dist/Time-HiRes/t/stat.t @@ -22,9 +22,9 @@ my @mtime; for (1..5) { note "cycle $_"; Time::HiRes::sleep(rand(0.1) + 0.1); - open(X, '>', $$); - print X $$; - close(X); + open(my $fh, '>', $$); + print $fh $$; + close($fh); my($a, $stat, $b) = ("a", [Time::HiRes::stat($$)], "b"); is $a, "a", "stat stack discipline"; is $b, "b", "stat stack discipline"; @@ -43,9 +43,9 @@ for (1..5) { } is_deeply $lstat, $stat, "write: stat and lstat returned same values"; Time::HiRes::sleep(rand(0.1) + 0.1); - open(X, '<', $$); - ; - close(X); + open(my $fh, '<', $$); + <$fh>; + close($fh); $stat = [Time::HiRes::stat($$)]; push @atime, $stat->[8]; $lstat = [Time::HiRes::lstat($$)]; @@ -88,9 +88,9 @@ SKIP: { my $targetname = "tgt$$"; my $linkname = "link$$"; SKIP: { - open(X, '>', $targetname); - print X $$; - close(X); + open(my $fh, '>', $targetname); + print $fh $$; + close($fh); eval { symlink $targetname, $linkname or die "can't symlink: $!"; }; skip "can't symlink", 7 if $@ ne ""; note "compare Time::HiRes::stat with ::lstat"; @@ -111,5 +111,3 @@ SKIP: { } 1 while unlink $linkname; 1 while unlink $targetname; - -1; diff --git a/dist/Time-HiRes/t/tv_interval.t b/dist/Time-HiRes/t/tv_interval.t index 8ac876daf3a3..479d1d66309d 100644 --- a/dist/Time-HiRes/t/tv_interval.t +++ b/dist/Time-HiRes/t/tv_interval.t @@ -6,5 +6,3 @@ BEGIN { require_ok "Time::HiRes"; } my $f = Time::HiRes::tv_interval [5, 100_000], [10, 500_000]; ok abs($f - 5.4) < 0.001 or print("# $f\n"); - -1; diff --git a/dist/Time-HiRes/t/ualarm.t b/dist/Time-HiRes/t/ualarm.t index d478224cbe75..3521b595c004 100644 --- a/dist/Time-HiRes/t/ualarm.t +++ b/dist/Time-HiRes/t/ualarm.t @@ -109,5 +109,3 @@ for my $n (100_000, 1_100_000, 2_200_000, 4_300_000) { my $got2 = Time::HiRes::ualarm(0); ok $got2 == 0 or print("# $got2\n"); } - -1; diff --git a/dist/Time-HiRes/t/usleep.t b/dist/Time-HiRes/t/usleep.t index 396341dc0837..ebfd8bb8b083 100644 --- a/dist/Time-HiRes/t/usleep.t +++ b/dist/Time-HiRes/t/usleep.t @@ -75,5 +75,3 @@ SKIP: { ok $a < $limit or print("# $msg\n"); } } - -1; diff --git a/dist/Time-HiRes/t/utime.t b/dist/Time-HiRes/t/utime.t index 8a4f0717a874..220f7c13ce24 100644 --- a/dist/Time-HiRes/t/utime.t +++ b/dist/Time-HiRes/t/utime.t @@ -248,5 +248,3 @@ print "# negative mtime dies;\n"; }; done_testing(); - -1; From 01c4539928f6c63e924820eba4838ac5ca50b996 Mon Sep 17 00:00:00 2001 From: Lukas Mai Date: Fri, 22 Aug 2025 23:13:55 +0200 Subject: [PATCH 3/3] perldelta for Time::HiRes 1.9779 (sleep prototype) --- pod/perldelta.pod | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/pod/perldelta.pod b/pod/perldelta.pod index 2289feed4510..b00fb6b04aa1 100644 --- a/pod/perldelta.pod +++ b/pod/perldelta.pod @@ -125,9 +125,21 @@ XXX Remove this section if F did not add any cont =item * -L has been upgraded from version A.xx to B.yy. +L has been upgraded from version 1.9778 to 1.9779. -XXX If there was something important to note about this change, include that here. +The subsecond-resolution C function provided by L now has +the same prototype as perl's built-in L. This +means it now requires a scalar argument, not a list, just like C: + + use Time::HiRes qw(sleep); + + sleep(1, "foo", "bar"); # Syntax error. + # It used to silently ignore the extra arguments. + + my @t = 42; + sleep @t; + # Evaluates @t in scalar context (giving the number of elements) + # and sleeps for one second. It used to sleep for 42 seconds. =back