|Perl Monk, Perl Meditation|
Re: Differ. array and List (there is no List)by tye (Cardinal)
|on Apr 26, 2005 at 03:58 UTC||Need Help??|
You'll get a lot of people telling you that a "list" lives on the stack. You'll probably get a few people telling you that a "list" is things separated by commas. In fact, the word "list" is used for many different things in the Perl documentation and so anyone who tries to define it strictly for you is likely to cause you confusion when you run into one of the many parts of the Perl documentation that use the word "list" to mean something else.
In the Perl documentation, it is best to interpret "list" as an English word that describes several items of nearly any type associated together in a specific order. Within parts of the documentation, you will probably be able to infer more precisely what the word "list" is being used for within that section. But there are also parts where the word "list" is used in one way rather close to it being used in quite a different way.
An array, however, does have a rather precise meaning in Perl. It is something that you can push or pop or shift or unshift or splice. An array is a list of scalar values held in a structure that gives you access to make modifications to that list. It often has a name but it can be anonymous. Array is a type of Perl variable. You can take a reference to it.
The most important Perl concept that comes closest to being a "List" (capital "L") is better named "an operation that would return a list of scalar values if used in a list context". The concept of "a list of scalar values on the stack" is an oft-invoked image but I don't find it useful (unless you are trying to define "List", in which case it is certainly a simple definition, though a misleading one and not nearly the most important one, in my experience).
The "operation that would return a list of scalar values if used in a list context" aims right at the heart of Perl's list and scalar contexts. These will often be called "list" for short.
A list of expressions separated by commas is one way to write an "operation that would return a list of scalar values if used in a list context". Note that the list of expressions is not the same list as the list of scalar values that might be returned. This is important because, in a scalar context, this list of expressions returns the value that is returned by the last expression when it is called in a scalar context. This can be quite different from the last value of the list of scalar values that would be returned if the list of expressions were called in a list context.
Many have previously summed up what the above paragraph describes by saying that a list in a scalar context returns the last value of the list. So beware when you hear someone make restrictive proclamations about "lists" in Perl.