[EMAIL PROTECTED] (Jeffrey Baker) wrote:
>Cliff Rayman wrote:
>>
>> `perldoc -f defined` yields a couple of sentences:
>>
>> You may also use C<defined()> to check whether a subroutine exists, by
>> saying C<defined &func> without parentheses. On the other hand, use
>> of C<defined()> upon aggregates (hashes and arrays) is not guaranteed to
>> produce intuitive results, and should probably be avoided.
>
>I hate it when that happens.
>
>> why not use:
>>
>> if(@foo_in)
>>
>> instead of:
>>
>> if (defined @foo_in)
>
>Yeah. I guess the reason I do the latter is b/c I want the code to
>reflect what I am actually trying to test. I don't really want to test
>the trueness of @foo, I want to test for it's existence. But in perl
>the operation is overloaded. Feh.
>
>I think I will test for defined $foo_in[0] instead.
Are you sure? I'm still not sure you understand about defined(@list). It
actually tests whether Perl has allocated memory for the structure, not whether
the structure has ever been used or anything like that. And even the present
behavior may change without notice.
When you do "my @list = &func();" and &func returns no values, @list will be an
empty list. And it will evaluate in a boolean context to 0 (the number of
elements), which is false. You shouldn't really care whether Perl has
allocated memory to it or not, because different versions of Perl will give you
different results with that test.
Finally, what if &func returns (undef, 1) or even (undef)? If you use your
"defined $list[0]" test, you'll get a false result here, when &func actually
did return stuff.
Of course, I don't know your specific application, but "defined @list" is one
of the red flags of code.
------------------- -------------------
Ken Williams Last Bastion of Euclidity
[EMAIL PROTECTED] The Math Forum