On Mon Nov 19 05:00:44 2012, GRAF wrote:
Show quoted text> Try this:
>
> ~> perl -MParams::Util=_POSINT -wE '
> $x = 1;
> say $x;
> say _POSINT($x) ? "true" : "false";
> $x =~ /(\d+)/;
> say $1;
> say _POSINT($1) ? "true" : "false";
> '
Attached are tests exercising this use case as well as a patch which allows POSINT to accept regexp match variables as arguments.
Cheers,
Paul
From 17402d376d26a2042e817fcba604cf8c7c769ec0 Mon Sep 17 00:00:00 2001
From: Paul Cochrane <paul@liekut.de>
Date: Thu, 29 May 2014 15:35:02 +0200
Subject: [PATCH 1/2] Adding tests for regexp matches as args to POSINT
As mentioned in RT#81276, passing a regexp match as an argument to POSINT
returns undef even though the match evaluates to a positive integer. This
change adds tests to exercise this use case.
---
t/02_main.t | 14 +++++++++++++-
t/12_main.t | 15 ++++++++++++---
2 files changed, 25 insertions(+), 4 deletions(-)
diff --git a/t/02_main.t b/t/02_main.t
index 64ef1e4..72768de 100644
--- a/t/02_main.t
+++ b/t/02_main.t
@@ -7,7 +7,7 @@ BEGIN {
$ENV{PERL_PARAMS_UTIL_PP} ||= 0;
}
-use Test::More tests => 632;
+use Test::More tests => 634;
use File::Spec::Functions ':ALL';
use Scalar::Util 'refaddr';
use Params::Util ();
@@ -239,6 +239,12 @@ foreach my $id ( qw{1 2 10 123456789} ) {
is( Params::Util::_POSINT($id), $id, "...::_POSINT('$id') returns ok" );
}
+{
+ my $pos_int = 2;
+ $pos_int =~ /(\d+)/;
+ is( Params::Util::_POSINT($1), $1, "...::_POSINT('$1') returns ok" );
+}
+
# Import the function
use_ok( 'Params::Util', '_POSINT' );
ok( defined *_POSINT{CODE}, '_POSINT imported ok' );
@@ -264,6 +270,12 @@ foreach my $id ( qw{1 2 10 123456789} ) {
is( _POSINT($id), $id, "_POSINT('$id') returns ok" );
}
+{
+ my $pos_int = 2;
+ $pos_int =~ /(\d+)/;
+ is( _POSINT($1), $1, "...::_POSINT('$1') returns ok" );
+}
+
diff --git a/t/12_main.t b/t/12_main.t
index d8cf68f..63e172d 100644
--- a/t/12_main.t
+++ b/t/12_main.t
@@ -7,7 +7,7 @@ BEGIN {
$ENV{PERL_PARAMS_UTIL_PP} ||= 1;
}
-use Test::More tests => 632;
+use Test::More tests => 634;
use File::Spec::Functions ':ALL';
use Scalar::Util 'refaddr';
use Params::Util ();
@@ -239,6 +239,12 @@ foreach my $id ( qw{1 2 10 123456789} ) {
is( Params::Util::_POSINT($id), $id, "...::_POSINT('$id') returns ok" );
}
+{
+ my $pos_int = 2;
+ $pos_int =~ /(\d+)/;
+ is( Params::Util::_POSINT($1), $1, "...::_POSINT('$1') returns ok" );
+}
+
# Import the function
use_ok( 'Params::Util', '_POSINT' );
ok( defined *_POSINT{CODE}, '_POSINT imported ok' );
@@ -264,8 +270,11 @@ foreach my $id ( qw{1 2 10 123456789} ) {
is( _POSINT($id), $id, "_POSINT('$id') returns ok" );
}
-
-
+{
+ my $pos_int = 2;
+ $pos_int =~ /(\d+)/;
+ is( _POSINT($1), $1, "...::_POSINT('$1') returns ok" );
+}
#####################################################################
--
1.7.10.4
From d741982501965b10f96e2b03aed7f650d7a2963d Mon Sep 17 00:00:00 2001
From: Paul Cochrane <paul@liekut.de>
Date: Thu, 29 May 2014 15:37:42 +0200
Subject: [PATCH 2/2] Allowing POSINT to accept regexp matches as args
This bug was actually very subtle. A POSINT is something which is (a)
defined, (b) not a reference and (c) matches a regexp for a positive
integer. If these evaluate as true, then the argument is returned,
otherwise undef is returned. As it turns out, when a regexp match is passed
in as an argument to this function, then (a), (b) and (c) all evaluate to
true, however undef was *still* returned. The reason being that (c) must
use $_[0] and sets it to undef, which was then being returned. The fix is
thus to set the argument to POSINT ($_[0]) to a variable and then use this
variable in (a), (b) and (c) to process the function correctly. It might
possibly be a good idea to use 'shift' to obtain the input argument, but
that can be improved as needed later.
This change should address the issue raised in RT#81276.
---
lib/Params/Util.pm | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/lib/Params/Util.pm b/lib/Params/Util.pm
index 9a40e59..a55c189 100644
--- a/lib/Params/Util.pm
+++ b/lib/Params/Util.pm
@@ -302,7 +302,8 @@ name.
eval <<'END_PERL' unless defined &_POSINT;
sub _POSINT ($) {
- (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[1-9]\d*$/) ? $_[0] : undef;
+ my $arg = $_[0];
+ (defined $arg and ! ref $arg and $arg =~ m/^[1-9]\d*$/) ? $arg : undef;
}
END_PERL
--
1.7.10.4