|Just another Perl shrine|
Re: On Scalar Contextby ambrus (Abbot)
|on Apr 24, 2004 at 21:11 UTC||Need Help??|
You've left out an important case from the table: regular expressions.
Here's how they work wrt contexts (correct me where I'm wrong).
Regexps without the /g flag return true/false in saclar context (the list of captures or 1 if there are no captures, empty list when does not match).
Regexps with the /g flag match only once in scalar (or void) context and return true/false. (In list context, they match as many times as possible.)
Substitutions (s///) without the /g flag return true/false in either context, wit the g flag they return the number of successful matches in either context.
Also missing are the .. and ... operators which do quite different thing in scalar context than in list context.
List assignments (of form (LLIST)=EXPR) return the length of the right hand side.
As for your claim that less expressions return the length than not, it was quite obvious for me, as most expressions always return a 1 long list and the same one element in scalar context, all arithmetic operators and also the and, or operators work like this.
(Also there's this interesting issue about what a substr-assignment returns, but it's the same in scalar and list contexts, so it's unrelated to this thread.)