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.
}
####
@trimmed = trim(@not_trimmed);
####
foreach (@untrimmed) {
say trim;
}
####
sub trim (_;@) {
# ... as above
}
my @data = (' a', ' b ' , 'c ');
say trim (@data);