File Coverage

blib/lib/App/perlmv/scriptlet/remove_common_suffix.pm
Criterion Covered Total %
statement 11 11 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 15 15 100.0


line stmt bran cond sub pod time code
1             package App::perlmv::scriptlet::remove_common_suffix;
2              
3 2     2   34 use 5.010001;
  2         7  
4 2     2   13 use strict;
  2         4  
  2         55  
5 2     2   21 use warnings;
  2         4  
  2         242  
6              
7             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8             our $DATE = '2023-06-30'; # DATE
9             our $DIST = 'App-perlmv'; # DIST
10             our $VERSION = '0.607'; # VERSION
11              
12             our $SCRIPTLET = {
13             summary => q[Remove suffix that are common to all args, while preserving extension, e.g. (1-radiolab.mp3, 2-radiolab.mp3) -> (1.mp3, 2.mp3)],
14             code => sub {
15             package
16             App::perlmv::code;
17 2     2   15 use vars qw($COMMON_SUFFIX $TESTING $FILES $EXT);
  2         4  
  2         622  
18              
19             if (!defined($COMMON_SUFFIX) && !$TESTING) {
20             for (@$FILES) { $_ = reverse };
21             my $i;
22             for ($i=0; $i[0]); $i++) {
23             last if grep { substr($_, $i, 1) ne substr($FILES->[0], $i, 1) } @{$FILES}[1..@$FILES-1];
24             }
25             $COMMON_SUFFIX = reverse substr($FILES->[0], 0, $i);
26             for (@$FILES) { $_ = reverse };
27             # don't wipe extension, if exists
28             $EXT = $COMMON_SUFFIX =~ /.(\.\w+)$/ ? $1 : "";
29             }
30             s/\Q$COMMON_SUFFIX\E$/$EXT/;
31             $_;
32             },
33             };
34              
35             1;
36              
37             # ABSTRACT: Remove suffix that are common to all args, while preserving extension, e.g. (1-radiolab.mp3, 2-radiolab.mp3) -> (1.mp3, 2.mp3)
38              
39             __END__