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   25491 use strict;
  71         86  
  71         1665  
10 71     71   234 use warnings;
  71         79  
  71         1653  
11 71     71   1679 use utf8;
  71         105  
  71         263  
12              
13             package Devel::PerlySense::Util;
14             $Devel::PerlySense::Util::VERSION = '0.0218';
15 71     71   2647 use base "Exporter";
  71         77  
  71         735  
16              
17             our @EXPORT = (
18             qw/
19             slurp
20             spew
21             textRenderTemplate
22             filePathNormalize
23             /);
24              
25              
26              
27              
28              
29 71     71   7393 use Carp;
  71         135  
  71         3305  
30 71     71   1237 use Data::Dumper;
  71         8821  
  71         2321  
31 71     71   247 use File::Basename;
  71         74  
  71         3718  
32 71     71   975 use Path::Class 0.11;
  71         56620  
  71         3104  
33 71     71   15316 use File::Spec::Functions qw/ splitdir /;
  71         19694  
  71         26194  
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 1059865 my ($raParam, @aArg) = @_;
51 528042         1329609 my %hArg = @aArg;
52              
53 528042         321489 my @aResult;
54 528042         451110 for my $param (@$raParam) {
55 4180690 100       4570594 exists $hArg{$param} or do {
56 5         11 local $Carp::CarpLevel = 1;
57 5         112 croak("Missing argument ($param). Arguments: (" . join(", ", sort keys %hArg) . ")");
58             };
59 4180685         3349295 push(@aResult, $hArg{$param});
60             }
61              
62 528037         1725220 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 1761 my ($file) = @_;
77 20 100       556 open(my $fh, "<", $file) or return undef;
78 18         975 local $/;
79 18         810 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 8 my ($file, $text) = @_;
95 5 50       48 open(my $fh, ">", $file) or return 0;
96 5 50       532 print $fh $text or return 0;
97 5         242 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 3175 my ($filePath) = @_;
114              
115 32         30 my @aDirNew;
116 32         84 for my $dir (splitdir($filePath)) {
117 343 100       1130 if($dir eq "..") {
118             ###TODO: @aDirNew or die("Malformed file ($filePath). Too many parent dirs ('sample_dir/../..')\n");
119 11         15 pop(@aDirNew);
120             }
121             else {
122 332         392 push(@aDirNew, $dir);
123             }
124             }
125            
126 32         127 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         18 my $rex = join("|", map { quotemeta } sort keys %$rhParam);
  10         19  
147 5         76 my $rhParamEnv = { %ENV, %$rhParam };
148              
149 5 50       258 $template =~ s/\${($rex)}/ $rhParamEnv->{$1} || "" /eg; ###TODO: should be //
  8         36  
150              
151 5         25 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