in reply to What's most efficient way to get list context?
I don't think I could care less which is more efficient, but I chuckled when I read your "honest work" note and so thought I should mention that the return value from () = ... (in a scalar context) is the count of items in the right-hand side list that gets thrown away. So instead of just fetching the first one, you have to count all of them. I kinda doubt that actually has much to do with your benchmark results.
Regarding your last paragraph: What is the point of saying "I want a list context" if you don't actually want something from the list? Any use other than benchmarks? Because I'm not at all sad that Perl lacks a keyword that would have no use other than benchmarking. If you want a list, then you do something with the list and you doing that causes the list context.
The scalar keyword makes sense because this makes sense:
it puts a single item into @list even if function() would return lots of scalars if given a list context. The equivalent:my @list = scalar function();
my $scalar = list function();
doesn't make sense because there is no universal "list in scalar context" behavior. You have to specify which conversion (which has the side effect of inducing the context).
Sure, if what you really want is:
0; # void context below! list function(); # fake list context 0; # void context above!
then there doesn't really need to be any conversion. But, again, this isn't "productive" -- it is something you only do as a hack for unusual situations. So I don't miss having some official feature just for such trickery.
Note that you can put "return 0;" or just "0;" on the end of your benchmark subs in case you want to ensure you are getting void context (or use the source, of Benchmark.pm).
It's silly to have trick perl into it.
But what you are doing is a trick. So being required to write tricky code in order to do something this tricky is not silly at all. (:
- tye
|
|---|