in reply to Re^3: What's most efficient way to get list context? (count)
in thread What's most efficient way to get list context?
OK, here is what I was trying to convey when I wrote the line you quoted:
As you can see, for all the cases you cited, when perl is coaxed into evaluating the RHS in a list context, and then the returned list is put in a scalar context, the result is the size of the list. I am not sure what is the correct wording to describe what I illustrate above, but it is clearly distinct from the fact that context-sensitive subexpressions will make idiosyncratic choices about how they respond to different contexts.DB<1> @a = qw(foo bar baz) DB<2> @h{@a} = (1) x @a DB<3> p $s = () = @a 3 DB<4> p $s = () = (1, 2, 3, localtime()) 12 DB<5> p $s = () = %h 6 DB<6> p $s = () = getpwuid($<) 9 DB<7> p $s = () = sort @a 3
In fact, as I illustrated with the m// example in my first reply to you, it is precisely the fact that different context-sensitive operations will make idiosyncratic choices for how they respond to a scalar context that makes it desirable to be able to explicitly instruct these operations to assume a list context, which is what the = () = kluge does.
I readily admit that this is one of the areas of Perl that I have the hardest time with, as I've stated elswhere, so I really appreciate this discussion. I think our discrepancy here gets at the root of why this is such a persistent blind spot for me. As I'm beginning to understand it, there is a nasty tension between the whole DWIM thing on the one hand, and the idea that functions should do something "useful", even if idiosyncratic, depending on context. For DWIM to be possible, I think some consistency is necessary, otherwise "what I mean" will constantly be confounded by whatever usually-useful-but-idionsyncratic context-sensitive behavior functions may choose to adopt. But such consistency would prevent functions from behaving as usefully as possible. I think the only way to preserve both is to give the programmer the ability to explicitly specify context, just like we have the ability to explicitly specify precedence with ( )'s.
Update: Fixed link.
the lowliest monk
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: What's most efficient way to get list context? (count)
by merlyn (Sage) on Apr 15, 2005 at 14:44 UTC | |
by Roy Johnson (Monsignor) on Apr 15, 2005 at 15:18 UTC | |
by merlyn (Sage) on Apr 15, 2005 at 16:11 UTC | |
by Roy Johnson (Monsignor) on Apr 15, 2005 at 16:45 UTC |