Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dumpvalue: use 'defined' on array elements, not 'exists' #17179

Closed
p5pRT opened this issue Oct 10, 2019 · 6 comments
Closed

Dumpvalue: use 'defined' on array elements, not 'exists' #17179

p5pRT opened this issue Oct 10, 2019 · 6 comments

Comments

@p5pRT
Copy link

p5pRT commented Oct 10, 2019

Migrated from rt.perl.org#134492 (status was 'pending release')

Searchable as RT134492$

@p5pRT
Copy link
Author

p5pRT commented Oct 10, 2019

From @jkeenan

pod/perlfunc.pod advises us​:

  WARNING​: Calling "exists" on array values is strongly
  discouraged. The notion of deleting or checking the existence of
  Perl array elements is not conceptually coherent, and can lead
  to surprising behavior.

While trying to extend the test coverage of Dumpvalue.pm, two instances
of 'exists ARRAYREF' were noted. In the second of these two instances,
using 'exists ARRAYREF' resulted in the 'if' condition of an if/else
block always returning true, thereby making the corresponding 'else'
condition unreachable -- a situation which was confirmed by coverage
analysis.

We should only use best practices in modules maintained by Perl 5
Porters; this patch implements that. When we do so, a different (but
actually better) error message appears in the second instance; so tests
are updated as well.

Available for smoke-testing in this branch​:

smoke-me/jkeenan/Dumpvalue-dont-call-exists-on-array-elements

Thank you very much.
Jim Keenan

perl perl perl

@p5pRT
Copy link
Author

p5pRT commented Oct 10, 2019

From @jkeenan

0001-Do-not-use-exists-on-arrays-use-defined-instead.patch
From a333aabbd8178d79e5fb3838e7e22e4de3c7127f Mon Sep 17 00:00:00 2001
From: James E Keenan <jkeenan@cpan.org>
Date: Thu, 10 Oct 2019 18:49:20 -0400
Subject: [PATCH] Do not use 'exists' on arrays; use 'defined' instead

pod/perlfunc.pod advises us:

       WARNING: Calling "exists" on array values is strongly
       discouraged. The notion of deleting or checking the existence of
       Perl array elements is not conceptually coherent, and can lead
       to surprising behavior.

While trying to extend the test coverage of Dumpvalue.pm, two instances
of 'exists ARRAYREF' were noted.  In the second of these two instances,
using 'exists ARRAYREF' resulted in the 'if' condition of an if/else
block always returning true, thereby making the corresponding 'else'
condition unreachable -- a situation which was confirmed by coverage
analysis.

We should only use best practices in modules maintained by Perl 5
Porters; this patch implements that.  When we do so, a different (but
actually better) error message appears in the second instance; so tests
are updated as well.
---
 dist/Dumpvalue/lib/Dumpvalue.pm        | 6 +++---
 dist/Dumpvalue/t/rt-134441-dumpvalue.t | 8 ++++----
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/dist/Dumpvalue/lib/Dumpvalue.pm b/dist/Dumpvalue/lib/Dumpvalue.pm
index 3faf829538..cc14cc4701 100644
--- a/dist/Dumpvalue/lib/Dumpvalue.pm
+++ b/dist/Dumpvalue/lib/Dumpvalue.pm
@@ -1,7 +1,7 @@
 use 5.006_001;			# for (defined ref) and $#$v and our
 package Dumpvalue;
 use strict;
-our $VERSION = '1.19';
+our $VERSION = '1.21';
 our(%address, $stab, @stab, %stab, %subs);
 
 sub ASCII { return ord('A') == 65; }
