4 use vars qw($VERSION @ISA @EXPORT);
9 @ISA = qw(Exporter DynaLoader);
10 # Items to export into callers namespace by default. Note: do not export
11 # names by default without a very good reason. Use EXPORT_OK instead.
12 # Do not simply export all your public functions/methods/constants.
14 AnyIni UserIni SystemIni
18 bootstrap OS2::PrfDB $VERSION;
20 # Preloaded methods go here.
23 new_from_int OS2::PrfDB::Hini OS2::Prf::System(0),
24 'Anyone of two "systemish" databases', 1;
28 new_from_int OS2::PrfDB::Hini OS2::Prf::System(1), 'User settings database', 1;
32 new_from_int OS2::PrfDB::Hini OS2::Prf::System(2),'System settings database',1;
35 use vars qw{$debug @ISA};
37 push @ISA, qw{Tie::Hash};
39 # Internal structure 0 => HINI, 1 => array of entries, 2 => iterator.
42 die "Usage: tie %arr, OS2::PrfDB, filename\n" unless @_ == 2;
43 my ($obj, $file) = @_;
44 my $hini = ref $file eq 'OS2::PrfDB::Hini' ? $file
45 : new OS2::PrfDB::Hini $file;
46 die "Error opening profile database `$file': $!" unless $hini;
47 # print "tiehash `@_', hini $hini\n" if $debug;
48 bless [$hini, undef, undef];
52 my ($self, $key, $val) = @_;
54 die unless ref $val eq 'HASH';
56 tie %sub, 'OS2::PrfDB::Sub', $self->[0], $key;
61 my ($self, $key) = @_;
64 tie %sub, 'OS2::PrfDB::Sub', $self->[0], $key;
69 my ($self, $key) = @_;
72 tie %sub, 'OS2::PrfDB::Sub', $self->[0], $key;
76 # CLEAR ???? - deletion of the whole
79 my ($self, $key) = @_;
81 return OS2::Prf::GetLength($self->[0]->[0], $key, undef) >= 0;
86 my $keys = OS2::Prf::Get($self->[0]->[0], undef, undef);
87 return undef unless defined $keys;
89 $self->[1] = [split /\0/, $keys];
90 # print "firstkey1 $self, `$self->[3]->[0], $self->[3]->[1]'\n" if $debug;
92 return $self->[1]->[0];
93 # OS2::Prf::Get($self->[0]->[0], $self->[2], $self->[3]->[0]));
97 # print "nextkey `@_'\n" if $debug;
99 return undef unless $self->[2]++ < $#{$self->[1]};
100 my $key = $self->[1]->[$self->[2]];
101 return $key; #, OS2::Prf::Get($self->[0]->[0], $self->[2], $key));
104 package OS2::PrfDB::Hini;
107 die "Usage: new OS2::PrfDB::Hini filename\n" unless @_ == 2;
110 my $hini = OS2::Prf::Open($file);
111 die "Error opening profile database `$file': $!" unless $hini;
112 bless [$hini, $file];
115 # Takes HINI and file name:
117 sub new_from_int { shift; bless [@_] }
119 # Internal structure 0 => HINI, 1 => filename, 2 => do-not-close.
123 my $hini = $self->[0];
124 unless ($self->[2]) {
125 OS2::Prf::Close($hini) or die "Error closing profile `$self->[1]': $!";
129 package OS2::PrfDB::Sub;
130 use vars qw{$debug @ISA};
132 @ISA = qw{Tie::Hash};
134 # Internal structure 0 => HINI, 1 => array of entries, 2 => iterator,
138 die "Usage: tie %arr, OS2::PrfDB::Sub, filename, appname\n" unless @_ == 3;
139 my ($obj, $file, $app) = @_;
140 my $hini = ref $file eq 'OS2::PrfDB::Hini' ? $file
141 : new OS2::PrfDB::Hini $file;
142 die "Error opening profile database `$file': $!" unless $hini;
143 # print "tiehash `@_', hini $hini\n" if $debug;
144 bless [$hini, undef, undef, $app];
148 my ($self, $key, $val) = @_;
150 OS2::Prf::Set($self->[0]->[0], $self->[3], $key, $val);
154 my ($self, $key) = @_;
156 OS2::Prf::Get($self->[0]->[0], $self->[3], $key);
160 my ($self, $key) = @_;
162 OS2::Prf::Set($self->[0]->[0], $self->[3], $key, undef);
165 # CLEAR ???? - deletion of the whole
168 my ($self, $key) = @_;
170 return OS2::Prf::GetLength($self->[0]->[0], $self->[3], $key) >= 0;
175 my $keys = OS2::Prf::Get($self->[0]->[0], $self->[3], undef);
176 return undef unless defined $keys;
178 $self->[1] = [split /\0/, $keys];
179 # print "firstkey1 $self, `$self->[3]->[0], $self->[3]->[1]'\n" if $debug;
181 return $self->[1]->[0];
182 # OS2::Prf::Get($self->[0]->[0], $self->[2], $self->[3]->[0]));
186 # print "nextkey `@_'\n" if $debug;
188 return undef unless $self->[2]++ < $#{$self->[1]};
189 my $key = $self->[1]->[$self->[2]];
190 return $key; #, OS2::Prf::Get($self->[0]->[0], $self->[2], $key));
193 # Autoload methods go after =cut, and are processed by the autosplit program.
197 # Below is the stub of documentation for your module. You better edit it!
201 OS2::PrfDB - Perl extension for access to OS/2 setting database.
206 tie %settings, OS2::PrfDB, 'my.ini';
207 tie %subsettings, OS2::PrfDB::Sub, 'my.ini', 'mykey';
209 print "$settings{firstkey}{subkey}\n";
210 print "$subsettings{subkey}\n";
212 tie %system, OS2::PrfDB, SystemIni;
213 $system{myapp}{mykey} = "myvalue";
218 The extention provides both high-level and low-level access to .ini
221 =head2 High level access
223 High-level access is the tie-hash access via two packages:
224 C<OS2::PrfDB> and C<OS2::PrfDB::Sub>. First one supports one argument,
225 the name of the file to open, the second one the name of the file to
226 open and so called I<Application name>, or the primary key of the
229 tie %settings, OS2::PrfDB, 'my.ini';
230 tie %subsettings, OS2::PrfDB::Sub, 'my.ini', 'mykey';
232 One may substitute a handle for already opened ini-file instead of the
233 file name (obtained via low-level access functions). In particular, 3
234 functions SystemIni(), UserIni(), and AnyIni() provide handles to the
235 "systemish" databases. AniIni will read from both, and write into User
238 =head2 Low-level access
240 Low-level access functions reside in the package C<OS2::Prf>. They are
246 Opens the database, returns an I<integer handle>.
250 Closes the database given an I<integer handle>.
252 =item C<Get(hndl, appname, key)>
254 Retrieves data from the database given 2-part-key C<appname> C<key>.
255 If C<key> is C<undef>, return the "\0" delimited list of C<key>s,
256 terminated by \0. If C<appname> is C<undef>, returns the list of
257 possible C<appname>s in the same form.
259 =item C<GetLength(hndl, appname, key)>
261 Same as above, but returns the length of the value.
263 =item C<Set(hndl, appname, key, value [ , length ])>
265 Sets the value. If the C<value> is not defined, removes the C<key>. If
266 the C<key> is not defined, removes the C<appname>.
270 Return an I<integer handle> associated with the system database. If
271 C<val> is 1, it is I<User> database, if 2, I<System> database, if
272 0, handle for "both" of them: the handle works for read from any one,
273 and for write into I<User> one.
277 returns a reference to a list of two strings, giving names of the
278 I<User> and I<System> databases.
280 =item C<SetUser(file)>
282 B<(Not tested.)> Sets the profile name of the I<User> database. The
283 application should have a message queue to use this function!
287 =head2 Integer handles
289 To convert a name or an integer handle into an object acceptable as
290 argument to tie() interface, one may use the following functions from
291 the package C<OS2::Prf::Hini>:
295 =item C<new(package, file)>
297 =item C<new_from_int(package, int_hndl [ , filename ])>
303 SystemIni(), UserIni(), and AnyIni().
307 Ilya Zakharevich, ilya@math.ohio-state.edu