sample.pl untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 2 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 59 CC 2 SCAR 15.4
CC
2
Cov
0%
method
6.0
BEGIN
2.0
CRAP 4.7
1 use Hash::Filler;
2
3 my $hf = new Hash::Filler; # The hash filler object
4
5 # The following line will produce
6 # abundant information about how
7 # each ->fill method is fulfilled.
8
9 $Hash::Filler::DEBUG = 1;
10
11 # This is an example of a user-supplied
12 # function to see if a given hash key
13 # exists already.
14
15 sub method {
16 my $rhash = shift;
17 my $key = shift;
18 print "Checking key {$key}... ";
19 if ($rhash->{$key}) {
20 print "ok\n";
21 return 1;
22 }
23 else {
24 print "fail\n";
25 return 0;
26 }
27 }
28
29 ##
30 ## These are the rules that control how to populate the hash automatically.
31 ## Some of them are explained. Note that the return values for ->add
32 ## are discarded as we do not need them. In practice, you only need them
33 ## if you plan to manage the rules or do detailed profiling with them.
34 ##
35
36 # This is a simple rule. It applies to
37 # the 'key0' hash key. It does not
38 # require any prerequisite keys to be
39 # defined() in the hash.
40 $hf->add(
41 'key0',
42 sub { $_[0]->{$_[1]} = 'i:key0'; sleep 1;},
43 []);
44
45 $hf->add(
46 'key2',
47 sub { $_[0]->{$_[1]} = 'i:key2'; },
48 []);
49
50 # This is a rule to generate hash key 'key1'.
51 # It needs the hash key 'key0' to be present
52 # in the hash for it to be applied.
53 $hf->add(
54 'key1',
55 sub { $_[0]->{$_[1]} = 'k1(' . $_[0]->{'key0'} . ')'; sleep 1;},
56 ['key0']);
57
58 # This rule uses a higher precedence (1000)
59 # to indicate that it must be used instead
60 # of other rules with default precedences.
61 # The precedence is more important than the
62 # prerequisites of a rule when determining
63 # which rule to use.
64 $hf->add(
65 'key2',
66 sub { $_[0]->{$_[1]} = 'k2(' . $_[0]->{'key1'} . ')'; },
67 ['key1'], 1000);
68
69 $hf->add(
70 'key3',
71 sub { $_[0]->{$_[1]} = 'k3(' . $_[0]->{'key4'} . ')'; },
72 ['key4'], 1000);
73
74 $hf->add(
75 'key4',
76 sub { $_[0]->{$_[1]} = 'k4(' . $_[0]->{'key3'} . ')'; sleep 1;},
77 ['key3'], 1000);
78
79 $hf->add(
80 'key4',
81 sub { $_[0]->{$_[1]} = 'i:key4'; },
82 []);
83
84 $hf->add(
85 'key5',
86 sub { 1; },
87 []);
88
89 $hf->add(
90 'key6',
91 sub { $_[0]->{$_[1]} = '<does' .
92 ((exists $_[0]->{'key5'}) ? '' : ' not') . ' exist>'; 1; },
93 ['key5']);
94
95 # This is a wildcard rule. It will be
96 # used to generate any missing key in
97 # the hash. Keep in mind that this rule
98 # can also be used to generate
99 # missing prerequisites. It could also have
100 # prerequisites.
101 $hf->add(
102 undef,
103 sub { $_[0]->{$_[1]} = 'wildcard(' . $_[1] . ')'; 1; },
104 []);
105
106 # This allows the specification of the
107 # method to use to determine if a given
108 # hash key must be filled.
109 $hf->method(\&method);
110 # $hf->method(Hash::Filler::EXISTS);
111
112 # $hf->dump_r_tree;
113
114 my %hash;
115
116 # The following is a code example that uses
117 # fill to populate the hash.
118
119 foreach my $key (qw(key7 key2 key3 key6 key6))
120 {
121 print "*** Filling of key $key:\n";
122 if ($hf->fill(\%hash, $key)) {
123 print "*** Succeeded\n";
124 }
125 else {
126 print "*** Failed\n";
127 }
128 print "*** Value of $key is ", $hash{$key}, "\n";
129 }
130 # Dump the whole rule tree
131 $hf->dump_r_tree;