When I crop a string, I don't often want to keep the original. It would be nice to have a sub abstraction, but also no need to fuss about temporary values.
Turns out we can do just that, as perlsub explains:
Any arguments passed are displayed in the @_ array. Therefore, if you call a function with two arguments, they will be stored in $_[0] and $_[1] . The @_ array is a local array, but its elements are aliases for real scalar parameters. In particular, if the element $_[0] updated, the corresponding argument is updated (or an error occurs if it is not updated).
In your case, trim becomes
sub trim { for (@_) { s/^ \s+ //x; s/ \s+ $//x; } wantarray ? @_ : $_[0]; }
Remember that map and for are cousins, so with a loop in trim you no longer need map . for instance
my $line = "1\t 2\t3 \t 4 \t 5 \n"; my ($a, $b, $c, $d, $e) = split(/\t/, $line); print "BEFORE: [", join("] [" => $a, $b, $c, $d), "]\n"; trim $a, $b, $c, $d; print "AFTER: [", join("] [" => $a, $b, $c, $d), "]\n";
Conclusion:
BEFORE: [1] [2] [3] [4]
AFTER: [1] [2] [3] [4]