FAQ 4.54 Why does defined() return true on empty arrays and hashes?
FAQ 4.54 Why does defined() return true on empty arrays and hashes?
am 03.12.2007 21:03:02 von PerlFAQ Server
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
------------------------------------------------------------ --------
4.54: Why does defined() return true on empty arrays and hashes?
The short story is that you should probably only use defined on scalars
or functions, not on aggregates (arrays and hashes). See "defined" in
perlfunc in the 5.004 release or later of Perl for more detail.
------------------------------------------------------------ --------
The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.
If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
Re: FAQ 4.54 Why does defined() return true on empty arrays and
am 04.12.2007 04:44:37 von xueweizhong
I see the rules in perlfun:
perldoc -tf defined:
Use of "defined" on aggregates (hashes and arrays) is
deprecated. It used to report whether memory for that
aggregate
has ever been allocated. This behavior may disappear in
future
versions of Perl. You should instead use a simple test for
size:
But lost in the difference below:
case 1:
#! /bin/perl -l
print defined @a ? "defined--acutally means memory allocated" :
"undefined";
__END__
undefined
case 2:
#! /bin/perl -l
@a=undef;
print defined @a ? "defined--acutally means memory allocated" :
"undefined";
__END__
defined--acutally means memory allocated
case 3:
#! /bin/perl -l
@a=();
print defined @a ? "defined--acutally means memory allocated" :
"undefined";
__END__
undefined
Why case 2 is different from case 3? Does it means defined on
aggregator is totally meaningless in modern Perl?
-Todd
Re: FAQ 4.54 Why does defined() return true on empty arrays and
am 04.12.2007 07:58:50 von Ben Morrow
Quoth Todd :
>
> I see the rules in perlfun:
>
> perldoc -tf defined:
>
> Use of "defined" on aggregates (hashes and arrays) is
> deprecated. It used to report whether memory for that
> aggregate
> has ever been allocated. This behavior may disappear in
> future
> versions of Perl. You should instead use a simple test for
> size:
>
> But lost in the difference below:
>
> case 1:
>
> #! /bin/perl -l
> print defined @a ? "defined--acutally means memory allocated" :
> "undefined";
>
> __END__
> undefined
>
> case 2:
>
> #! /bin/perl -l
> @a=undef;
This puts one element (undef) into @a. scalar(@a) at this point is 1.
> print defined @a ? "defined--acutally means memory allocated" :
> "undefined";
>
> __END__
> defined--acutally means memory allocated
>
>
> case 3:
> #! /bin/perl -l
> @a=();
This has no effect: @a was empty before, and is still empty afterwards.
defined on an array currently returns true if the array has at least one
element, or the array has 'get' magic, or the array is tied. It tells
you nothing about whether the underlying AV has actually been allocated
or not, because just referring to @a is enough to make sure that it has
been.
> print defined @a ? "defined--acutally means memory allocated" :
> "undefined";
>
> __END__
> undefined
>
> Why case 2 is different from case 3? Does it means defined on
> aggregator is totally meaningless in modern Perl?
Please don't use defined on aggregates. It doesn't tell you anything
useful, and may at some point change its behaviour. That's why it says
not to use it in perlfunc. Use scalar(@a) or scalar(keys %a) instead.
Ben
Re: FAQ 4.54 Why does defined() return true on empty arrays and
am 04.12.2007 09:11:52 von xueweizhong
Thanks, Ben. You make it clear.
So it does have real & useful meaning when operate on aggregators. :-(
-Todd
Re: FAQ 4.54 Why does defined() return true on empty arrays and
am 04.12.2007 09:20:53 von xueweizhong
Ben Morrow wrote:
> > @a=undef;
>
> This puts one element (undef) into @a. scalar(@a) at this point is 1.
I'm affected by lisp language: where `()' and `nil' are same:-(
-Todd
Re: FAQ 4.54 Why does defined() return true on empty arrays and hashes?
am 04.12.2007 16:13:23 von merlyn
>>>>> "Todd" == Todd writes:
Todd> Ben Morrow wrote:
>> > @a=undef;
>>
>> This puts one element (undef) into @a. scalar(@a) at this point is 1.
Todd> I'm affected by lisp language: where `()' and `nil' are same:-(
So the first thing to learn is that Lisp's "nil" isn't Perl's "undef".
Get over that, and you wouldn't have made that mistake.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!