Question

In perldata, I found the following examples and explanations.

@foo = ('cc', '-E', $bar);

assigns the entire list value to array @foo, but

$foo = ('cc', '-E', $bar);

assigns the value of variable $bar to the scalar variable $foo. 

This really confuses me, so $foo is equivalent to $bar? How to understand the difference between @foo and $foo

Was it helpful?

Solution

The examples in perldata:

@foo = ('cc', '-E', $bar);
$foo = ('cc', '-E', $bar);

Because @foo creates a list context, all the values in the parens are assigned to @foo. $foo on the other hand is a scalar, and so is only assigned the last element in the list, because it is in scalar context.

It is equal to saying:

'cc', '-E';
$foo = $bar;

In Perl, a scalar, like $foo, can only hold a single value, and so the rest of the list is simply discarded. An array, like @foo will slurp as many values as the list holds.

In Perl, it is allowed to have the same name on variables of different types. @foo and $foo will be considered two different variables.

OTHER TIPS

Expressions are allowed to have different meanings depending on which context they're evaluated in. The three main contexts are list, scalar, and void, though there exists several subcontexts of scalar context (boolean, string, and numeric being the most important ones).

The comma operator is no exception to this rule. In list context, the comma operator acts as a list concatenation operator, evaluating its operands in list context and combining the resulting lists into a single list. This is likely the context you're familiar with when dealing with the comma operator.

However, in scalar context, the comma operator functions much like the comma operator in C; it evaluates a sequence of expressions and discards their results, except for the rightmost expression which it returns (as a side note, the expressions that are discarded are evaluated in void context, and the expression that's returned is evaluated in scalar context). To learn how each of the perl operators behave in different contexts, I suggest reading perlop.

In order to fully understand context, you have to realize that the outermost operator enforces a context on its operands, whose operators then enforce a context on their operands, and so on (another side note: the outermost expression of a line is always evaluated in void context). So, for example, when the assignment operator is being used with an array or hash variable (beginning with a % or @), the right-hand side of the assignment is consequently evaluated in list context. If the variable is a scalar, however, the right-hand side of the assignment is evaluated in scalar context instead. This is why the comma operators in the assignments below:

@foo = ('cc', '-E', $bar);
$foo = ('cc', '-E', $bar);

act in completely different ways.

For more information on how you can write code that controls or reacts to context, read about the scalar and wantarray operators.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top