If you are using 5.10 or later, you can specify _ as the prototype for trim . If you are using earlier versions, use Axeman's answer :
As the last character of the prototype or immediately before the semicolon, you can use _ instead of $ : if this argument is not specified, $_ will be used instead.
use strict; use warnings; my @x = ("bla ", "ha 1"); sub trim(_) { my ($x) = @_; $x =~ s!\s+$!!; $x } print map trim, @x;
By the way, do not use $a and $b outside the sort comparator: they are not protected from strict checks.
However, I prefer not to use prototypes for functions, which I write mainly because their use makes it difficult to mentally analyze the code. Therefore, I would prefer to use:
map trim($_), @x;
See also perldoc perlsub :
This is all very powerful, of course, and should only be used in moderation to make the world a better place.
source share