File Coverage

blib/lib/Perlbal/Fields.pm
Criterion Covered Total %
statement 32 32 100.0
branch 2 4 50.0
condition n/a
subroutine 8 8 100.0
pod 0 2 0.0
total 42 46 91.3


line stmt bran cond sub pod time code
1             package Perlbal::Fields;
2 23     23   1857 use strict;
  23         51  
  23         799  
3 23     23   140 use warnings;
  23         51  
  23         601  
4 23     23   127 use fields;
  23         171  
  23         149  
5              
6             # allow package to be called in command line
7             __PACKAGE__->run(@ARGV) unless caller();
8              
9             # should be the main method called, extra sub could be triggered from this point
10             sub run {
11 1     1 0 272 my ( $package, @options ) = @_;
12              
13             # unactivate fields if launch in command line
14 1         3 $package->remove();
15              
16 1         5 1;
17             }
18              
19             # hash with keys and undef val for each class
20             my $cache_for_class = {};
21              
22             # replace fields::new method which uses Hash::Util::lock_ref_keys
23             # - it's a good idea to keep using the original fields::new during development stage
24             # - but during production we can avoid locking hash and wasting time doing this ( ~ 30 % )
25             sub remove {
26 1     1 0 1 my ($class) = @_;
27              
28 23     23   2637 no warnings "redefine";
  23         55  
  23         1126  
29 23     23   119 no strict 'refs';
  23         50  
  23         5305  
30             *fields::new = sub {
31 2     2   2 my $class = shift;
32 2 50       7 $class = ref $class if ref $class;
33              
34 2 50       6 if ( !defined( $cache_for_class->{$class} ) ) {
35 2         3 my $h = {};
36 2         3 my @keys = keys %{ $class . "::FIELDS" };
  2         9  
37 2         4 map { $h->{$_} = undef; } @keys;
  4         9  
38 2         5 $cache_for_class->{$class} = $h;
39             }
40 2         3 my %h = %{ $cache_for_class->{$class} };
  2         6  
41              
42 2         9 return bless \%h, $class;
43 1         11 };
44             }
45              
46             1;
47