| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | $param{'msg'} = "Cannot open file '$filename'" | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | if (!defined($param{'msg'})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | my $is_failed = 0; | |||||||||||||||||||||||||||
| 42 | 0 | local *FH; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if ($filename =~ /\.bz2$/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | open(FH, "bzcat $filename |") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | or _open_file_failed(\$is_failed, %param); | ||||||||||||||||||||||||||||
| 47 | } elsif ($filename =~ /\.gz$/) { | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | open(FH, "zcat $filename |") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | return ($is_failed ? undef: *FH); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | return if (defined($filter) && $File::Find::name !~ /$filter/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $filter and not $File::Find::name =~ /$filter/
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/
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | ############################################################################ | ||||||||||||||||||||||||||||
| 98 | 3 | return 1; | |||||||||||||||||||||||||||