File Coverage

File:C4/Utils.pm
Coverage:42.6%

linestmtbrancondsubtimecode
1package C4::Utils;
2
3# Useful code I didn't feel like duplicating all over the place.
4#
5
6
3
3
3
26877
37
112
use strict;
7
3
3
3
42
51
139
use warnings;
8
3
3
3
36
49
584
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug);
9
10BEGIN {
11
3
37
        require Exporter;
12
3
37
        $VERSION = 1.00; # set the version for version checking
13
3
50
        $debug = $ENV{DEBUG} || 0;
14
3
147
        @ISA = qw(Exporter);
15
3
29
        @EXPORT_OK = qw(&maxwidth &hashdump);
16
3
1148
        %EXPORT_TAGS = ( all => [qw(&maxwidth &hashdump)], );
17}
18
19
20sub maxwidth (@) {
21
0
        (@_) or return 0;
22
0
0
0
        return (sort {$a <=> $b} map {length} @_)[-1];
23}
24
25sub hashdump ($$) {
26
0
        my $pre = shift;
27
0
        my $val = shift;
28
0
        if (ref($val) =~ /HASH/) {
29
0
                print "$pre = HASH w/ " . scalar(keys %$val) . " keys.\n";
30
0
                my $w2 = maxwidth(keys %$val);
31
0
                foreach (sort keys %$val) {
32
0
                        &hashdump($pre . '->{' . sprintf('%' . $w2 .'s', $_) . '}', $val->{$_});
33                }
34
0
                print "\n";
35        } elsif (ref($val) =~ /ARRAY/) {
36
0
                print "$pre = ARRAY w/ " . scalar(@$val) . " members.\n";
37
0
                my $w2 = maxwidth(@$val);
38
0
                foreach (@$val) {
39
0
                        &hashdump($pre . '->{' . sprintf('%' . $w2 .'s', $_) . '}', $_);
40                }
41
0
                print "\n";
42        } else {
43
0
                print "$pre = $val\n";
44        }
45}
46
471;