File Coverage

blib/lib/Rose/Conf.pm
Criterion Covered Total %
statement 42 52 80.7
branch 12 20 60.0
condition 2 3 66.6
subroutine 10 12 83.3
pod 3 3 100.0
total 69 90 76.6


line stmt bran cond sub pod time code
1             package Rose::Conf;
2              
3 2     2   48622 use strict;
  2         5  
  2         75  
4              
5 2     2   11 use Carp();
  2         4  
  2         768  
6              
7             our $VERSION = '0.021';
8              
9             our $ExportLevel = 0;
10              
11             sub import
12             {
13 5     5   36 my($pkg, $export_name) = @_;
14              
15 5 100 66     189 return unless(@_ > 1 && length $export_name);
16              
17 2         23 my($callpkg) = caller($ExportLevel);
18              
19 2 50       11 return if($pkg eq __PACKAGE__);
20              
21             #
22             # Mostly lifted from Exporter.pm
23             #
24              
25             # Make import warnings look like they're coming from the "use"
26             local $SIG{'__WARN__'} = sub
27             {
28 0     0   0 my($text) = shift;
29              
30 0 0       0 if($text =~ s/ at \S*Rose\/Conf.pm line \d+.*\n//)
31             {
32 0         0 local $Carp::CarpLevel = 1; # ignore package calling us too.
33 0         0 carp $text;
34             }
35             else
36             {
37 0         0 warn $text;
38             }
39 2         18 };
40              
41             local $SIG{'__DIE__'} = sub
42             {
43 0     0   0 local $Carp::CarpLevel = 1; # ignore package calling us too.
44 0 0       0 Carp::croak "$_[0] Illegal null symbol in \@${1}::EXPORT"
45             if($_[0] =~ /^Unable to create sub named "(.*?)::"/);
46 2         39 };
47              
48 2 50       17 unless($export_name =~ /^%\w+$/)
49             {
50 0         0 local $Carp::CarpLevel = 0;
51 0         0 Carp::croak "Usage: use $pkg qw(%SOME_HASH)";
52             }
53              
54             # Chop off the %
55 2         5 $export_name = substr($export_name, 1);
56              
57             # Alias the %CONF hash
58 2     2   19 no strict 'refs';
  2         14  
  2         607  
59 2         4 *{"${callpkg}::$export_name"} = \%{"${pkg}::CONF"}
  2         31  
  2         9  
60             }
61              
62             sub param
63             {
64 52     52 1 4861 my($class) = shift;
65              
66 52         115 my $conf = $class->conf_hash;
67              
68 52 50       128 if(@_)
69             {
70 52         62 my $param = shift;
71              
72 52 100       98 if(@_)
73             {
74 3         16 return $conf->{$param} = shift;
75             }
76             else
77             {
78 49 100       463 Carp::croak "No such conf parameter in $class: '$param'\n"
79             unless(exists $conf->{$param});
80              
81 45 100       246 if(ref $conf->{$param} eq 'HASH')
82             {
83 5         25 return bless $conf->{$param}, 'Rose::Conf::Setting';
84             }
85              
86 40         173 return $conf->{$param};
87             }
88             }
89              
90 0         0 Carp::croak "Cannot get param() without parameter name";
91             }
92              
93             sub conf_hash
94             {
95 66     66 1 720 my($class) = shift;
96              
97 2     2   12 no strict 'refs';
  2         4  
  2         308  
98              
99 66         69 return \%{$class . '::CONF'};
  66         266  
100             }
101              
102             sub param_exists
103             {
104 18     18 1 2077 my($class, $param) = @_;
105              
106 18 50       44 Carp::croak "Cannot check if param_exists() without parameter name"
107             unless(defined $param);
108              
109 18         38 my $conf = $class->conf_hash;
110              
111 18         84 return exists $conf->{$param};
112             }
113              
114             BEGIN
115             {
116             package Rose::Conf::Setting;
117 2     2   86 our @ISA = qw(Rose::Conf);
118 20     20   32 sub conf_hash { $_[0] }
119             }
120              
121              
122             1;
123              
124             __END__