When the function is localtime-ish, I make it work like that. I have several that return a human readable string in scalar context, but a list of (numeric) elements in list context.
If the function returns a list that has a logical order and a variable number of elements, I return wantarray ? @array : \@array. But not when the function has a prototype of (&@).
I find return @array; annoying, unless the function warns or dies in scalar context. Again, an exception is made for map-ish functions.
I often use void context for mutating values: (Or: The function becomes a procedure in void context)
This is great when working with a hash of things that are about to be displayed, transmitted, etc:my @bar = encode @foo; # mutated copy in @bar my $bar = encode @foo; # mutated copy in @$bar encode @foo; # @foo's elements are now encoded
my $row = $db->query('select ...')->hash; encode values %$row;
Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }
In reply to Re: What should be returned in scalar context?
by Juerd
in thread What should be returned in scalar context?
by tilly
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |