Problem: the s/../.../ operator and the Perl map are required, expecting you to want to change each input element; Perl does not actually have a built-in for a functional map , which gives its results without changing the input.
When using s option is to add the r modifier:
#!/usr/bin/perl my @input = ( "a.txt" , "b.txt" , "c.txt" ) ; my @output = map { s/\..*$//r } @input ; print join(' ', @output), "\n";
The general solution (suggested by derobert) is to use List :: MoreUtils :: apply :
#!/usr/bin/perl use List::MoreUtils qw(apply); my @input = ( "a.txt" , "b.txt" , "c.txt" ) ; my @output = apply { s/\..*$// } @input ; print join(' ', @output), "\n";
or copy its definition into your code:
sub apply (&@) { my $action = shift; &$action foreach my @values = @_; wantarray ? @values : $values[-1]; }
reinierpost
source share