What am I missing
The $word* variables aren't getting assigned "an array", they are getting assigned the values from the array.
@collect = split( / /, 'This is a sentence.' ); |
- The array on the LHS means that the assigment is in list context.
- Split returns a list of scalars.
- In list context, the elements of what's on the LHS get assigned to the values from the list on the RHS, so the @collect array is populated from the values returned by split
|
( $word1, $word2, $word3, $word4 ) = @collect; |
- The (...) on the LHS mean that the assignment is in list context
- Per List value constructors, the @collect array gets evaluated in list context, which results in the list (on the RHS) consisting of the elements from the @collect array
- The assignment then happens in list context, so the leftmost item of the list on the LHS ($word1) gets assigned the value of the leftmost item from the list on the RHS, and so on
|
print join( q[ ; ], ( $word1, $word2, $word3, $word4 ); |
=> syntax error at parv.pl line 3, near ");"
but with a second ) on that line to allow it to compile:
- the words are given list context by the parentheses (unnecessarily, because join already gives list context)
- join concatenates the elements of that list, using space-semicolon-space as the separator, into a string
- That string is then printed.
|
(LHS = "Left-hand side", RHS = "Right-hand side": in these cases, relative to the assignment operator =)
| [reply] [Watch: Dir/Any] [d/l] [select] |