This is the most important section in this lesson. In fact, it's the most important section in the entire tutorial. In fact, it wouldn't be an exaggeration to say that your entire career in using Perl will depend upon understanding this section. So if you've gotten away with skimming the text up to this point, this is where you should really pay attention.
That's not to say that this section is in any way difficult to understand. It's actually a simple idea: a given expression may mean different things depending upon where it appears. This is nothing new to you; it happens all the time in natural languages. For example, in English,1 suppose someone asked you what the word "read"2 means. It has different meanings depending upon how it's used. You can't identify the meaning, until you know the context.
The context refers to where an expression is found. As Perl is parsing your expression, it always expects either a scalar value or a list value.3 What Perl expects is called the context of the expression.4
5 + something # The something must be a scalar
sort something # The something must be a list
Even if something is the exact same sequence of characters, in one case it may give a single, scalar value, while in the other, it may give a list.5
Expressions in Perl always return the appropriate value for their context. For example, how about the "name"6 of an array. In a list context, it gives the list of elements. But in a scalar context, it returns the number of elements in the array:
@people = qw( fred barney betty );
@sorted = sort @people; # list context: barney, betty, fred
$number = 5 + @people; # scalar context: 5 + 3 gives 8
Even ordinary assignment (to a scalar or a list) causes different contexts:
@list = @people; # a list of three people
$n = @people; # the number 3
But please don't jump to the conclusion that scalar context always gives the number of elements that would have been returned in list context. Most list-producing expressions 7 return something much more interesting than that.
Next: Using List_Producing Expressions in Scalar Context
[1]
If you aren't a native speaker of English, this analogy may not be obvious to you.
But context sensitivity happens in every spoken language, so you may be able to think
of an example in your own language.
Return to the page from whence you came
[2]
Or maybe they were asking what the word "red" means, if they were speaking rather than
writing a book. It's ambiguous either way. As Douglas Hofstadter said, no
language can express every thought unambiguously, especially this one.
Return to the page from whence you came
[3]
Unless, of course, Perl is expecting something else entirely. There are other
contexts that aren't covered here. In fact, nobody knows how many contexts Perl
uses; the biggest brains in all of Perl haven't agreed on an answer to that yet.
Return to the page from whence you came
[4]
This is no different than what you are used to in human languages. If I make a
grammatical mistake, you notice it right away, because you expect certain words in
places certain. Eventually, you'll read Perl this way, too, but at first you
have to think about it.
Return to the page from whence you came
[5]
The list may be just one element long, of course. It could also be empty, or it
could have any number of elements.
Return to the page from whence you came
[6]
Well, the true name of the array @people is just people. The
@-sign is just a qualifier.
Return to the page from whence you came
[7]
But with regard to the point of this section, there's no difference between a "list-
producing" expression and a "scalar-producing" one; any expression can produce a list
or a scalar, depending upon context. So when we say "list-producing expressions"
we mean expressions that are typically used in a list context and that therefore
might suprise you when they're used unexpectedly in a scalar context (like
reverse or @fred).
Return to the page from whence you came