File Coverage

blib/lib/Unix/Conf.pm
Criterion Covered Total %
statement 15 26 57.6
branch n/a
condition n/a
subroutine 5 9 55.5
pod 1 1 100.0
total 21 36 58.3


line stmt bran cond sub pod time code
1             # Provides utility modules for use by file configuration manipulation classes
2             #
3             # Copyright Karthik Krishnamurthy
4              
5             =head1 NAME
6              
7             Unix::Conf - Front end for class methods in various utility modules
8             under the Unix::Conf namespace.
9              
10             =head1 DESCRIPTION
11              
12             Methods in Unix::Conf are intended as a gateway into the various
13             utility modules like Unix::Conf::ConfIO, Unix::Conf::Err. Unix::Conf
14             is the preferred way to access class constructors in the above mentioned
15             modules. Methods starting with a '_' are intended for use from other
16             modules under the Unix::Conf namespace. Those without the '_' prefix
17             are for general users of the Unix::Conf suite of modules.
18              
19             =head1 METHODS
20              
21             =cut
22              
23             package Unix::Conf;
24              
25 1     1   5425 use 5.6.0;
  1         4  
  1         67  
26 1     1   4 use strict;
  1         1  
  1         24  
27 1     1   4 use warnings;
  1         11  
  1         27  
28 1     1   739 use Unix::Conf::Err;
  1         3  
  1         24  
29 1     1   571 use Unix::Conf::ConfIO;
  1         3  
  1         181  
30              
31             $Unix::Conf::VERSION = "0.2";
32              
33             =over 4
34              
35             =item debuglevel ()
36              
37             Arguments
38             DEBUGLEVEL,
39             Set the class debuglevel variable in Unix::Conf::Err. This enables
40             debugging messages to be printed for all class objects. The actual
41             level at which debug messages are printed is the maximum of class
42             debuglevel variable and the object specific debuglevel variable.
43             Refer to Unix::Conf::Err for the behaviour of the three debuglevels.
44              
45             Example
46             Unix::Conf->debuglevel (2);
47              
48             =cut
49              
50             sub debuglevel
51             {
52 0     0 1   shift ();
53 0           return (Unix::Conf::Err->debuglevel (@_));
54             }
55              
56             =item _open_conf ()
57              
58             Arguments
59             NAME => 'PATHNAME',
60             MODE => FILE_OPEN_MODE, # default is O_RDWR | O_CREAT
61             PERMS => FILE_CREATION_PERMS,# default is 0600
62             LOCK_STYLE => 'flock'/'dotlock', # default is 'flock'
63             SECURE_OPEN => 0/1, # default is 0 (disabled)
64             PERSIST=> 0/1, # default is 0 (disabled)
65             Open a configuration file and return a Unix::Conf::ConfIO object.
66             A LOCK_STYLE of 'dotlock' is used to access /etc/passwd,
67             /etc/shadow, /etc/group, /etc/gshadow. Refer to Unix::Conf::ConfIO
68             for the various methods that this object offers. Returns a new
69             ConfIO object in case of success, or an Err object in case of
70             failure. Refer to documentation for
71              
72             Example
73             my $conf = Unix::Conf->_open_conf (
74             NAME => '/etc/passwd',
75             SECURE_OPEN => 1,
76             LOCK_STYLE => 'dotlock',
77             );
78              
79             =cut
80              
81             # For use by other modules only. use goto &funcname to warp to that function
82             # replacing the frame for these functions. The actual functions/methods meddle
83             # with the stack and hence are sensitive to the calling sequence. We could
84             # alter those methods to omit one frame, i.e. that of Unix::Conf->_*. However
85             # this way, even if users call Unix::Conf::Err, or Unix::Conf::ConfIO directly,
86             # it will still work
87             sub _open_conf
88             {
89 0     0     shift ();
90 0           unshift (@_, 'Unix::Conf::ConfIO');
91 0           goto &Unix::Conf::ConfIO::open;
92             }
93              
94             =item _release_all ()
95              
96             Release all objects which have been opened persistently by the
97             calling class.
98              
99             Example
100             my $conf = Unix::Conf->_open_conf (
101             NAME => 'some_conf',
102             PERSISTENT => 1,
103             LOCK => 'flock',
104             );
105             # Now this object will be held in the Unix:Conf::ConfIO
106             # object cache even though $conf passes out of scope.
107             # This is for ancillary files which need to be held open
108             # so that they remain locked. It eases the user from
109             # having to prevent the user of these objects from going
110             # out of scope. Call this from the destructor to release
111             # all such objects.
112              
113             sub DESTROY
114             {
115             # do stuff
116             Unix::Conf->_release_all ();
117             }
118              
119             # Now all persistently held Unix::Conf::ConfIO objects
120             # will be released this triggering their destructors
121             # which will effectively sync the files and release
122             # the locks.
123              
124             =cut
125              
126             sub _release_all
127             {
128 0     0     shift ();
129 0           unshift (@_, 'Unix::Conf::ConfIO');
130 0           goto &Unix::Conf::ConfIO::release_all;
131             }
132              
133             =item _err ()
134              
135             Arguments
136             PREFIX,
137             ERRMSG,
138             Create a new Unix::Conf::Err object. This object remembers the stack
139             at the creation. The returned object can thrown or returned to
140             indicate an error condition as it evaluates to false in a boolean
141             context. Refer to Unix::Conf::Err for the various methods that this
142             object offers. If error message is missing, a stringified version of
143             $! is stored as the error message.
144              
145             Example
146             return (Unix::Conf::->_err ('chdir')) unless (chdir ('/etc'));
147              
148             return (
149             Unix::Conf::->_err (
150             'object_method', 'argument not an object of class BLAH'
151             )
152             ) unless (ref ($obj) eq 'BLAH');
153              
154             =cut
155              
156             sub _err
157             {
158 0     0     shift ();
159 0           unshift (@_, 'Unix::Conf::Err');
160 0           goto &Unix::Conf::Err::new;
161             }
162              
163             1;
164             __END__