[comp.lang.perl] Array wierdness

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (02/06/91)

In article <1991Feb04.193857.21894@convex.com> sunshine@convex.com (Roger Sunshine) writes:
: Given the simple script:
: @array = (1,2,3,4,5,6,7);
: 
: while(defined $array[0])
: {
:   print("Foo: $array[0]\n");
:   shift(@array);
: }
: 
: printf("array len: %d, defined: %d\n", $#array, defined $array[0]);
: 
: perl -v shows:
: This is perl, version 3.0
: 
: $Header: perly.c,v 3.0.1.10 91/01/11 18:22:48 lwall Locked $
: Patch level: 44
: 
: Using ths perl, I get the following results when run:
: Foo: 1
: Foo: 2
: Foo: 3
: Foo: 4
: Foo: 5
: Foo: 6
: Foo: 7
: array len: 0, defined: 0
: 
: Seems to me like $#array should be -1?
: 
: This is cleared up by using "while(@array)".

The problem is that the defined operator thinks it wants an lvalue, so
it creates the array element in question while trying to decide if it's
defined, much as saying "$array[0] = undef" would.  It doesn't change
the value of defined(), but it does modify the max array index.  It may
be fixable, but it's not way up there on my list.  "while (@array)" is the
recommended way to iterate an array to death.

Larry