Ken Williams wrote:
> 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.

Actually that isn't what defined does.  defined is supposed to indicate
whether something has a real value or not.  It is not and should not be
related to whether Perl has allocated memory for it, although one
implies the other.  If no memory has been allocated, then the value is
certainly undefined.  However, if memory has been allocated the value
may or may not be defined.

I understand that defined doesn't work on lists and hashes.  I had
simply forgotten about that bit of trivia.  defined most certainly does
work on list *members* and hash *entries*.  For example, 'defined
$hash{key}' is definitely going to work.  Same for 'defined $array[0]'.

> 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.

I don't care whether Perl has allocate memory or not.  All I care about
is whether or not there are any defined entries in the list, which I
think is most clearly expressed as 'if (defined $list[0])'.  What is
more clear than that?  'if (@list)' could certainly work because an
empty list evaluates to 0 in a scalar context.  But the code doesn't
look like what I am intending to do.

> 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.

Obviously I need to know the calling conventions and return values for
all of my functions :)  In this case the function must either return an
empty list, or a list consisting of only defined scalar values.

> Of course, I don't know your specific application, but "defined @list" is one
> of the red flags of code.

Right.  As I said I simply managed to let that bit of info slip my
mind.  Perhaps I should use lint or some such.

-jwb
-- 
Jeffrey W. Baker * [EMAIL PROTECTED]
Critical Path, Inc. * we handle the world's email * www.cp.net
415.808.8807

Reply via email to