Re: Perl array bug?



In article <n212k7-0e81.ln1@xxxxxxxxxxxxxxxxxxxxxxx>, Ben Morrow <ben@xxxxxxxxxxxx> wrote:

It's going to be tough to reproduce without a testcase. If you can, it
would be good to start stripping things out of you app until you can't
reproduce the problem any more.

Yeah, I think you missed the top part of that post, with a
test case ;-)

perlbug is correct, but without a test case it's rather unlikely anyone
can do anything about it. (It is, of course, possible someone on p5p
will spot something I haven't.)

Here's a draft of what I plan to submit via perlbug including
the test case:

This is a bug report for perl from malch@xxxxxxxxx,

Operating System: 64-bit Windows 7.

Perl Version: ActivePerl version 5.12.1.1201 64-bit:

---
Flags:
category=core
severity=high
---

Code to reproduce this problem:

#====================================================

@Big1 = ();
$Big1[4] = 0;
$Big1[9] = 1;
$Big1[6] = 2;
my $len = 0; # This appears to be significant
foreach $key (@Big1) {
$len = length ($key);
print "key = $key, len=$len\n";
}
exit;

#=====================================================

Using ActivePerl version 5.12.1.1201 64-bit I get:

C:\ZIP>c:\perl512\bin\perl.exe test.pl
key = , len=0
key = , len=0
key = , len=0
key = , len=0
key = 0, len=1
key = , len=1 <==== ???
key = 2, len=1
key = , len=1 <==== ???
key = , len=1 <==== ???
key = 1, len=1

Using ActivePerl version 5.10.1.1007 64-bit I get:

C:\ZIP>C:\perl510\bin\perl.exe test.pl
key = , len=0
key = , len=0
key = , len=0
key = , len=0
key = 0, len=1
key = , len=0
key = 2, len=1
key = , len=0
key = , len=0
key = 1, len=1


Some other Perl programmers have taken the position that this
is a programmer error because the code attempts to establish
the length() of an undef value. That, they say, is garbage
analogous to division by zero.

I disagree and cite the following in support of my position
that this constitutes a bug:

1. Perl Version 5.10.1.1007 and every other major Perl version
going back 10 plus years have consistently returned zero
for the length of an undef value.

2. perlsyn clearly states:

"A variable holds the undefined value ("undef") until it has been
assigned a defined value, which is anything other than "undef".
When used as a number, "undef" is treated as 0; when used as a
string, it is treated as the empty string, ""; and when used as
a reference that isn't being assigned to, it is treated as an error."

Thus "undef" should be treated as the empty string. And the
length of the empty string is unambiguously zero. Hence the
behaviour of the 5.12.1.1201 is clearly a bug!


Summary of my perl5 (revision 5 version 12 subversion 1) configuration:
[snip]




--
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
| Malcolm Hoar "The more I practice, the luckier I get". |
| malch@xxxxxxxxx Gary Player. |
| http://www.malch.com/ Shpx gur PQN. |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.



Relevant Pages

  • Re: Does undef = binary zero?
    ... Or an empty string or the undefined value "undef", ... It looks like the state of Undef is a binary zero to Perl. ... simple boolean tests that your program should let through, ...
    (perl.beginners)
  • Re: Perl array bug?
    ... the length of undef is well defined. ... The only things you need to declare in Perl are report formats and ... subroutines. ... when used as a string, it is treated as the empty string, ...
    (comp.lang.perl.misc)
  • Re: Checking environment variable existence
    ... AK> environment variable exists and is not undef ... script, then you can't get undefined entries. ... empty string. ... Search or Offer Perl Jobs ...
    (comp.lang.perl.misc)
  • Re: Newbie: Testing for Null
    ... Pritchie wrote: ... There is no such thing as a "null" value in Perl. ... There is the empty string: ... > How can I test for null or undef? ...
    (comp.lang.perl.misc)
  • Re: Check POP3 E-mail
    ... > Note the sassign and the undef; ... default language, and, from what I've seen, extremely common in business ... COBOL, so I don't know if those required explicit initialization or not, ... I am familiar with Perl; however, ...
    (comp.lang.perl.misc)