3 ms·
I'll admit that this is baffling. The inner parentheses force evaluation of the two inner comma operators in the scalar context provided by the function protot
by chromatic 13y ago
I'll admit that this is baffling.
The inner parentheses force evaluation of the two inner comma operators in the scalar context provided by the function prototype. `1` and `3` get evaluated in void context and discarded, leaving `2` and `4` as the two arguments to the function. (I had to look up `+` in prototypes, however.)
Without a working understanding of lists and context, this example is undoubtedly baffling, but that's why the documentation exists.
- theOnliest 13y agoI guess I really meant "is baffling without a thorough understanding of context." I feel better about looking up the + now that I know you did too, though! I don't think I've ever seen code like that in the wild, and I've never seen a Perl programmer attempt to call a sub with nested parens, like what_are((1,2),(3,4)) before.
- perigrin 13y agoYou may not realized that's what you saw, but for example all of the Moose documentation uses effectively a nested param: has foo => ( is => 'ro' ); is equivalent to: has('foo', ('is', 'ro')); because Moose's `has` sugar is written as a exported function.
- theOnliest 13y agoThat's true, but I've never actually seen anyone call 'has' like that. Plus, Perl's behavior on lists makes sense (always flatten) if there's no mucking around with prototypes, so that's much less confusing than the behavior in the article.
- perigrin 13y agoThe first one I presented is the way the Moose documentation calls has, the way the Moose test suite typically calls has, and the way I and most of the rest of the Moose Cabal call has. The second one isn't common at all, but I have seen people both completely leave out the parentheses: has foo => is => 'ro', isa => 'Str', ...; or treat has as a straight function has(foo, is => 'ro', isa => 'Str'); both of which cause Perl::Tidy to do weird things.
- theOnliest 13y agoSorry, I meant I'd never seen it called the second way (nested parens). I also haven't seen the no-parens version, which looks really bizarre!