Rolling. Much new material.
[ZOOM-Perl-moved-to-github.git] / lib / ZOOM.pod
1 # $Id: ZOOM.pod,v 1.8 2005-11-17 13:32:30 mike Exp $
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 ZOOM - Perl extension implementing the ZOOM API for Information Retrieval
9
10 =head1 SYNOPSIS
11
12  use ZOOM;
13  eval {
14      $conn = new ZOOM::Connection($host, $port)
15      $conn->option(preferredRecordSyntax => "usmarc");
16      $rs = $conn->search_pqf('@attr 1=4 dinosaur');
17      $n = $rs->size();
18      print $rs->record(0)->render();
19  };
20  if ($@) {
21      print "Error ", $@->code(), ": ", $@->message(), "\n";
22  }
23
24 =head1 DESCRIPTION
25
26 This module provides a nice, Perlish implementation of the ZOOM
27 Abstract API described and documented at http://zoom.z3950.org/api/
28
29 the ZOOM module is implemented as a set of thin classes on top of the
30 non-OO functions provided by this distribution's C<Net::Z3950::ZOOM>
31 module, which in 
32 turn is a thin layer on top of the ZOOM-C code supplied as part of
33 Index Data's YAZ Toolkit.  Because ZOOM-C is also the underlying code
34 that implements ZOOM bindings in C++, Visual Basic, Scheme, Ruby, .NET
35 (including C#) and other languages, this Perl module works compatibly
36 with those other implementations.  (Of course, the point of a public
37 API such as ZOOM is that all implementations should be compatible
38 anyway; but knowing that the same code is running is reassuring.)
39
40 The ZOOM module provides two enumerations (C<ZOOM::Error> and
41 C<ZOOM::Event>), a single utility function C<diag_str()> in the C<ZOOM>
42 package itself, and eight classes:
43 C<ZOOM::Exception>,
44 C<ZOOM::Options>,
45 C<ZOOM::Connection>,
46 C<ZOOM::Query>,
47 C<ZOOM::ResultSet>,
48 C<ZOOM::Record>,
49 C<ZOOM::ScanSet>
50 and
51 C<ZOOM::Package>.
52 Of these, the Query class is abstract, and has two concrete
53 subclasses:
54 C<ZOOM::Query::CQL>
55 and
56 C<ZOOM::Query::PQF>.
57 Many useful ZOOM applications can be built using only the Connection,
58 ResultSet, Record and Exception classes, as in the example
59 code-snippet above.
60
61 A typical application will begin by creating an Connection object,
62 then using that to execute searches that yield ResultSet objects, then
63 fetching records from the result-sets to yield Record objects.  If an
64 error occurs, an Exception object is thrown and can be dealt with.
65
66 More sophisticated applications might also browse the server's indexes
67 to create a ScanSet, from which indexed terms may be retrieved; others
68 might send ``Extended Services'' Packages to the server, to achieve
69 non-standard tasks such as database creation and record update.
70 Searching using a query syntax other than PQF can be done using an
71 query object of one of the Query subclasses.  Finally, sets of options
72 may be manipulated independently of the objects they are associated
73 with using an Options object.
74
75 In general, method calls throw an exception if anything goes wrong, so
76 you don't need to test for success after each call.  See the section
77 below on the Exception class for details.
78
79 =head1 UTILITY FUNCTION
80
81 =head2 ZOOM::diag_str()
82
83  $msg = ZOOM::diag_str(ZOOM::Error::INVALID_QUERY);
84
85 Returns a human-readable English-language string corresponding to the
86 error code that is its own parameter.  This works for any error-code
87 returned from
88 C<ZOOM::Exception::code()>,
89 C<ZOOM::Connection::error_x()>
90 or
91 C<ZOOM::Connection::errcode()>,
92 irrespective of whether it is a member of the C<ZOOM::Error>
93 enumeration or drawn from the BIB-1 diagnostic set.
94
95 =head1 CLASSES
96
97 The eight ZOOM classes are described here in ``sensible order'':
98 first, the four commonly used classes, in the he order that they will
99 tend to be used in most programs (Connection, ResultSet, Record,
100 Exception); then the four more esoteric classes in descending order of
101 how often they are needed.
102
103 With the exception of the Options class, which is an extension to the
104 ZOOM model, the introduction to each class includes a link to the
105 relevant section of the ZOOM Abstract API.
106
107 =head2 ZOOM::Connection
108
109  $conn = new ZOOM::Connection("indexdata.dk:210/gils");
110  print("server is '", $conn->option("serverImplementationName"), "'\n");
111  $conn->option(preferredRecordSyntax => "usmarc");
112  $conn->option_binary(iconBlob => "foo\0bar");
113  $rs = $conn->search_pqf('@attr 1=4 mineral');/usr/local/src/mike/records/acc-ounts/cheques-
114  $ss = $conn->scan('@attr 1=1003 a');
115  if ($conn->errcode() != 0) {
116     die("somthing went wrong: " . $conn->errmsg())
117  }
118  $conn->destroy()
119
120 This class represents a connection to an information retrieval server,
121 using an IR protocol such as ANSI/NISO Z39.50, SRW (the
122 Search/Retrieve Webservice), SRU (the Search/Retrieve URL) or
123 OpenSearch.  Not all of these protocols require a low-level connection
124 to be maintained, but the Connection object nevertheless provides a
125 location for the necessary cache of configuration and state
126 information, as well as a uniform API to the connection-oriented
127 facilities (searching, index browsing, etc.), provided by these
128 protocols.
129
130 See the description of the C<Connection> class in the ZOOM Abstract
131 API at
132 http://zoom.z3950.org/api/zoom-current.html#3.2
133
134 =head3 Methods
135
136 =head4 new()
137
138  $conn = new ZOOM::Connection("indexdata.dk", 210);
139  $conn = new ZOOM::Connection("indexdata.dk:210/gils");
140  $conn = new ZOOM::Connection("tcp:indexdata.dk:210/gils");
141  $conn = new ZOOM::Connection("http:indexdata.dk:210/gils");
142
143 Creates a new Connection object, and immediately connects it to the
144 specified server.  If you want to make a new Connection object but
145 delay forging the connection, use the C<create()> and C<connect()>
146 methods instead.
147
148 This constructor can be called with two arguments or a single
149 argument.  In the former case, the arguments are the name and port
150 number of the Z39.50 server to connect to; in the latter case, the
151 single argument is a YAZ service-specifier string of the form
152
153 =over 4
154
155 =item
156
157 [I<scheme>:]I<host>[:I<port>][/I<databaseName>]
158
159 =back
160
161 In which the I<host> and I<port> parts are as in the two-argument
162 form, the I<databaseName> if provided specifies the name of the
163 database to be used in subsequent searches on this connection, and the
164 optional I<scheme> (default C<tcp>) indicates what protocol should be
165 used.  At present, the following schemes are supported:
166
167 =over 4
168
169 =item tcp
170
171 Z39.50 connection.
172
173 =item ssl
174
175 Z39.50 connection encrypted using SSL (Secure Sockets Layer).  Not
176 many servers support this, but Index Data's Zebra is one that does.
177
178 =item unix
179
180 Z39.50 connection on a Unix-domain (local) socket, in which case the
181 I<hostname> portion of the string is instead used as a filename in the
182 local filesystem.
183
184 =item http
185
186 SRW connection using SOAP over HTTP.
187
188 =back
189
190 Support for SRU will follow in the fullness of time.
191
192 If an error occurs, an exception is thrown.  This may indicate a
193 networking problem (e.g. the host is not found or unreachable), or a
194 protocol-level problem (e.g. a Z39.50 server rejected the Init
195 request).
196
197 =head4 create() / connect()
198
199  $options = new ZOOM::Options();
200  $options->option(implementationName => "my client");
201  $conn = create ZOOM::Connection($options)
202  $conn->connect($host, 0);
203
204 The usual Connection constructor, C<new()> brings a new object into
205 existence and forges the connection to the server all in one
206 operation, which is often what you want.  For applications that need
207 more control, however, these two method separate the two steps,
208 allowing additional steps in between such as the setting of options.
209
210 C<create()> creates and returns a new Connection object, which is
211 I<not> connected to any server.  It may be passed an options block, of
212 type C<ZOOM::Options> (see below), into which options may be set
213 before or after the creation of the Connection.  The connection to the
214 server may then be forged by the C<connect()> method, the arguments of
215 which are the same as those of the C<new()> constructor.
216
217 =head4 error_x() / errcode() / errmsg() / addinfo() / diagset()
218
219  ($errcode, $errmsg, $addinfo, $diagset) = $conn->error_x();
220  $errcode = $conn->errcode();
221  $errmsg = $conn->errmsg();
222  $addinfo = $conn->addinfo();
223  $diagset = $conn->diagset();
224
225 These methods may be used to obtain information about the last error
226 to have occurred on a connection - although typically they will not
227 been used, as the same information is available through the
228 C<ZOOM::Exception> that is thrown when the error occurs.  The
229 C<errcode()>,
230 C<errmsg()>,
231 C<addinfo()>
232 and
233 C<diagset()>
234 methods each return one element of the diagnostic, and
235 C<error_x()>
236 returns all four at once.
237
238 See the C<ZOOM::Exception> for the interpretation of these elements.
239
240 =head4 option() / option_binary()
241
242 Objects of the Connection, ResultSet, ScanSet and Package classes
243 carry with them a set of named options which affect their behaviour in
244 certain ways.  See the ZOOM-C options documentation for details:
245
246 =over 4
247
248 =item *
249
250 Connection options are listed at
251 http://indexdata.com/yaz/doc/zoom.tkl#zoom.connections
252
253 =item *
254
255 ResultSet options are listed at
256 http://indexdata.com/yaz/doc/zoom.resultsets.tkl
257 I<### move this obvservation down the appropriate place>
258
259 =item *
260
261 ScanSet options are listed at
262 http://indexdata.com/yaz/doc/zoom.scan.tkl
263 I<### move this obvservation down the appropriate place>
264
265 =item *
266
267 Package options are listed at
268 http://indexdata.com/yaz/doc/zoom.ext.html
269 I<### move this obvservation down the appropriate place>
270
271 =back
272
273 These options are set and fetched using the C<option()> method, which
274 may be called with either one or two arguments.  In the two-argument
275 form, the option named by the first argument is set to the value of
276 the second argument, and its old value is returned.  In the
277 one-argument form, the value of the specified option is returned.
278
279 For historical reasons, option values are not binary-clean, so that a
280 value containing a NUL byte will be returned in truncated form.  The
281 C<option_binary()> method behaves identically to C<option()> except
282 that it is binary-clean, so that values containing NUL bytes are set
283 and returned correctly.
284
285 =head4 search() / search_pqf()
286
287 I<###>
288
289 =head4 scan()
290
291 I<###>
292
293 =head4 package()
294
295 I<###>
296
297 =head4 destroy()
298
299  $conn->destroy()
300
301 Destroys a Connection object, tearing down any low-level connection
302 associated with it and freeing its resources.  It is an error to reuse
303 a Connection that has been C<destroy()>ed.
304
305 =head2 ZOOM::ResultSet
306
307 I<###>
308
309 =head2 ZOOM::Record
310
311 I<###>
312
313 =head2 ZOOM::Exception
314
315 In general, method calls throw an exception (of class
316 C<ZOOM::Exception>) if anything goes wrong, so you don't need to test
317 for success after each call.  Exceptions are caught by enclosing the
318 main code in an C<eval{}> block and checking C<$@> on exit from that
319 block, as in the code-sample above.
320
321 There are a small number of exceptions to this rule: the three
322 record-fetching methods in the C<ZOOM::ResultSet> class,
323 C<record()>,
324 C<record_immediate()>,
325 and
326 C<records()>
327 can all return undefined values for legitimate reasons, under
328 circumstances that do not merit throwing an exception.  For this
329 reason, the return values of these methods should be checked.  See the
330 individual methods' documentation for details.
331
332 =head3 Methods
333
334 I<###>
335
336 =head2 ZOOM::ScanSet
337
338 I<###>
339
340 =head2 ZOOM::Package
341
342 I<###>
343
344 =head2 ZOOM::Query
345
346 I<###>
347
348 =head2 ZOOM::Options
349
350 I<###>
351
352 =head1 ENUMERATIONS
353
354 The ZOOM module provides two enumerations that list possible return
355 values from particular functions.  They are described in the following
356 sections.
357
358 =head2 ZOOM::Error
359
360  if ($@->code() == ZOOM::Error::QUERY_PQF) {
361      return "your query was not accepted";
362  }
363
364 This class provides a set of manifest constants representing some of
365 the possible error codes that can be raised by the ZOOM module.  The
366 methods that return error-codes are
367 C<ZOOM::Exception::code()>,
368 C<ZOOM::Connection::error_x()>
369 and
370 C<ZOOM::Connection::errcode()>.
371
372 The C<ZOOM::Error> class provides the constants
373 C<NONE>,
374 C<CONNECT>,
375 C<MEMORY>,
376 C<ENCODE>,
377 C<DECODE>,
378 C<CONNECTION_LOST>,
379 C<INIT>,
380 C<INTERNAL>,
381 C<TIMEOUT>,
382 C<UNSUPPORTED_PROTOCOL>,
383 C<UNSUPPORTED_QUERY>,
384 C<INVALID_QUERY>,
385 C<CREATE_QUERY>,
386 C<QUERY_CQL>,
387 C<QUERY_PQF>,
388 C<SORTBY>,
389 C<CLONE>,
390 C<PACKAGE>
391 and
392 C<SCANTERM>,
393 each of which specifies a client-side error.  Since errors may also be
394 diagnosed by the server, and returned to the client, error codes may
395 also take values from the BIB-1 diagnostic set of Z39.50, listed at
396 the Z39.50 Maintenance Agency's web-site at
397 http://www.loc.gov/z3950/agency/defns/bib1diag.html
398
399 All error-codes, whether client-side from the C<ZOOM::Error>
400 enumeration or server-side from the BIB-1 diagnostic set, can be
401 translated into human-readable messages by passing them to the
402 C<ZOOM::diag_str()> utility function.
403
404 =head2 ZOOM::Event
405
406  if ($conn->last_event() == ZOOM::Event::CONNECT) {
407      print "Connected!\n";
408  }
409
410 In applications that need it - mostly complex multiplexing
411 applications - The C<ZOOM::Connection::last_event()> method is used to
412 return an indication of the last event that occurred on a particular
413 connection.  It always returns a value drawn from this enumeration,
414 that is, one of C<NONE>, C<CONNECT>, C<SEND_DATA>, C<RECV_DATA>,
415 C<TIMEOUT>, C<UNKNOWN>, C<SEND_APDU>, C<RECV_APDU>, C<RECV_RECORD> or
416 C<RECV_SEARCH>.
417
418 You almost certainly don't need to know about this.  Frankly, I'm not
419 sure how to use it myself.
420
421 =head1 SEE ALSO
422
423 The ZOOM abstract API,
424 http://zoom.z3950.org/api/zoom-current.html
425
426 The C<Net::Z3950::ZOOM> module, included in the same distribution as this one.
427
428 The C<Net::Z3950> module, which this one supersedes.
429
430 The documentation for the ZOOM-C module of the YAZ Toolkit, which this
431 module is built on.  Specifically, its lists of options are useful.
432 http://indexdata.com/yaz/doc/zoom.tkl
433
434 The BIB-1 diagnostic set of Z39.50,
435 http://www.loc.gov/z3950/agency/defns/bib1diag.html
436
437 =head1 AUTHOR
438
439 Mike Taylor, E<lt>mike@indexdata.comE<gt>
440
441 =head1 COPYRIGHT AND LICENCE
442
443 Copyright (C) 2005 by Index Data.
444
445 This library is free software; you can redistribute it and/or modify
446 it under the same terms as Perl itself, either Perl version 5.8.4 or,
447 at your option, any later version of Perl 5 you may have available.
448
449 =cut
450
451 1;