blib/lib/Alvis/Utils.pm

filter: Statementstmt 38.4% 20 / 52 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 5 / 10 Podpod 0.0% 0 / 3 totaltotal 26.0% 25 / 96 CC 15 SCAR 39.2
CC
15
Cov
25%
open_file
110.0
__ANON__
20.0
_open_file_failed
6.0
CRAP 50.5
« blib/lib/Alvis/AinoDump.pm ↑ Summary blib/lib/Alvis/Document/Meta.pm »
1 package Alvis::Utils;
2  
3 3 require Exporter;
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use open ':utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our @ISA = qw(Exporter);
11  
12 #our @EXPORT = qw(open_file get_files);
13 3 our @EXPORT_OK = qw( open_file get_files absolutize_path );
14 3 our $VERSION = 0.01;
15  
16 #############################################################################
17 # Opens file and returns its filehandle.
18 #
19 # open_file('my_file');
20 # open_file('file' =>'my_file', 'die' => '0', 'msg' => 'message');
21 #
22 # Returns undef if file openning fails and 'die' => '0'
23 #
24 sub open_file
25 0 ▶{
Subroutine open_file: not called CC=10 SCAR=47.0
Pod undocumented
26 0 my %param = ('die' => 1);
27  
28 0 ▶ if (!defined($_[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[1]) { } 0 0 0
29 0 $param{'file'} = shift;
30 } else {
31 0 my %x = @_;
32 0 %param = map {$_ => $x{$_}} keys %x;
33 }
34  
35 0 my $filename = $param{'file'};
36 0 ▶ croak "Filename to open is not defined" if (!defined($param{'file'}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param{"file"} 0 0 0
37  
38 $param{'msg'} = "Cannot open file '$filename'"
39 0 ▶ if (!defined($param{'msg'}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param{"msg"} 0 0 0
40  
41 0 my $is_failed = 0;
42 0 local *FH;
43  
44 0 ▶ if ($filename =~ /\.bz2$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($filename =~ /\.bz2$/) { } 0 0 0
elsif ($filename =~ /\.gz$/) { } 0 0 0
45 0 ▶ open(FH, "bzcat $filename |")
Branch: 1 branch
BranchTrueFalseTotal
open FH, "bzcat $filename |" or _open_file_failed(\$is_failed, %param) 0 0 0
46 or _open_file_failed(\$is_failed, %param);
47 } elsif ($filename =~ /\.gz$/) {
48 0 ▶ open(FH, "zcat $filename |")
Branch: 1 branch
BranchTrueFalseTotal
open FH, "zcat $filename |" or _open_file_failed(\$is_failed, %param) 0 0 0
49 or _open_file_failed(\$is_failed, %param);
50 } else {
51 0 ▶ open(FH, $filename) or _open_file_failed(\$is_failed, %param);
Branch: 1 branch
BranchTrueFalseTotal
open FH, $filename or _open_file_failed(\$is_failed, %param) 0 0 0
52 }
53  
54 0 ▶ return ($is_failed ? undef: *FH);
Branch: 1 branch
BranchTrueFalseTotal
$is_failed ? : 0 0 0
55 }
56  
57 ############################################################################
58 sub _open_file_failed
59 0 ▶{
Subroutine _open_file_failed: not called CC=2 SCAR=17.9
60 0 my $is_failed = shift;
61 0 my %param = @_;
62 0 ▶ croak "$param{'msg'}: $!" if ($param{'die'});
Branch: 1 branch
BranchTrueFalseTotal
if $param{"die"} 0 0 0
63 0 $$is_failed = 1;
64 }
65  
66 ############################################################################
67 # Returns all files in given directory recursively.
68 sub get_files
69 0 ▶{
Subroutine get_files: not called CC=1 SCAR=6.9
Pod undocumented
70 0 my ($in_dir, $filter) = @_;
71 0 $in_dir = abs_path($in_dir);
72  
73 0 my @files;
74 find(
75 {
76 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
77 0 ▶ return unless (-f $File::Find::name);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $File::Find::name 0 0 0
78 0 ▶ return if (defined($filter) && $File::Find::name !~ /$filter/);
Branch: 1 branch
BranchTrueFalseTotal
if defined $filter and not $File::Find::name =~ /$filter/ 0 0 0
Condition0%0/3
defined $filter and not $File::Find::name =~ /$filter/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $filter and not $File::Find::name =~ /$filter/
defined $filter not $File::Find::name =~ /$filter/
Truth table0%0/3
A and not(B)
A= defined $filterB= not $File::Find::name =~ /$filter/
ABresult
0X0
100
111
79 0 push @files, $File::Find::name;
80 },
81 },
82 0 $in_dir
83 );
84 0 return @files;
85 }
86  
87 ############################################################################
88 # Returns absolute path for given relative path; or undef if
89 # path does not exist
90 sub absolutize_path
91 0 ▶{
Subroutine absolutize_path: not called CC=2 SCAR=17.9
Pod undocumented
92 0 my $path = shift;
93 0 eval {$path = abs_path($path)};
94 0 ▶ return -e $path ? $path : undef;
Branch: 1 branch
BranchTrueFalseTotal
-e $path ? : 0 0 0
95 }
96  
97 ############################################################################
98 3 return 1;
« blib/lib/Alvis/AinoDump.pm ↑ Summary blib/lib/Alvis/Document/Meta.pm »