bin/cavaspazi

filter: Statementstmt 91.2% 52 / 57 Branchbran 62.5% 15 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 83.9% 73 / 87 CC 1 SCAR 0.0
CC
1
Cov
83%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Acme/Cavaspazi.pm ↑ Summary
1 #!/usr/bin/env perl
2 # PODNAME: cavaspazi
3 # ABSTRACT: a simple script to remove spaces from filenames or file contents
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 my $rename = 0;
12 3 my $substitute = 0;
13 3 my $dry_run;
14 my $verbose;
15 3 my $help;
16 3 GetOptions(
17 'rename|r' => \$rename,
18 'substitute|s' => \$substitute,
19 'dry-run|n' => \$dry_run,
20 'verbose' => \$verbose,
21 'h|help' => \$help,
22 );
23  
24 3 ▶pod2usage({-exitval => 0, -verbose => 2}) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 3 3
25  
26 3 my $cnt = 0;
27 3 my $tot = scalar @ARGV;
28 3 for my $file (@ARGV) {
29 5 $cnt++;
30 5 my $basename = basename($file);
31 5 my $dirname = dirname($file);
32 # check if file exists
33 5 ▶ if (!-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 5 5
34 0 print STDERR "$cnt/$tot File $file does not exist. Skipping...\n";
35 0 next;
36 }
37 5 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 3 2 5
38 3 ▶ my $type = -d $file ? 'directory' : 'file';
Branch: 1 branch
BranchTrueFalseTotal
-d $file ? : 1 2 3
39 3 print STDERR "$cnt/$tot Processing $dirname -> $basename [$type]...\n"
40 }
41 5 ▶ if ($rename) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rename) { } 4 1 5
elsif ($substitute) { } 1 0 1
42  
43 4 my $new_file = $basename;
44 4 $new_file =~ s/ /_/g;
45 4 my $dest = File::Spec->catfile($dirname, $new_file);
46 4 ▶ if ($dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dry_run) { } 2 2 4
47 2 print "#mv \"$file\" \"$dest\"\n";
48 } else {
49 2 ▶ rename "$file", "$dest" or die "Can't rename $file to $dest: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename "$file", "$dest" or die "Can't rename $file to ${dest}: $!" 2 0 2
50 }
51 }
52 elsif ($substitute) {
53 1 ▶ if (-d $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file) 0 1 1
54 0 ▶ print STDERR "Skipping: substitute only works with files...\n" if ($verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
55 0 next;
56 }
57 1 ▶ open my $fh, '<', $file or die "Can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Can't open ${file}: $!" 1 0 1
58 1 my $content = do { local $/; <$fh> };
59 1 close $fh;
60 1 $content =~ s/ /_/g;
61 1 ▶ open $fh, '>', $file or die "Can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $file or die "Can't open ${file}: $!" 1 0 1
62 1 print $fh $content;
63 1 print STDERR $content;
64 1 close $fh;
65 }
66 else {
67 0 die "You must specify either the -r or -s option";
68 }
69 }
70  
71 __END__
« blib/lib/Acme/Cavaspazi.pm ↑ Summary