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
Symantec Backup Exec 12 and Backup Exec System Recovery 8 deliver industry leading Windows data protection and system recovery. Download this whitepaper to find out the top reasons to upgrade and how to get continuous data protection and complete system recovery.
Data and system loss — from a hard drive failure, malicious attack, natural disaster, or simple human error — can happen anytime. Don’t leave your business vulnerable. Make sure you have a secure recovery strategy in place. Symantec's latest backup and system recovery technology can efficiently restore critical applications, individual emails and documents and even restore your entire system in minutes in the event of a loss.
Businesses face a growing challenge to ensure that the IT environment is properly protected. Backup Exec 12 integrates with other applications in the Symantec family of products, to complement your current data protection strategy, keep your data securely backed up and make it recoverable when you need it most.
VMware ESX Server in the Enterprise
By Edward L. Haletky
Published Dec 29, 2007 by Prentice Hall.
Enter now! | Official rules | Sample chapter
Green IT
By Toby Velte, Anthony Velte, Robert C. Elsenpeter
To be published Oct. 10, 2008 by McGraw Hill Professional
Enter now! | Official rules | About the book







