in reply to inline caching a list-generating sub
Rather than "if an actual list is returned", just say that you want the function to return the last value if used in a scalar context. Then people won't have to try to figure out what lists you consider to be "actual" vs. whatever the other lists are. (:
What "expense" are you complaining about here? I don't see any big CPU or memory cost being incurred in the last case. Perhaps clarity? If so, just write the code less densely. For example, rather than using a slice just to get the side effect of returning the last item in a scalar context (a justification that would require more guessing when your code is later read), make it clear what you wanted:
my %foo_cache; sub foo { my $ret = $foo_cache{join",",@_}; ||= [ do { ... } ]; return wantarray ? @$ret : $ret->[-1]; }
- tye
|
|---|