[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

Re: exists $foo[7] and delete $foo[7]



On Thu, 13 Jan 2000 11:17:14 MST, Tom Christiansen wrote:
>This is disconcerting.  In Perl lingo, we have always used the terms
>"undefined" and "uninitialized" interchangeably when it came to
>scalar values.
>
>    % perl -wle 'print $a + $a' (y|n|e|a)? yes
>    Use of uninitialized value at -e line 1.
>    Use of uninitialized value at -e line 1.
>    0
>
>    % perl -wle 'print $H{a} + $H{a}'
>    Use of uninitialized value at -e line 1.
>    Use of uninitialized value at -e line 1.
>    0
>
>    % perl -wle '%H = ("a",undef); print $H{a} + $H{a}'
>    Use of uninitialized value at -e line 1.
>    Use of uninitialized value at -e line 1.
>    0
>
>    % perl -wle '%H = ("a",undef); print $H{a} + $H{a}; delete $H{a}; print $H{a} + $H{a
>};'
>    Use of uninitialized value at -e line 1.
>    Use of uninitialized value at -e line 1.
>    0
>    Use of uninitialized value at -e line 1.
>    Use of uninitialized value at -e line 1.
>    0
>
>I believe there is firm precedent for understanding uninitialized
>to mean undefined.

Agreed.  But the reverse is not true.

>                   Furthermore, it would appear that introducing
>a nuance between these that operating in some cases (on array
>elements' scalar values)

You keep saying "array elements' scalar values" as though there
were array elements that weren't scalar values.  Is there something
I'm missing?

>                         but not on others (on non-aggregate scalar
>values) offers no sufficiently redeeming advantage that I have been
>able to gleen which would offset the added complexity.

I can appreciate that line of argument.

>>>What about the scalar elements in a hash?
>>The change does not affect hashes.
>
>>>Can they also be uninitialized?
>>Yes, using delete() as we've always been able.
>
>No, we've not been able to call delete() on anything but
>hash elements before.

But I thought the question was only about hashes.

>>>The nature of truth is becoming very blurred in my own mind, at least.
>
>>I think I agree, but the notion of "whether an element truly exists" is
>>not new.  We've had it all along for hash elements.
>
>Why do we *care* whether space for a scalar has ever been allocated
>in an array?

Because if it isn't, then one may not care to do anything with that
element.

>             And if we do care so much about that, why don't we
>care whether space for a scalar that's *not* in an array has ever
>been allocated?  
>
>    @array = qw/zero one two three/, undef, 'five';
>
>Why do we care to distinguish between $array[4] and $array[17] above?

$array[4] has been explicitly initialized with a value (undef).
$array[17] hasn't.  One would expect to consider $array[17] as being
"not there", whereas $array[4] is "there" because you gave it a
value.

>IF we undef $array[1], why should that be different that the status 
>fo $array[17]?  Is there some reason why people shouldn't be expected
>to simply write an expression like
>
>	(@array < $N && defined $array[$N])

No other reason than the "desirability" of the more intuitive way
to ask that question:

        (exists $array[$N])

>I am quite comfortable with delete and exists only applying to
>hashes.  I do not understand why making them work on pseudo-hashes
>requires that their semantic be stretched to cover arrays as well.
>Please hit me over the head once or twice more; maybe I'll understand
>then.

I'm reluctant to forcefully make arguments to convince people on this
issue.  If the benefits are not immediately obvious, I begin to suspect
that perhaps its not the right thing to do after all.


Sarathy
gsar@ActiveState.com


Follow-Ups from:
Bart Schuller <schuller@lunatech.com>
Nick Ing-Simmons <nick@ing-simmons.net>
References to:
Tom Christiansen <tchrist@chthon.perl.com>

[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]