1 |
argrath |
1.1 |
|
2 |
|
|
=encoding euc-jp |
3 |
|
|
|
4 |
|
|
=head1 NAME |
5 |
|
|
|
6 |
|
|
=begin original |
7 |
|
|
|
8 |
|
|
perldbmfilter - Perl DBM Filters |
9 |
|
|
|
10 |
|
|
=end original |
11 |
|
|
|
12 |
|
|
perldbmfilter - Perl DBM フィルタ |
13 |
|
|
|
14 |
|
|
=head1 SYNOPSIS |
15 |
|
|
|
16 |
|
|
$db = tie %hash, 'DBM', ... |
17 |
|
|
|
18 |
|
|
$old_filter = $db->filter_store_key ( sub { ... } ); |
19 |
|
|
$old_filter = $db->filter_store_value( sub { ... } ); |
20 |
|
|
$old_filter = $db->filter_fetch_key ( sub { ... } ); |
21 |
|
|
$old_filter = $db->filter_fetch_value( sub { ... } ); |
22 |
|
|
|
23 |
|
|
=head1 DESCRIPTION |
24 |
|
|
|
25 |
|
|
=begin original |
26 |
|
|
|
27 |
|
|
The four C<filter_*> methods shown above are available in all the DBM |
28 |
|
|
modules that ship with Perl, namely DB_File, GDBM_File, NDBM_File, |
29 |
|
|
ODBM_File and SDBM_File. |
30 |
|
|
|
31 |
|
|
=end original |
32 |
|
|
|
33 |
|
|
上述の四つの C<filter_*> メソッドは、Perl と共に出荷されている全ての |
34 |
|
|
DBM モジュール、つまり DB_File, GDBM_File, NDBM_File, ODBM_File, |
35 |
|
|
SDBM_File で利用可能です。 |
36 |
|
|
|
37 |
|
|
=begin original |
38 |
|
|
|
39 |
|
|
Each of the methods works identically, and is used to install (or |
40 |
|
|
uninstall) a single DBM Filter. The only difference between them is the |
41 |
|
|
place that the filter is installed. |
42 |
|
|
|
43 |
|
|
=end original |
44 |
|
|
|
45 |
|
|
それぞれのメソッドは全く同じように動作し、一つの DBM フィルタの |
46 |
|
|
インストール(またはアンインストール)のために使われます。 |
47 |
|
|
これらの唯一の違いはフィルタをインストールする場所です。 |
48 |
|
|
|
49 |
|
|
=begin original |
50 |
|
|
|
51 |
|
|
To summarise: |
52 |
|
|
|
53 |
|
|
=end original |
54 |
|
|
|
55 |
|
|
要約すると: |
56 |
|
|
|
57 |
|
|
=over 5 |
58 |
|
|
|
59 |
|
|
=item B<filter_store_key> |
60 |
|
|
|
61 |
|
|
=begin original |
62 |
|
|
|
63 |
|
|
If a filter has been installed with this method, it will be invoked |
64 |
|
|
every time you write a key to a DBM database. |
65 |
|
|
|
66 |
|
|
=end original |
67 |
|
|
|
68 |
|
|
フィルタがこのメソッドにインストールされると、DBM データベースにキーを |
69 |
|
|
書き込む毎に起動されます。 |
70 |
|
|
|
71 |
|
|
=item B<filter_store_value> |
72 |
|
|
|
73 |
|
|
=begin original |
74 |
|
|
|
75 |
|
|
If a filter has been installed with this method, it will be invoked |
76 |
|
|
every time you write a value to a DBM database. |
77 |
|
|
|
78 |
|
|
=end original |
79 |
|
|
|
80 |
|
|
フィルタがこのメソッドにインストールされると、DBM データベースに値を |
81 |
|
|
書き込む毎に起動されます。 |
82 |
|
|
|
83 |
|
|
=item B<filter_fetch_key> |
84 |
|
|
|
85 |
|
|
=begin original |
86 |
|
|
|
87 |
|
|
If a filter has been installed with this method, it will be invoked |
88 |
|
|
every time you read a key from a DBM database. |
89 |
|
|
|
90 |
|
|
=end original |
91 |
|
|
|
92 |
|
|
フィルタがこのメソッドにインストールされると、DBM データベースからキーを |
93 |
|
|
読み込む毎に起動されます。 |
94 |
|
|
|
95 |
|
|
=item B<filter_fetch_value> |
96 |
|
|
|
97 |
|
|
=begin original |
98 |
|
|
|
99 |
|
|
If a filter has been installed with this method, it will be invoked |
100 |
|
|
every time you read a value from a DBM database. |
101 |
|
|
|
102 |
|
|
=end original |
103 |
|
|
|
104 |
|
|
フィルタがこのメソッドにインストールされると、DBM データベースから値を |
105 |
|
|
読み込む毎に起動されます。 |
106 |
|
|
|
107 |
|
|
=back |
108 |
|
|
|
109 |
|
|
=begin original |
110 |
|
|
|
111 |
|
|
You can use any combination of the methods from none to all four. |
112 |
|
|
|
113 |
|
|
=end original |
114 |
|
|
|
115 |
|
|
これらのメソッドは 0 個から 4 個全てまで好きな組み合わせで使えます。 |
116 |
|
|
|
117 |
|
|
=begin original |
118 |
|
|
|
119 |
|
|
All filter methods return the existing filter, if present, or C<undef> |
120 |
|
|
if not. |
121 |
|
|
|
122 |
|
|
=end original |
123 |
|
|
|
124 |
|
|
全てのフィルタメソッドはもしあれば既に登録されているフィルタを、 |
125 |
|
|
なければ C<undef> を返します。 |
126 |
|
|
|
127 |
|
|
=begin original |
128 |
|
|
|
129 |
|
|
To delete a filter pass C<undef> to it. |
130 |
|
|
|
131 |
|
|
=end original |
132 |
|
|
|
133 |
|
|
フィルタを削除するには C<undef> を渡します。 |
134 |
|
|
|
135 |
|
|
=head2 The Filter |
136 |
|
|
|
137 |
|
|
(フィルタ) |
138 |
|
|
|
139 |
|
|
=begin original |
140 |
|
|
|
141 |
|
|
When each filter is called by Perl, a local copy of C<$_> will contain |
142 |
|
|
the key or value to be filtered. Filtering is achieved by modifying |
143 |
|
|
the contents of C<$_>. The return code from the filter is ignored. |
144 |
|
|
|
145 |
|
|
=end original |
146 |
|
|
|
147 |
|
|
それぞれのフィルタが Perl によって呼び出されると、C<$_> のローカルコピーには |
148 |
|
|
フィルタされるキーや値が入ります。 |
149 |
|
|
フィルタリングは C<$_> の内容を変更することによって行われます。 |
150 |
|
|
フィルタからの返り値は無視されます。 |
151 |
|
|
|
152 |
|
|
=head2 An Example: the NULL termination problem. |
153 |
|
|
|
154 |
|
|
(例: NULL 終端問題) |
155 |
|
|
|
156 |
|
|
=begin original |
157 |
|
|
|
158 |
|
|
DBM Filters are useful for a class of problems where you I<always> |
159 |
|
|
want to make the same transformation to all keys, all values or both. |
160 |
|
|
|
161 |
|
|
=end original |
162 |
|
|
|
163 |
|
|
全てのキー、全ての値、あるいはその両方に対して同じ変換を I<常に> |
164 |
|
|
行いたいような問題の種類には DBM フィルタが便利です。 |
165 |
|
|
|
166 |
|
|
=begin original |
167 |
|
|
|
168 |
|
|
For example, consider the following scenario. You have a DBM database |
169 |
|
|
that you need to share with a third-party C application. The C application |
170 |
|
|
assumes that I<all> keys and values are NULL terminated. Unfortunately |
171 |
|
|
when Perl writes to DBM databases it doesn't use NULL termination, so |
172 |
|
|
your Perl application will have to manage NULL termination itself. When |
173 |
|
|
you write to the database you will have to use something like this: |
174 |
|
|
|
175 |
|
|
=end original |
176 |
|
|
|
177 |
|
|
例えば、以下のようなシナリオを考えます。 |
178 |
|
|
サードパーティ C アプリケーションと共有する必要がある DBM データベースが |
179 |
|
|
あります。 |
180 |
|
|
C アプリケーションは I<全ての> キーと値が NULL 終端されていることを |
181 |
|
|
仮定しています。 |
182 |
|
|
残念ながら Perl が DBM データベースに書き込むときには NULL 終端しないので、 |
183 |
|
|
Perl アプリケーションは自分自身で NULL 終端を管理する必要があります。 |
184 |
|
|
データベースに書き込むときに、以下のようなことをする必要があります: |
185 |
|
|
|
186 |
|
|
$hash{"$key\0"} = "$value\0"; |
187 |
|
|
|
188 |
|
|
=begin original |
189 |
|
|
|
190 |
|
|
Similarly the NULL needs to be taken into account when you are considering |
191 |
|
|
the length of existing keys/values. |
192 |
|
|
|
193 |
|
|
=end original |
194 |
|
|
|
195 |
|
|
同様に、すでにあるキーや値の長さを考慮するときには NULL を計算に入れる |
196 |
|
|
必要があります。 |
197 |
|
|
|
198 |
|
|
=begin original |
199 |
|
|
|
200 |
|
|
It would be much better if you could ignore the NULL terminations issue |
201 |
|
|
in the main application code and have a mechanism that automatically |
202 |
|
|
added the terminating NULL to all keys and values whenever you write to |
203 |
|
|
the database and have them removed when you read from the database. As I'm |
204 |
|
|
sure you have already guessed, this is a problem that DBM Filters can |
205 |
|
|
fix very easily. |
206 |
|
|
|
207 |
|
|
=end original |
208 |
|
|
|
209 |
|
|
メインアプリケーションのコードでは NULL 終端問題を無視して、 |
210 |
|
|
データベースに書き込むときには自動的に終端の NULL を追加して、 |
211 |
|
|
データベースから読み込むときには自動的に削除する機構があれば |
212 |
|
|
とても便利です。 |
213 |
|
|
既に想像はしていると思いますが、これは DBM フィルタが簡単に修正できる |
214 |
|
|
問題です。 |
215 |
|
|
|
216 |
|
|
use strict; |
217 |
|
|
use warnings; |
218 |
|
|
use SDBM_File; |
219 |
|
|
use Fcntl; |
220 |
|
|
|
221 |
|
|
my %hash; |
222 |
|
|
my $filename = "filt"; |
223 |
|
|
unlink $filename; |
224 |
|
|
|
225 |
|
|
my $db = tie(%hash, 'SDBM_File', $filename, O_RDWR|O_CREAT, 0640) |
226 |
|
|
or die "Cannot open $filename: $!\n"; |
227 |
|
|
|
228 |
|
|
# Install DBM Filters |
229 |
|
|
$db->filter_fetch_key ( sub { s/\0$// } ); |
230 |
|
|
$db->filter_store_key ( sub { $_ .= "\0" } ); |
231 |
|
|
$db->filter_fetch_value( |
232 |
|
|
sub { no warnings 'uninitialized'; s/\0$// } ); |
233 |
|
|
$db->filter_store_value( sub { $_ .= "\0" } ); |
234 |
|
|
|
235 |
|
|
$hash{"abc"} = "def"; |
236 |
|
|
my $a = $hash{"ABC"}; |
237 |
|
|
# ... |
238 |
|
|
undef $db; |
239 |
|
|
untie %hash; |
240 |
|
|
|
241 |
|
|
=begin original |
242 |
|
|
|
243 |
|
|
The code above uses SDBM_File, but it will work with any of the DBM |
244 |
|
|
modules. |
245 |
|
|
|
246 |
|
|
=end original |
247 |
|
|
|
248 |
|
|
上述のコードでは SDBM_File を使っていますが、いずれの DBM モジュールでも |
249 |
|
|
動作します。 |
250 |
|
|
|
251 |
|
|
=begin original |
252 |
|
|
|
253 |
|
|
Hopefully the contents of each of the filters should be |
254 |
|
|
self-explanatory. Both "fetch" filters remove the terminating NULL, |
255 |
|
|
and both "store" filters add a terminating NULL. |
256 |
|
|
|
257 |
|
|
=end original |
258 |
|
|
|
259 |
|
|
出来れば、それぞれのフィルタの内容は自己説明的であるべきです。 |
260 |
|
|
両方の "fetch" フィルタは終端の NULL を削除し、両方の "store" フィルタは |
261 |
|
|
終端する NULL を追加します。 |
262 |
|
|
|
263 |
|
|
=head2 Another Example: Key is a C int. |
264 |
|
|
|
265 |
|
|
(もう一つの例: キーは C の int) |
266 |
|
|
|
267 |
|
|
=begin original |
268 |
|
|
|
269 |
|
|
Here is another real-life example. By default, whenever Perl writes to |
270 |
|
|
a DBM database it always writes the key and value as strings. So when |
271 |
|
|
you use this: |
272 |
|
|
|
273 |
|
|
=end original |
274 |
|
|
|
275 |
|
|
もう一つの実際の生活の例です。 |
276 |
|
|
デフォルトでは、Perl が DBM データベースに書き込むときはいつでも、 |
277 |
|
|
キーと値を文字列として書き込みます。 |
278 |
|
|
従って、以下のように使うと: |
279 |
|
|
|
280 |
|
|
$hash{12345} = "something"; |
281 |
|
|
|
282 |
|
|
=begin original |
283 |
|
|
|
284 |
|
|
the key 12345 will get stored in the DBM database as the 5 byte string |
285 |
|
|
"12345". If you actually want the key to be stored in the DBM database |
286 |
|
|
as a C int, you will have to use C<pack> when writing, and C<unpack> |
287 |
|
|
when reading. |
288 |
|
|
|
289 |
|
|
=end original |
290 |
|
|
|
291 |
|
|
キー 12345 は、5 バイトの文字列 "12345" として DBM データベースとして |
292 |
|
|
保管されます。 |
293 |
|
|
実際に DBM データベースに C の int としてキーを保管したい場合は、 |
294 |
|
|
書き込むときに C<pack> を使い、読み込むときに C<unpack> を使う |
295 |
|
|
必要があります。 |
296 |
|
|
|
297 |
|
|
=begin original |
298 |
|
|
|
299 |
|
|
Here is a DBM Filter that does it: |
300 |
|
|
|
301 |
|
|
=end original |
302 |
|
|
|
303 |
|
|
以下はこのために DBM を使います: |
304 |
|
|
|
305 |
|
|
use strict; |
306 |
|
|
use warnings; |
307 |
|
|
use DB_File; |
308 |
|
|
my %hash; |
309 |
|
|
my $filename = "filt"; |
310 |
|
|
unlink $filename; |
311 |
|
|
|
312 |
|
|
my $db = tie %hash, 'DB_File', $filename, O_CREAT|O_RDWR, 0666, |
313 |
|
|
$DB_HASH or die "Cannot open $filename: $!\n"; |
314 |
|
|
|
315 |
|
|
$db->filter_fetch_key ( sub { $_ = unpack("i", $_) } ); |
316 |
|
|
$db->filter_store_key ( sub { $_ = pack ("i", $_) } ); |
317 |
|
|
$hash{123} = "def"; |
318 |
|
|
# ... |
319 |
|
|
undef $db; |
320 |
|
|
untie %hash; |
321 |
|
|
|
322 |
|
|
=begin original |
323 |
|
|
|
324 |
|
|
The code above uses DB_File, but again it will work with any of the |
325 |
|
|
DBM modules. |
326 |
|
|
|
327 |
|
|
=end original |
328 |
|
|
|
329 |
|
|
上述のコードでは DB_File を使っていますが、いずれの DBM モジュールでも |
330 |
|
|
動作します。 |
331 |
|
|
|
332 |
|
|
=begin original |
333 |
|
|
|
334 |
|
|
This time only two filters have been used; we only need to manipulate |
335 |
|
|
the contents of the key, so it wasn't necessary to install any value |
336 |
|
|
filters. |
337 |
|
|
|
338 |
|
|
=end original |
339 |
|
|
|
340 |
|
|
今回は二つのフィルタだけを使っています; キーの内容だけを操作する |
341 |
|
|
必要があるので、値フィルタをインストールする必要はありませんでした。 |
342 |
|
|
|
343 |
|
|
=head1 SEE ALSO |
344 |
|
|
|
345 |
|
|
=begin original |
346 |
|
|
|
347 |
|
|
L<DB_File>, L<GDBM_File>, L<NDBM_File>, L<ODBM_File> and L<SDBM_File>. |
348 |
|
|
|
349 |
|
|
=end original |
350 |
|
|
|
351 |
|
|
L<DB_File>, L<GDBM_File>, L<NDBM_File>, L<ODBM_File>, L<SDBM_File> |
352 |
|
|
|
353 |
|
|
=head1 AUTHOR |
354 |
|
|
|
355 |
|
|
Paul Marquess |
356 |
|
|
|
357 |
|
|
=begin meta |
358 |
|
|
|
359 |
|
|
Translate: SHIRAKATA Kentaro <argrath@ub32.org> |
360 |
|
|
Status: completed |
361 |
|
|
|
362 |
|
|
=end meta |
363 |
|
|
|