File Coverage

blib/lib/Devel/PerlySense/Util.pm
Criterion Covered Total %
statement 58 58 100.0
branch 9 12 75.0
condition n/a
subroutine 14 14 100.0
pod 5 5 100.0
total 86 89 96.6


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Devel::PerlySense::Util - Utility routines
4              
5             =cut
6              
7              
8              
9 71     71   25588 use strict;
  71         86  
  71         1655  
10 71     71   220 use warnings;
  71         88  
  71         1563  
11 71     71   1552 use utf8;
  71         109  
  71         261  
12              
13             package Devel::PerlySense::Util;
14             $Devel::PerlySense::Util::VERSION = '0.0217';
15 71     71   2682 use base "Exporter";
  71         93  
  71         732  
16              
17             our @EXPORT = (
18             qw/
19             slurp
20             spew
21             textRenderTemplate
22             filePathNormalize
23             /);
24              
25              
26              
27              
28              
29 71     71   7377 use Carp;
  71         87  
  71         3520  
30 71     71   1252 use Data::Dumper;
  71         9495  
  71         2352  
31 71     71   257 use File::Basename;
  71         78  
  71         3809  
32 71     71   930 use Path::Class 0.11;
  71         55352  
  71         3044  
33 71     71   15289 use File::Spec::Functions qw/ splitdir /;
  71         20179  
  71         26242  
34              
35              
36              
37              
38              
39             =head1 ROUTINES
40              
41             =head2 aNamedArg($raParam, @aArg)
42              
43             Return list of argument valies in $rhArg for the param names in
44             $raParam.
45              
46             Die on missing arguments.
47              
48             =cut
49             sub aNamedArg {
50 528042     528042 1 1072259 my ($raParam, @aArg) = @_;
51 528042         1300682 my %hArg = @aArg;
52              
53 528042         321367 my @aResult;
54 528042         461337 for my $param (@$raParam) {
55 4180690 100       4512955 exists $hArg{$param} or do {
56 5         7 local $Carp::CarpLevel = 1;
57 5         105 croak("Missing argument ($param). Arguments: (" . join(", ", sort keys %hArg) . ")");
58             };
59 4180685         3332319 push(@aResult, $hArg{$param});
60             }
61              
62 528037         1701798 return(@aResult);
63             }
64              
65              
66              
67              
68              
69             =head2 slurp($file)
70              
71             Read the contents of $file and return it, or undef if the file
72             couldn't be opened.
73              
74             =cut
75             sub slurp {
76 20     20 1 1844 my ($file) = @_;
77 20 100       476 open(my $fh, "<", $file) or return undef;
78 18         905 local $/;
79 18         628 return <$fh>;
80             }
81              
82              
83              
84              
85              
86             =head2 spew($file, $text)
87              
88             Crete a new $file a and print $text to it.
89              
90             Return 1 on success, else 0.
91              
92             =cut
93             sub spew {
94 5     5 1 9 my ($file, $text) = @_;
95 5 50       36 open(my $fh, ">", $file) or return 0;
96 5 50       480 print $fh $text or return 0;
97 5         162 return 1;
98             }
99              
100              
101              
102              
103              
104             =head2 filePathNormalize($file)
105              
106             Return the normalized path of $file, i.e. with "dir/dir2/../dir3"
107             becoming "dir/dir3".
108              
109             The path doesn't have to exist.
110              
111             =cut
112             sub filePathNormalize {
113 32     32 1 3025 my ($filePath) = @_;
114              
115 32         33 my @aDirNew;
116 32         60 for my $dir (splitdir($filePath)) {
117 343 100       796 if($dir eq "..") {
118             ###TODO: @aDirNew or die("Malformed file ($filePath). Too many parent dirs ('sample_dir/../..')\n");
119 11         12 pop(@aDirNew);
120             }
121             else {
122 332         259 push(@aDirNew, $dir);
123             }
124             }
125            
126 32         90 return file(@aDirNew) . "";
127             }
128              
129              
130              
131              
132              
133             =head2 textRenderTemplate($template, $rhParam)
134              
135             Replace the keys in $rhParam with the values in $rhParam, for
136             everything in $template that looks like
137              
138             ${KEY_NAME}
139              
140             Return the rendered template.
141              
142             =cut
143             sub textRenderTemplate {
144 5     5 1 69 my ($template, $rhParam) = @_;
145              
146 5         21 my $rex = join("|", map { quotemeta } sort keys %$rhParam);
  10         20  
147 5         86 my $rhParamEnv = { %ENV, %$rhParam };
148              
149 5 50       276 $template =~ s/\${($rex)}/ $rhParamEnv->{$1} || "" /eg; ###TODO: should be //
  8         37  
150              
151 5         23 return $template;
152             }
153              
154              
155              
156              
157              
158             1;
159              
160              
161              
162              
163              
164             __END__
165              
166             =encoding utf8
167              
168             =head1 AUTHOR
169              
170             Johan Lindstrom, C<< <johanl@cpan.org> >>
171              
172             =head1 BUGS
173              
174             Please report any bugs or feature requests to
175             C<bug-devel-perlysense@rt.cpan.org>, or through the web interface at
176             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Devel-PerlySense>.
177             I will be notified, and then you'll automatically be notified of progress on
178             your bug as I make changes.
179              
180             =head1 ACKNOWLEDGEMENTS
181              
182             =head1 COPYRIGHT & LICENSE
183              
184             Copyright 2005 Johan Lindstrom, All Rights Reserved.
185              
186             This program is free software; you can redistribute it and/or modify it
187             under the same terms as Perl itself.
188              
189             =cut