Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: On Scalar Context

by ambrus (Abbot)
on Apr 24, 2004 at 21:11 UTC ( #347891=note: print w/replies, xml ) Need Help??

in reply to On Scalar Context

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.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://347891]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2020-07-12 00:44 GMT
Find Nodes?
    Voting Booth?

    No recent polls found