File Coverage

blib/lib/Config/Setting/FileProvider.pm
Criterion Covered Total %
statement 40 41 97.5
branch 4 6 66.6
condition 1 3 33.3
subroutine 7 7 100.0
pod 2 2 100.0
total 54 59 91.5


line stmt bran cond sub pod time code
1             # Copyright (C) 2004 by Dominic Mitchell. All rights reserved.
2             #
3             # Redistribution and use in source and binary forms, with or without
4             # modification, are permitted provided that the following conditions
5             # are met:
6             # 1. Redistributions of source code must retain the above copyright
7             # notice, this list of conditions and the following disclaimer.
8             # 2. Redistributions in binary form must reproduce the above copyright
9             # notice, this list of conditions and the following disclaimer in the
10             # documentation and/or other materials provided with the distribution.
11             #
12             # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13             # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15             # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
16             # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17             # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18             # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19             # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20             # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21             # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22             # SUCH DAMAGE.
23              
24             =pod
25              
26             =head1 NAME
27              
28             Config::Setting::FileProvider - return the contents of files.
29              
30             =head1 SYNOPSIS
31              
32             use Config::Setting::FileProvider;
33             my $p = Config::Setting::FileProvider->new(Env => "MYRCFILES",
34             Paths => ["/etc/myrc",
35             "~/.myrc"]);
36             my @contents = $p->provide();
37              
38             =head1 DESCRIPTION
39              
40             This class presents an interface to file contents. It returns the
41             contents of various files, in order to the application that requests it.
42              
43             It is not intended that this class be used standalone, rather that it
44             be used as part of the L module.
45              
46             =head1 METHODS
47              
48             =over 4
49              
50             =item new ( ARGS )
51              
52             Create a new object. ARGS is a set of keyword / value pairs.
53             Recognised options are:
54              
55             =over 4
56              
57             =item Env
58              
59             The name of an environment variable to look at. If it exists, it will
60             contain a colon separated list of paths to settings files.
61              
62             =item Paths
63              
64             A list of file paths to be used, in order, for settings files.
65              
66             =back
67              
68             In both Env and Paths, you may use the tilde-notation ("~") to specify
69             home directories.
70              
71             Any files specified in Paths will also have an identical file searched
72             for but with the hostname specified. This should make per-host
73             customization simpler.
74              
75             Any Env settings files will be looked at I any Paths settings
76             files.
77              
78             It is reccomended that you specify both parameters in the constructor.
79              
80             =item provide ( )
81              
82             Return a list of file contents, one per file read.
83              
84             =back
85              
86             =head1 AUTHOR
87              
88             Dominic Mitchell, Ecpan (at) happygiraffe.netE
89              
90             =head1 SEE ALSO
91              
92             L.
93              
94             =cut
95              
96             package Config::Setting::FileProvider;
97              
98 3     3   91123 use strict;
  3         6  
  3         161  
99 3     3   19 use vars qw($rcsid $VERSION $default);
  3         5  
  3         227  
100              
101 3     3   16 use Carp;
  3         6  
  3         261  
102 3     3   3730 use Sys::Hostname;
  3         5575  
  3         2013  
103              
104             $rcsid = '@(#) $Id: FileProvider.pm 765 2005-08-31 20:05:59Z dom $ ';
105             $VERSION = (qw( $Revision: 765 $ ))[1];
106             $default = "~/.settingsrc";
107              
108             sub new {
109 1     1 1 13 my $class = shift;
110 1         5 my (%args) = @_;
111              
112 1         8 my $self = {
113             Env => "SETTINGS_FILES",
114             Paths => [ $default ],
115             %args,
116             Files => [ ], # Must not be overridden!
117             };
118 1         4 bless $self, $class;
119 1         7 return $self->_init();
120             }
121              
122             sub _init() {
123 1     1   2 my $self = shift;
124 1         2 my @files = @{ $self->{ Paths } };
  1         7  
125              
126             # Allow listed files to be overridden by a hostname-specific
127             # one.
128 1         6 my $hn = hostname;
129 1         15 @files = map { $_, "$_.$hn" } @files;
  1         6  
130              
131             # Always allow the environment to override previous choices.
132 1         3 my $envvar = $self->{ Env };
133 1 50 33     15 if ( $envvar && $ENV{ $envvar } ) {
134 0         0 push @files, split /:/, $ENV{ $envvar };
135             }
136 1         2 push @{ $self->{ Files } }, @files;
  1         4  
137 1         6 return $self;
138             }
139              
140             sub provide {
141 1     1 1 2 my $self = shift;
142             # Allow tilde notation for home directory.
143 1         2 my @files = map(glob, @{ $self->{Files} });
  1         75  
144 1         2 my @texts;
145 1         3 foreach my $f (@files) {
146 2 100       69 next unless -f $f;
147 1 50       40 open my $fh, $f
148             or croak "open($f): $!";
149 1         2 push @texts, do { local $/; <$fh> };
  1         4  
  1         33  
150 1         12 close $fh;
151             }
152 1         6 return @texts;
153             }
154              
155             1;
156             __END__