bin/cavaspazi

filter: Statementstmt 92.1% 47 / 51 Branchbran 60.0% 12 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 84.4% 65 / 77 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 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 my $rename = 0;
12 2 my $substitute = 0;
13 2 my $dry_run;
14 my $verbose;
15 2 my $help;
16 2 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 2 ▶pod2usage({-exitval => 0, -verbose => 2}) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 2 2
25  
26 2 for my $file (@ARGV) {
27 3 my $basename = basename($file);
28 3 my $dirname = dirname($file);
29 # check if file exists
30 3 ▶ if (!-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 3 3
31 0 print STDERR "File $file does not exist. Skipping...\n";
32 0 next;
33 }
34 3 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 3 0 3
35 3 ▶ my $type = -d $file ? 'directory' : 'file';
Branch: 1 branch
BranchTrueFalseTotal
-d $file ? : 1 2 3
36 3 print STDERR "Processing [$type] $file...\n"
37 }
38 3 ▶ if ($rename) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rename) { } 2 1 3
elsif ($substitute) { } 1 0 1
39  
40 2 my $new_file = $basename;
41 2 $new_file =~ s/ /_/g;
42 2 my $dest = File::Spec->catfile($dirname, $new_file);
43 2 ▶ if ($dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dry_run) { } 0 2 2
44 0 print STDERR "#mv \"$file\" \"$new_file\"";
45 } else {
46 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}: $!" 0 2 2
47 }
48 }
49 elsif ($substitute) {
50 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}: $!" 0 1 1
51 1 my $content = do { local $/; <$fh> };
52 1 close $fh;
53 1 $content =~ s/ /_/g;
54 1 ▶ open $fh, '>', $file or die "Can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $file or die "Can't open ${file}: $!" 0 1 1
55 1 print $fh $content;
56 1 print STDERR $content;
57 1 close $fh;
58 }
59 else {
60 0 die "You must specify either the -r or -s option";
61 }
62 }
63  
64 __END__
« blib/lib/Acme/Cavaspazi.pm ↑ Summary