# i don't understand 'context sensitivity'

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
==============================================================================================

@array = ('a', 'b', 'c');

and

(\$array[0], \$array[1], \$array[2]) = ('a', 'b', 'c');

do not do the same thing. The first clears the array, and gives it
three elements with the given values; the second changes the values of
the first three elements of the array, and leaves the rest alone.

==============================================================================================

from http://japhy.perlmonk.org/articles/pm/2000-02.html

what is different between

@array = ('a', 'b', 'c');

and

(\$array[0], \$array[1], \$array[2]) = ('a', 'b', 'c');

?

help me..

## Re: i don't understand 'context sensitivity'

On 2011-01-14, Anster wrote:

# Begin example 1
my @array = ('q', 'w', 'e', 'r');

print "Before:\n";
print "\$_\n" for @array;
print "After:\n";
@array = ('a', 'b', 'c');
print "\$_\n" for @array;

# End example 1

# Begin example 2
my @array = ('q', 'w', 'e', 'r');

print "Before:\n";
print "\$_\n" for @array;
print "After:\n";
(\$array[0], \$array[1], \$array[2]) = ('a', 'b', 'c');
print "\$_\n" for @array;

# End example 2

To see the difference between two things, just experiment! ;-)

--
Vivien MOREAU

## Re: i don't understand 'context sensitivity'

There is nothing relating to modules in your post.

Why did you post it to a newsgroup about modules?

You have found the wrong newsgroup.

There is nothing regarding different contexts in your post.

The asssignment operator is in list context in both of your examples.

It must be something else that you are not understanding...

This assigns a list to @array, stomping over its current contents.

\$num = 42;

This assigns a scalar to \$num, stomping over its current contents.

Same thing.

This assigns a list to another list, first on the right into first
on the left, second on the right into second on the left etc.

Since the list on the left is composed of only the first three elements
of @array, only the first three elements of @array are affected. All
other elements of @array, if any, remain unaffected.

Let's try another example without any arrays.

my \$var1 = 1;
my \$var2 = 2;
my \$var3 = 3;
my \$var4 = 4;

(\$var1, \$var2, \$var3) = (10, 20, 30);

The values of \$var1, \$var2 and \$var3 were changed. The value
of \$var4 remains unaffected.

Japhy and Rick's example in the "A Word on Context" section
is a poor one. It does not illustrate the difference between
list context and scalar context.

The difference between scalar context and list context is the same
as the difference between "singular" and "plural".

It even says so in the relevant part of Perl's standard documentation.
From the "Context" section in perldata.pod:

Perl overloads certain operations based on whether the expected
return value is singular or plural.  Some words in English work
this way, like "fish" and "sheep".

People understand the idea of context quite well, we do it every day.

It is just that we do it in a natural language rather than in a
programming language.

Consider:

I have a sheep.

I have some sheep.

In the first one, "sheep" is a singular noun (scalar).

In the second one, "sheep" is a plural noun (list).

People have no trouble deciding which is which.

How do they do that?

By looking at what is around the word "sheep". They see "a" which
conveys a single animal and "some" which conveys multiple animals.

What is another term for "what is around" a thing?

The "context" of the thing!

An assignment is a "thing". How can we tell whether it is a scalar
assignment or a list assignment? By looking at what is around it.

\$num = 42;

Here, what is around the assignment operator are scalars, so this is
a scalar assignment.

(\$var1, \$var2, \$var3) = (10, 20, 30);

Here, what is around the assignment operator are lists, so this is
a list assignment.

The first assigns to the whole array.

The second assigns to only part of the array.

--