Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your question is ultimately "why is @backwards in scalar context in line 6", which begs the question, "how can I determine a term's context?".</p> <p>Context is determined by "what is around" (i.e. its "context") the term. </p> <p>How can I determine a term's context? By looking at the operator/function that is using the term.</p> <p>What steps could you follow to figure out the context for @backwards yourself if you didn't have helpful stackoverflow folks around to tell you its context?</p> <p>Here we have</p> <pre><code>print @backwards."\n" </code></pre> <p>so there are two operators/functions. How do we know which one provides context to @backwards? By consulting precedence. Near the top of perlop.pod we have Perl's precedence chart (print is a "list operator"):</p> <pre><code>left terms and list operators (leftward) ... left + - . ... nonassoc list operators (rightward) </code></pre> <p>Oh great, now we need to know whether print is leftward or rightward. By consulting the "Terms and List Operators (Leftward)" section in perlop (right after the precedence list) we see that print is rightward here, because we have not enclosed its arguments in parenthesis.</p> <p>So concatenation is higher precedence, so concatenation provides context to @backwards.</p> <p>Next step is to check the docs (perlop again) for concatenation:</p> <pre><code>Binary "." concatenates two strings. </code></pre> <p>Strings are scalars, so binary "." concatenates two scalars.</p> <p>And we finally have it!</p> <p>@backwards has scalar context because concatenation provides scalar context to each of its operands.</p> <p>Woo. That was easy, wasn't it :-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload