From 1ba2550b725954ba2ce4bf0d3bfc871387660617 Mon Sep 17 00:00:00 2001 From: Florent Angly Date: Thu, 5 Nov 2015 17:55:20 +0100 Subject: [PATCH] Let Timer accept all allowable perl numbers Incidentally, it is now possible to have a timer that never times out using 'inf' --- Makefile.PL | 8 ++++++-- lib/IPC/Run/Timer.pm | 16 ++++++++++------ t/timer.t | 4 +++- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Makefile.PL b/Makefile.PL index 300b8b4..1fe0972 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -27,8 +27,7 @@ TOHERE ## Die nicely in case some install manager cares about the canonical ## error message for this. Not that I've ever seen one, but those ## wacky CPANPLUSers might just do something cool in this case. - - ## Older perls' Socket.pm don't export IPPROTO_TCP + ## Older perls' Socket.pm don't export IPPROTO_TCP require 5.006; ## Most of the time it's not needed (since IPC::Run tries not to ## use sockets), but the user is not likely to know what the hell @@ -39,6 +38,11 @@ TOHERE } } +if ( $^V < version->parse('v5.8.1') ) { + # need Scalar::Util::looks_like_number + $PREREQ_PM{'Scalar::List::Utils'} = '1.10'; +} + WriteMakefile( NAME => 'IPC::Run', ABSTRACT => 'system() and background procs w/ piping, redirs, ptys (Unix, Win32)', diff --git a/lib/IPC/Run/Timer.pm b/lib/IPC/Run/Timer.pm index 9fe1124..c283197 100644 --- a/lib/IPC/Run/Timer.pm +++ b/lib/IPC/Run/Timer.pm @@ -71,9 +71,9 @@ it's in. =head2 Time values -All time values are in seconds. Times may be specified as integer or -floating point seconds, optionally preceded by puncuation-separated -days, hours, and minutes.\ +All time values are in seconds. Times may be any kind of perl number, +e.g. as integer or floating point seconds, optionally preceded by +punctuation-separated days, hours, and minutes. Examples: @@ -84,6 +84,7 @@ Examples: 1:1 1 minute, 1 second 1:90 2 minutes, 30 seconds 1:2:3:4.5 1 day, 2 hours, 3 minutes, 4.5 seconds + 'inf' the infinity perl special number (the timer never finishes) Absolute date/time strings are *not* accepted: year, month and day-of-month parsing is not available (patches welcome :-). @@ -161,6 +162,7 @@ use Carp; use Fcntl; use Symbol; use Exporter; +use Scalar::Util; use vars qw( $VERSION @ISA @EXPORT_OK %EXPORT_TAGS ); BEGIN { $VERSION = '0.90'; @@ -202,9 +204,11 @@ sub _parse_time { if (scalar @f > 4) { croak "IPC::Run: expected <= 4 elements in time string '$_'"; } - map { - $_ =~ /\d+/ ? $_ : croak "IPC::Run: non-numeric element '$_' in time string '$_'"; - } @f; + for (@f) { + if (not Scalar::Util::looks_like_number($_)) { + croak "IPC::Run: non-numeric element '$_' in time string '$_'"; + } + } my ( $s, $m, $h, $d ) = reverse @f; $val = ( ( ( $d || 0 ) * 24 diff --git a/t/timer.t b/t/timer.t index 1d1372a..004ac8a 100644 --- a/t/timer.t +++ b/t/timer.t @@ -19,7 +19,7 @@ BEGIN { } } -use Test::More tests => 76; +use Test::More tests => 77; use IPC::Run qw( run ); use IPC::Run::Timer qw( :all ); @@ -41,6 +41,8 @@ $t->interval( 30 ); ok( $t->interval >= 30 ); $t->interval( 30.1 ); ok( $t->interval > 30 ); $t->interval( 30.1 ); ok( $t->interval <= 31 ); +$t->interval( 'inf' ); ok( $t->interval > 1000 ); + $t->interval( "1:0" ); is( $t->interval, 60 ); $t->interval( "1:0:0" ); is( $t->interval, 3600 ); $t->interval( "1:1:1" ); is( $t->interval, 3661 );