File Coverage

blib/lib/ODS/Utils.pm
Criterion Covered Total %
statement 96 97 98.9
branch 11 16 68.7
condition n/a
subroutine 24 24 100.0
pod 0 11 0.0
total 131 148 88.5


line stmt bran cond sub pod time code
1             package ODS::Utils;
2              
3 72     72   502 use YAOO;
  72         147  
  72         493  
4 72     72   32949 no strict 'refs';
  72         418  
  72         3466  
5 72     72   36974 use Data::GUID;
  72         1834793  
  72         577  
6 72     72   61544 use File::Copy qw/move/;
  72         298656  
  72         7186  
7 72     72   644 use Carp qw/croak/;
  72         140  
  72         3645  
8 72     72   1489 use Scalar::Util qw//;
  72         203  
  72         1761  
9 72     72   50024 use Data::Dumper qw/Dumper/;
  72         733841  
  72         8648  
10 72     72   57190 use Email::Valid;
  72         10232813  
  72         7113  
11 72     72   24693 use Phone::Valid::International::Loose qw/valid_phone/;
  72         46347  
  72         1072  
12              
13             our ( %EX, %EX2);
14              
15             BEGIN {
16 72     72   18538 %EX = (
17             load => [qw/all/],
18             clone => [qw/all/],
19             unique_class_name => [qw/all/],
20             build_temp_class => [qw/all/],
21             move => [qw/all/],
22             deep_unblessed => [qw/all/],
23             valid_email => [qw/all/],
24             valid_phone => [qw/all/],
25             reftype => [qw/all/],
26             read_file => [qw/all file_dir/],
27             write_file => [qw/all file_dir/],
28             read_directory => [qw/all file_dir/],
29             write_directory => [qw/all file_dir/],
30             croak => [qw/error/],
31             Dumper => [qw/error/]
32             );
33 72         372 for my $ex (keys %EX) {
34 1066         840 for (@{ $EX{$ex} }) {
  1066         1133  
35 1350         971 push @{ $EX2{$_} }, $ex;
  1350         60962  
36             }
37             }
38             }
39              
40             sub import {
41 561     561   1997 my ($self, @functions) = @_;
42              
43 561         1191 my $caller = caller();
44              
45 561         2358 for my $fun (@functions) {
46 1061 100       8008 if ($EX{$fun}) {
    50          
47 703         804 YAOO::make_keyword($caller, $fun, *{"${self}::${fun}"});
  703         3077  
48             } elsif ($EX2{$fun}) {
49 359         399 for (@{ $EX2{$fun} }) {
  359         830  
50 719         5179 YAOO::make_keyword($caller, $_, *{"${self}::${_}"});
  719         2265  
51             }
52             }
53             }
54              
55             }
56              
57             sub valid_email {
58 3     3 0 18 Email::Valid->address(shift);
59             }
60              
61             sub clone {
62 39383     39383 0 58953 my ($c) = @_;
63 39383         65002 my $n = bless YAOO::deep_clone_ordered_hash($c), ref $c;
64 39383         4786570 return $n;
65             }
66              
67             sub load {
68 1911     1911 0 4248 my ($module) = shift;
69 1911         8179 (my $require = $module) =~ s/\:\:/\//g;
70 1911         314358 require $require . '.pm';
71 1720         38839 return $module;
72             }
73              
74             sub unique_class_name {
75 1872     1872 0 18025 return 'A' . join("", split("-", Data::GUID->new->as_string()));
76             }
77              
78             sub build_temp_class {
79 160     160 0 1009 my ($class) = @_;
80 160         543 load $class;
81 160         403 my $temp = $class . '::' . unique_class_name();
82 160         45105 my $c = sprintf(
83             q|
84             package %s;
85             use YAOO;
86             extends '%s';
87             1;
88             |, $temp, $class );
89 160     71   22897 eval $c;
  70     67   1253  
  70         168  
  70         240  
  67         658  
  67         197  
  67         766  
90 160         735 return $temp;
91             }
92              
93             sub deep_unblessed {
94 5     5 0 11 my ($obj) = @_;
95 5         14 $obj = YAOO::deep_clone($obj);
96 5         44 return $obj;
97             }
98              
99             sub read_file {
100 1     1 0 2 my ($file) = @_;
101 1 0       3 open my $fh, '<:encoding(UTF-8)', $file or croak "Cannot open the file for reading: $file $!";
102 1         5 my $data = do { local $/; <$fh> };
  1         1  
  1         4  
103 1         4 close $fh;
104 1         2 return $data;
105             }
106              
107             sub write_file {
108 4     4 0 50 my ($file, $data) = @_;
109 3         12 write_directory($file, 1);
110 3 50       355 open my $fh, '>:encoding(UTF-8)', $file or croak "Cannot open the file for writing: $file $!";
111 3         1752 print $fh $data;
112 3         170 close $fh;
113 3         25 return $file;
114             }
115              
116             sub read_directory {
117 1     2 0 6 my ($directory) = @_;
118 1 50       54 opendir(my $dh, $directory) or croak "Cannot open directory: $directory $!";
119 1         29 my @files = sort { $a cmp $b } grep { $_ !~ m/^\./ } readdir($dh);
  0         0  
  3         10  
120 1         9 closedir $dh;
121 1         5 return @files;
122             }
123              
124             sub write_directory {
125 3691     3692 0 7002 my ($directory, $pop) = @_;
126 3691         13450 my @dir = split "\/", $directory;
127 3691 100       8398 pop @dir if ($pop);
128 3691         6023 my $path = '';
129 3691         7660 while (@dir) {
130 18452 100       34253 $path .= "/" if $path;
131 18452         21115 $path .= shift @dir;
132 18452 100       201358 mkdir $path unless -d $path;
133             }
134 3691         9532 return $directory;
135             }
136              
137             sub reftype {
138 8     9 0 51 Scalar::Util::reftype $_[0];
139             }
140              
141             1;