Context is Everything
When writing Perl programs, you really must be aware of the two main
contexts: Scalar and List. Things can behave differently when evaluated
in one or the other context.
We'll employ probably the most well know example, assigning an array:
my @list = @array; # @array is in list context
my $scalar = @array; # @array is in scalar context
The first case above assigns @array to another array, providing list
context, so it returns the list of its contents. The second example
assigns @array to a scalar variable, thus putting it in scalar context.
An array in scalar context returns the size of the array. Now, one
twist on the second example places @array in list context.
my ($scalar) = @array; # @array is in list context;
The parentheses around the scalar indicates a list of scalars (in this
case, a list of length one) on the assignment's left side, thus putting
the @array in list context. Now @array returns the list of its contents
and the first element is assigned to $scalar (the rest of the list
returned by the array is ignored).
Another example uses the keys() function of a hash. In list context, it
returns the given hash's list of keys; but in scalar context, it
returns the number of keys in the hash. A hash itself in list context
returns the full list of key value pairs (in the same sequence as the
keys() function returns the keys). However, in scalar context it
returns information about the underlying hash structure.
my %hash = (one => 1, two => 2);
print %hash, "\n"; # prints: one1two2
print scalar %hash, "\n"; # prints: 2/8
The print() function provides a list context (the function expects to
receive a list of arguments). The scalar() function can be used to
explicitly evaluate an expression in a scalar context (as we do in the
last example above). That final version tells us that our hash
currently has 8 buckets allocated and that two are being used.
Now we've already seen that functions can return different results
depending on context with the keys() function, so let's look at the
localtime() function (see: perldoc -f localtime). Operators can also
act context dependently, as with the match operator m// (and that
behaviour is also modified by the /g modifier). The x operator (string
replication) works differently if its left side is a scalar or a list.
Functions themselves may also supply context to their arguments. Take,
for example, the join() function. It first expects a scalar value as an
argument holding the string to be used as the join separator. If you
thought you could just put all the arguments to join into a single
array and pass that, then you wouldn't get the results you wanted.
my @args = (":", 1,2,3);
my $str = join @args;
print $str; # $str is empty
Above, the join() function expected a scalar as the first argument and
evaluated the @args array in a scalar context to get the separator
character (in this case the size of the array which is 4). No arguments
followed to join together.
If a function or operator behaves differently depending on scalar or
list context, it is documented in the relevant documentation pages (see
the perlfunc and perlop manpages).
Next Week: More on Context (boolean and operator contexts)
» posted by ITworld staff
ITworld
Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world
jfruh
Apple syncing patent can't come soon enough
pasmith
New Twitter features borrow from 3rd party clients
Esther Schindler
Open Source Changes the Software Acquisition Process
mikelgan
How to set up continuous podcast play on the new iTunes
David Strom
Five important Windows 7 mobility features
sjvn
Guard your Wi-Fi for your own sake
Sandra Henry-Stocker
Grepping on Whole Words
Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325
Join the conversation here
Quick, practical advice for IT pros. Made fresh daily.
Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.













