File Coverage

lib/WRT/Util.pm
Criterion Covered Total %
statement 26 27 96.3
branch 3 4 75.0
condition 2 5 40.0
subroutine 8 9 88.8
pod 2 6 33.3
total 41 51 80.3


line stmt bran cond sub pod time code
1             package WRT::Util;
2              
3 4     4   57558 use strict;
  4         14  
  4         96  
4 4     4   16 use warnings;
  4         7  
  4         96  
5              
6 4     4   17 use base qw(Exporter);
  4         8  
  4         1257  
7             our @EXPORT_OK = qw(dir_list get_date);
8              
9             =item dir_list($dir, $sort_order, $pattern)
10              
11             Return a $sort_order sorted list of files matching regex $pattern in a
12             directory.
13              
14             Calls $sort_order, which can be one of:
15              
16             alpha - alphabetical
17             reverse_alpha - alphabetical, reversed
18             high_to_low - numeric, high to low
19             low_to_high - numeric, low to high
20              
21             =cut
22              
23             sub dir_list {
24 46     46 1 93 my ($dir, $sort_order, $pattern) = @_;
25              
26 46   33     77 $pattern ||= qr/^[0-9]{1,2}$/;
27 46   50     68 $sort_order ||= 'high_to_low';
28              
29 46 50       823 opendir my $list_dir, $dir
30             or die "Couldn't open $dir: $!";
31              
32             my @files = sort $sort_order
33 46         495 grep { m/$pattern/ }
  199         1023  
34             readdir $list_dir;
35              
36 46         297 closedir $list_dir;
37              
38 46         185 return @files;
39             }
40              
41             # Various named sorts for dir_list:
42 9     9 0 33 sub alpha { $a cmp $b; } # alphabetical
43 3     3 0 13 sub high_to_low { $b <=> $a; } # numeric, high to low
44 2     2 0 9 sub low_to_high { $a <=> $b; } # numberic, low to high
45 0     0 0 0 sub reverse_alpha { $b cmp $a; } # alphabetical, reversed
46              
47             =item get_date('key', 'other_key', ...)
48              
49             Return current date values for the given key. Valid keys are sec, min, hour,
50             mday (day of month), mon, year, wday (day of week), yday (day of year), and
51             isdst (is daylight savings).
52              
53             Remember that year is given in years after 1900.
54              
55             =cut
56              
57             # Below replaces:
58             # my ($sec, $min, $hour, $mday, $mon,
59             # $year, $wday, $yday, $isdst) = localtime(time);
60             {
61             my %name_map = (
62             sec => 0, min => 1, hour => 2, mday => 3,
63             mon => 4, year => 5, wday => 6, yday => 5,
64             isdst => 6,
65             );
66              
67             sub get_date {
68 10     10 1 560 my (@names) = @_;
69 10         31 my (@indices) = @name_map{@names};
70 10         310 my (@values) = (localtime time)[@indices];
71              
72 10 100       33 if (wantarray()) {
73             # my ($foo, $bar) = get_date('foo', 'bar');
74 6         20 return @values;
75             } else {
76             # this is probably useless unless you're getting just one value
77 4         101 return join '', @values;
78             }
79             }
80             }
81              
82             1;