@@ -254,7 +254,7 @@ sub unwrap {
       if ($#$v >= 0) {
 	$short = $sp . "0..$#{$v}  " .
 	  join(" ", 
-	       map {exists $v->[$_] ? $self->stringify($v->[$_]) : "empty"} (0..$tArrayDepth)
+	       map {defined $v->[$_] ? $self->stringify($v->[$_]) : "empty"} (0..$tArrayDepth)
 	      ) . "$shortmore";
       } else {
 	$short = $sp . "empty array";
@@ -264,7 +264,7 @@ sub unwrap {
     for my $num (0 .. $tArrayDepth) {
       return if $DB::signal and $self->{stopDbSignal};
       print "$sp$num  ";
-      if (exists $v->[$num]) {
+      if (defined $v->[$num]) {
         $self->DumpElem($v->[$num], $s);
       } else {
 	print "empty slot\n";
diff --git a/dist/Dumpvalue/t/rt-134441-dumpvalue.t b/dist/Dumpvalue/t/rt-134441-dumpvalue.t
index cc9f270f5a..324845d48d 100644
--- a/dist/Dumpvalue/t/rt-134441-dumpvalue.t
+++ b/dist/Dumpvalue/t/rt-134441-dumpvalue.t
@@ -39,11 +39,11 @@ is( $y, $x,
 @foobar = (undef, 'bar');
 $d->dumpValue([@foobar]);
 $x = $out->read;
-is( $x, "0  undef\n1  'bar'\n",
+is( $x, "0  empty slot\n1  'bar'\n",
     'dumpValue worked on array ref, first element undefined' );
 $d->dumpValues(@foobar);
 $y = $out->read;
-is( $y, "0  undef\n1  'bar'\n",
+is( $y, "0  empty slot\n1  'bar'\n",
     'dumpValues worked on array, first element undefined' );
 is( $y, $x,
     "dumpValues called on array returns same as dumpValue on array ref, first element undefined");
@@ -51,11 +51,11 @@ is( $y, $x,
 @foobar = ('bar', undef);
 $d->dumpValue([@foobar]);
 $x = $out->read;
-is( $x, "0  'bar'\n1  undef\n",
+is( $x, "0  'bar'\n1  empty slot\n",
     'dumpValue worked on array ref, last element undefined' );
 $d->dumpValues(@foobar);
 $y = $out->read;
-is( $y, "0  'bar'\n1  undef\n",
+is( $y, "0  'bar'\n1  empty slot\n",
     'dumpValues worked on array, last element undefined' );
 is( $y, $x,
     "dumpValues called on array returns same as dumpValue on array ref, last element undefined");
-- 
2.17.1

@p5pRT
Copy link
Author

p5pRT commented Oct 11, 2019

From @jkeenan

On Thu, 10 Oct 2019 23​:04​:15 GMT, jkeenan@​pobox.com wrote​:

pod/perlfunc.pod advises us​:

    WARNING&#8203;: Calling "exists" on array values is strongly
    discouraged\. The notion of deleting or checking the existence of
    Perl array elements is not conceptually coherent\, and can lead
    to surprising behavior\.

While trying to extend the test coverage of Dumpvalue.pm, two instances
of 'exists ARRAYREF' were noted. In the second of these two instances,
using 'exists ARRAYREF' resulted in the 'if' condition of an if/else
block always returning true, thereby making the corresponding 'else'
condition unreachable -- a situation which was confirmed by coverage
analysis.

We should only use best practices in modules maintained by Perl 5
Porters; this patch implements that. When we do so, a different (but
actually better) error message appears in the second instance; so tests
are updated as well.

Available for smoke-testing in this branch​:

smoke-me/jkeenan/Dumpvalue-dont-call-exists-on-array-elements

If anyone has objections to this please speak up quickly, as I would like to resolve this RT before the transition of issues to github commences.

Thank you very much.

James E Keenan (jkeenan@​cpan.org)

@p5pRT
Copy link
Author

p5pRT commented Oct 11, 2019

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Oct 13, 2019

From @jkeenan

Pushed to blead in commit e4b1957.

Thank you very much.
--
James E Keenan (jkeenan@​cpan.org)

@p5pRT
Copy link
Author

p5pRT commented Oct 13, 2019

@jkeenan - Status changed from 'open' to 'pending release'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant