So, one of my favorite pieces of perl is Text::Trim, which uses contexts to do Just The Right Thing. Here is the trim() function, with some comments that I wrote:
sub trim { @_ = @_ ? @_ : $_ if defined wantarray; # When the caller is looking for a return value # (not void context), make a copy of the arguments, # so the original value is not changed. # If there are no arguments passed, # copy the value of $_ instead. for (@_ ? @_ : $_) { next unless defined; s/\A\s+//; s/\s+\z// } # If @_ contains any values, perform the # substitution on all defined values. If @_ contains # no values, perform the substitution on $_ instead. return @_ if wantarray || !defined wantarray; # return the changed values in array context # or (for a reason which escapes me) void context. if (my @def = grep defined, @_) { return "@def" } else { return } # In scalar context, return the results of # the substitution on all # defined values, if any. Otherwise just return. }
This code is pretty hard to read, mostly because of the punctuation variables. But it allows the caller to do
@trimmed = trim(@not_trimmed);
or
foreach (@untrimmed) { say trim; }
or
trim(@data);And the results make sense.
But, as with so many things, it doesn't work with lexical $_. I thought maybe I could get this to work with the new underscore _ prototype, but unfortunately that imposes scalar context on the argument. In other words,
sub trim (_;@) { # ... as above } my @data = (' a', ' b ' , 'c '); say trim (@data);
displays not "abc" but "3", since that's the value of @data in scalar context (the number of elements).
I can't think of any way around this that allows the flexibility of the Text::Trim calling conventions and also the use of lexical $_.
Any ideas?
In reply to underscore prototype imposing scalar context by Aaronrp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |