Void, Scalar, and List Context

Learn about the void, scalar, and list contexts in Perl.

Amount context

Amount context governs how many items we expect an operation to produce. Think of subject-verb number agreement in English. Even without knowing the formal description of this principle, you probably understand the error in the sentence, “Perl are a fun language.” (In terms of amount context, we could say that the verb “are” expects a plural noun or noun phrase.) In Perl, the number of items we request influences how many we receive.

Suppose the functionPerl uses the sub built-in to declare a function. called find_chores() sorts our household to-do list in order of priority. The number of chores we expect to read from our list influences what the function produces. If we expect nothing, we’re just pretending to be busy. If we expect one task, we have something to do for the next fifteen minutes. If we have a burst of energy on a free weekend, we could get all of our chores done.

Get hands-on with 1200+ tech skills courses.