Updated documentation for the scanning methods in the Connection
[ZOOM-Perl-moved-to-github.git] / lib / ZOOM.pm
1 # $Id: ZOOM.pm,v 1.23 2005-12-19 17:46:09 mike Exp $
2
3 use strict;
4 use warnings;
5 use Net::Z3950::ZOOM;
6
7
8 package ZOOM;
9
10
11 # Member naming convention: hash-element names which begin with an
12 # underscore represent underlying ZOOM-C object descriptors; those
13 # which lack them represent Perl's ZOOM objects.  (The same convention
14 # is used in naming local variables where appropriate.)
15 #
16 # So, for example, the ZOOM::Connection class has an {_conn} element,
17 # which is a pointer to the ZOOM-C Connection object; but the
18 # ZOOM::ResultSet class has a {conn} element, which is a reference to
19 # the Perl-level Connection object by which it was created.  (It may
20 # be that we find we have no need for these references, but for now
21 # they are retained.)
22 #
23 # To get at the underlying ZOOM-C connection object of a result-set
24 # (if you ever needed to do such a thing, which you probably don't)
25 # you'd use $rs->{conn}->_conn().
26
27 # ----------------------------------------------------------------------------
28
29 # The "Error" package contains constants returned as error-codes.
30 package ZOOM::Error;
31 sub NONE { Net::Z3950::ZOOM::ERROR_NONE }
32 sub CONNECT { Net::Z3950::ZOOM::ERROR_CONNECT }
33 sub MEMORY { Net::Z3950::ZOOM::ERROR_MEMORY }
34 sub ENCODE { Net::Z3950::ZOOM::ERROR_ENCODE }
35 sub DECODE { Net::Z3950::ZOOM::ERROR_DECODE }
36 sub CONNECTION_LOST { Net::Z3950::ZOOM::ERROR_CONNECTION_LOST }
37 sub INIT { Net::Z3950::ZOOM::ERROR_INIT }
38 sub INTERNAL { Net::Z3950::ZOOM::ERROR_INTERNAL }
39 sub TIMEOUT { Net::Z3950::ZOOM::ERROR_TIMEOUT }
40 sub UNSUPPORTED_PROTOCOL { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_PROTOCOL }
41 sub UNSUPPORTED_QUERY { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_QUERY }
42 sub INVALID_QUERY { Net::Z3950::ZOOM::ERROR_INVALID_QUERY }
43 # The following are added specifically for this OO interface
44 sub CREATE_QUERY { 20001 }
45 sub QUERY_CQL { 20002 }
46 sub QUERY_PQF { 20003 }
47 sub SORTBY { 20004 }
48 sub CLONE { 20005 }
49 sub PACKAGE { 20006 }
50 sub SCANTERM { 20007 }
51
52 # The "Event" package contains constants returned by last_event()
53 package ZOOM::Event;
54 sub NONE { Net::Z3950::ZOOM::EVENT_NONE }
55 sub CONNECT { Net::Z3950::ZOOM::EVENT_CONNECT }
56 sub SEND_DATA { Net::Z3950::ZOOM::EVENT_SEND_DATA }
57 sub RECV_DATA { Net::Z3950::ZOOM::EVENT_RECV_DATA }
58 sub TIMEOUT { Net::Z3950::ZOOM::EVENT_TIMEOUT }
59 sub UNKNOWN { Net::Z3950::ZOOM::EVENT_UNKNOWN }
60 sub SEND_APDU { Net::Z3950::ZOOM::EVENT_SEND_APDU }
61 sub RECV_APDU { Net::Z3950::ZOOM::EVENT_RECV_APDU }
62 sub RECV_RECORD { Net::Z3950::ZOOM::EVENT_RECV_RECORD }
63 sub RECV_SEARCH { Net::Z3950::ZOOM::EVENT_RECV_SEARCH }
64
65 # ----------------------------------------------------------------------------
66
67 package ZOOM;
68
69 sub diag_str {
70     my($code) = @_;
71
72     # Special cases for error specific to the OO layer
73     if ($code == ZOOM::Error::CREATE_QUERY) {
74         return "can't create query object";
75     } elsif ($code == ZOOM::Error::QUERY_CQL) {
76         return "can't set CQL query";
77     } elsif ($code == ZOOM::Error::QUERY_PQF) {
78         return "can't set prefix query";
79     } elsif ($code == ZOOM::Error::SORTBY) {
80         return "can't set sort-specification";
81     } elsif ($code == ZOOM::Error::CLONE) {
82         return "can't clone record";
83     } elsif ($code == ZOOM::Error::PACKAGE) {
84         return "can't create package";
85     } elsif ($code == ZOOM::Error::SCANTERM) {
86         return "can't retrieve term from scan-set";
87     }
88
89     return Net::Z3950::ZOOM::diag_str($code);
90 }
91
92 sub _oops {
93     my($code, $addinfo, $diagset) = @_;
94
95     die new ZOOM::Exception($code, diag_str($code), $addinfo, $diagset);
96 }
97
98 # ----------------------------------------------------------------------------
99
100 package ZOOM::Exception;
101
102 sub new {
103     my $class = shift();
104     my($code, $message, $addinfo, $diagset) = @_;
105
106     return bless {
107         code => $code,
108         message => $message,
109         addinfo => $addinfo,
110         diagset => $diagset || "ZOOM",
111     }, $class;
112 }
113
114 sub code {
115     my $this = shift();
116     return $this->{code};
117 }
118
119 sub message {
120     my $this = shift();
121     return $this->{message};
122 }
123
124 sub addinfo {
125     my $this = shift();
126     return $this->{addinfo};
127 }
128
129 sub diagset {
130     my $this = shift();
131     return $this->{diagset};
132 }
133
134 sub render {
135     my $this = shift();
136     my $res = "ZOOM error " . $this->code() . ' "' . $this->message() . '"';
137     $res .= ' (addinfo: "' . $this->addinfo() . '")' if $this->addinfo();
138     return $res;
139 }
140
141 # This means that untrapped exceptions render nicely.
142 use overload '""' => \&render;
143
144 # ----------------------------------------------------------------------------
145
146 package ZOOM::Options;
147
148 sub new {
149     my $class = shift();
150     my($p1, $p2) = @_;
151
152     my $opts;
153     if (@_ == 0) {
154         $opts = Net::Z3950::ZOOM::options_create();
155     } elsif (@_ == 1) {
156         $opts = Net::Z3950::ZOOM::options_create_with_parent($p1->_opts());
157     } elsif (@_ == 2) {
158         $opts = Net::Z3950::ZOOM::options_create_with_parent2($p1->_opts(),
159                                                               $p2->_opts());
160     } else {
161         die "can't make $class object with more than 2 parents";
162     }
163
164     return bless {
165         _opts => $opts,
166     }, $class;
167 }
168
169 # PRIVATE to this class and ZOOM::Connection::create() and
170 # ZOOM::Connection::package()
171 #
172 sub _opts {
173     my $this = shift();
174
175     my $_opts = $this->{_opts};
176     die "{_opts} undefined: has this Options block been destroy()ed?"
177         if !defined $_opts;
178
179     return $_opts;
180 }
181
182 sub option {
183     my $this = shift();
184     my($key, $value) = @_;
185
186     my $oldval = Net::Z3950::ZOOM::options_get($this->_opts(), $key);
187     Net::Z3950::ZOOM::options_set($this->_opts(), $key, $value)
188         if defined $value;
189
190     return $oldval;
191 }
192
193 sub option_binary {
194     my $this = shift();
195     my($key, $value) = @_;
196
197     my $dummylen = 0;
198     my $oldval = Net::Z3950::ZOOM::options_getl($this->_opts(),
199                                                 $key, $dummylen);
200     Net::Z3950::ZOOM::options_setl($this->_opts(), $key,
201                                    $value, length($value))
202         if defined $value;
203
204     return $oldval;
205 }
206
207 # This is a bit stupid, since the scalar values that Perl returns from
208 # option() can be used as a boolean; but it's just possible that some
209 # applications will rely on ZOOM_options_get_bool()'s idiosyncratic
210 # interpretation of what constitutes truth.
211 #
212 sub bool {
213     my $this = shift();
214     my($key, $default) = @_;
215
216     return Net::Z3950::ZOOM::options_get_bool($this->_opts(), $key, $default);
217 }
218
219 # .. and the next two are even more stupid
220 sub int {
221     my $this = shift();
222     my($key, $default) = @_;
223
224     return Net::Z3950::ZOOM::options_get_int($this->_opts(), $key, $default);
225 }
226
227 sub set_int {
228     my $this = shift();
229     my($key, $value) = @_;
230
231     Net::Z3950::ZOOM::options_set_int($this->_opts(), $key, $value);
232 }
233
234 #   ### Feel guilty.  Feel very, very guilty.  I've not been able to
235 #       get the callback memory-management right in "ZOOM.xs", with
236 #       the result that the values of $function and $udata passed into
237 #       this function, which are on the stack, have sometimes been
238 #       freed by the time they're used by __ZOOM_option_callback(),
239 #       with hilarious results.  To avoid this, I copy the values into
240 #       module-scoped globals, and pass _those_ into the extension
241 #       function.  To avoid overwriting those globals by subsequent
242 #       calls, I keep all the old ones, pushed onto the @_function and
243 #       @_udata arrays, which means that THIS FUNCTION LEAKS MEMORY
244 #       LIKE IT'S GOING OUT OF FASHION.  Not nice.  One day, I should
245 #       fix this, but for now there's more important fish to fry.
246 #
247 my(@_function, @_udata);
248 sub set_callback {
249     my $o1 = shift();
250     my($function, $udata) = @_;
251
252     push @_function, $function;
253     push @_udata, $udata;
254     Net::Z3950::ZOOM::options_set_callback($o1->_opts(),
255                                            $_function[-1], $_udata[-1]);
256 }
257
258 sub destroy {
259     my $this = shift();
260
261     Net::Z3950::ZOOM::options_destroy($this->_opts());
262     $this->{_opts} = undef;
263 }
264
265
266 # ----------------------------------------------------------------------------
267
268 package ZOOM::Connection;
269
270 sub new {
271     my $class = shift();
272     my($host, $port) = @_;
273
274     my $_conn = Net::Z3950::ZOOM::connection_new($host, $port || 0);
275     my $conn = bless {
276         host => $host,
277         port => $port,
278         _conn => $_conn,
279     };
280     $conn->_check();
281     return $conn;
282 }
283
284 # PRIVATE to this class
285 sub _conn {
286     my $this = shift();
287
288     my $_conn = $this->{_conn};
289     die "{_conn} undefined: has this Connection been destroy()ed?"
290         if !defined $_conn;
291
292     return $_conn;
293 }
294
295 sub _check {
296     my $this = shift();
297
298     my($errcode, $errmsg, $addinfo, $diagset) = (undef, "x", "x", "x");
299     $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
300                                                     $addinfo, $diagset);
301     die new ZOOM::Exception($errcode, $errmsg, $addinfo, $diagset)
302         if $errcode;
303 }
304
305 sub create {
306     my $class = shift();
307     my($options) = @_;
308
309     my $_conn = Net::Z3950::ZOOM::connection_create($options->_opts());
310     return bless {
311         host => undef,
312         port => undef,
313         _conn => $_conn,
314     };
315 }
316
317 sub error_x {
318     my $this = shift();
319
320     my($errcode, $errmsg, $addinfo, $diagset) = (undef, "dummy", "dummy", "d");
321     $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
322                                                     $addinfo, $diagset);
323     return ($errcode, $errmsg, $addinfo, $diagset);
324 }
325
326 sub errcode {
327     my $this = shift();
328     return Net::Z3950::ZOOM::connection_errcode($this->_conn());
329 }
330
331 sub errmsg {
332     my $this = shift();
333     return Net::Z3950::ZOOM::connection_errmsg($this->_conn());
334 }
335
336 sub addinfo {
337     my $this = shift();
338     return Net::Z3950::ZOOM::connection_addinfo($this->_conn());
339 }
340
341 sub diagset {
342     my $this = shift();
343     return Net::Z3950::ZOOM::connection_diagset($this->_conn());
344 }
345
346 sub connect {
347     my $this = shift();
348     my($host, $port) = @_;
349
350     Net::Z3950::ZOOM::connection_connect($this->_conn(), $host, $port);
351     $this->_check();
352     # No return value
353 }
354
355 sub option {
356     my $this = shift();
357     my($key, $value) = @_;
358
359     my $oldval = Net::Z3950::ZOOM::connection_option_get($this->_conn(), $key);
360     Net::Z3950::ZOOM::connection_option_set($this->_conn(), $key, $value)
361         if defined $value;
362
363     return $oldval;
364 }
365
366 sub option_binary {
367     my $this = shift();
368     my($key, $value) = @_;
369
370     my $dummylen = 0;
371     my $oldval = Net::Z3950::ZOOM::connection_option_getl($this->_conn(),
372                                                           $key, $dummylen);
373     Net::Z3950::ZOOM::connection_option_setl($this->_conn(), $key,
374                                              $value, length($value))
375         if defined $value;
376
377     return $oldval;
378 }
379
380 sub search {
381     my $this = shift();
382     my($query) = @_;
383
384     my $_rs = Net::Z3950::ZOOM::connection_search($this->_conn(),
385                                                   $query->_query());
386     $this->_check();
387     return _new ZOOM::ResultSet($this, $query, $_rs);
388 }
389
390 sub search_pqf {
391     my $this = shift();
392     my($pqf) = @_;
393
394     my $_rs = Net::Z3950::ZOOM::connection_search_pqf($this->_conn(), $pqf);
395     $this->_check();
396     return _new ZOOM::ResultSet($this, $pqf, $_rs);
397 }
398
399 sub scan_pqf {
400     my $this = shift();
401     my($startterm) = @_;
402
403     my $_ss = Net::Z3950::ZOOM::connection_scan($this->_conn(), $startterm);
404     $this->_check();
405     return _new ZOOM::ScanSet($this, $startterm, $_ss);
406 }
407
408 sub scan {
409     my $this = shift();
410     my($query) = @_;
411
412     my $_ss = Net::Z3950::ZOOM::connection_scan1($this->_conn(),
413                                                  $query->_query());
414     $this->_check();
415     return _new ZOOM::ScanSet($this, $query, $_ss);
416 }
417
418 sub package {
419     my $this = shift();
420     my($options) = @_;
421
422     my $_o = defined $options ? $options->_opts() :
423         Net::Z3950::ZOOM::options_create();
424     my $_p = Net::Z3950::ZOOM::connection_package($this->_conn(), $_o)
425         or ZOOM::_oops(ZOOM::Error::PACKAGE);
426
427     return _new ZOOM::Package($this, $options, $_p);
428 }
429
430 sub destroy {
431     my $this = shift();
432
433     Net::Z3950::ZOOM::connection_destroy($this->_conn());
434     $this->{_conn} = undef;
435 }
436
437
438 # ----------------------------------------------------------------------------
439
440 package ZOOM::Query;
441
442 sub new {
443     my $class = shift();
444     die "You can't create $class objects: it's a virtual base class";
445 }
446
447 # PRIVATE to this class and ZOOM::Connection::search()
448 sub _query {
449     my $this = shift();
450
451     my $_query = $this->{_query};
452     die "{_query} undefined: has this Query been destroy()ed?"
453         if !defined $_query;
454
455     return $_query;
456 }
457
458 sub sortby {
459     my $this = shift();
460     my($sortby) = @_;
461
462     Net::Z3950::ZOOM::query_sortby($this->_query(), $sortby) == 0
463         or ZOOM::_oops(ZOOM::Error::SORTBY, $sortby);
464 }
465
466 sub destroy {
467     my $this = shift();
468
469     Net::Z3950::ZOOM::query_destroy($this->_query());
470     $this->{_query} = undef;
471 }
472
473
474 package ZOOM::Query::CQL;
475 our @ISA = qw(ZOOM::Query);
476
477 sub new {
478     my $class = shift();
479     my($string) = @_;
480
481     my $q = Net::Z3950::ZOOM::query_create()
482         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
483     Net::Z3950::ZOOM::query_cql($q, $string) == 0
484         or ZOOM::_oops(ZOOM::Error::QUERY_CQL, $string);
485
486     return bless {
487         _query => $q,
488     }, $class;
489 }
490
491
492 package ZOOM::Query::PQF;
493 our @ISA = qw(ZOOM::Query);
494
495 sub new {
496     my $class = shift();
497     my($string) = @_;
498
499     my $q = Net::Z3950::ZOOM::query_create()
500         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
501     Net::Z3950::ZOOM::query_prefix($q, $string) == 0
502         or ZOOM::_oops(ZOOM::Error::QUERY_PQF, $string);
503
504     return bless {
505         _query => $q,
506     }, $class;
507 }
508
509
510 # ----------------------------------------------------------------------------
511
512 package ZOOM::ResultSet;
513
514 sub new {
515     my $class = shift();
516     die "You can't create $class objects directly";
517 }
518
519 # PRIVATE to ZOOM::Connection::search() and ZOOM::Connection::search_pqf()
520 sub _new {
521     my $class = shift();
522     my($conn, $query, $_rs) = @_;
523
524     return bless {
525         conn => $conn,
526         query => $query,        # This is not currently used, which is
527                                 # just as well since it could be
528                                 # either a string (when the RS is
529                                 # created with search_pqf()) or a
530                                 # ZOOM::Query object (when it's
531                                 # created with search())
532         _rs => $_rs,
533     }, $class;
534 }
535
536 # PRIVATE to this class
537 sub _rs {
538     my $this = shift();
539
540     my $_rs = $this->{_rs};
541     die "{_rs} undefined: has this ResultSet been destroy()ed?"
542         if !defined $_rs;
543
544     return $_rs;
545 }
546
547 sub option {
548     my $this = shift();
549     my($key, $value) = @_;
550
551     my $oldval = Net::Z3950::ZOOM::resultset_option_get($this->_rs(), $key);
552     Net::Z3950::ZOOM::resultset_option_set($this->_rs(), $key, $value)
553         if defined $value;
554
555     return $oldval;
556 }
557
558 sub size {
559     my $this = shift();
560
561     return Net::Z3950::ZOOM::resultset_size($this->_rs());
562 }
563
564 sub record {
565     my $this = shift();
566     my($which) = @_;
567
568     my $_rec = Net::Z3950::ZOOM::resultset_record($this->_rs(), $which);
569     $this->{conn}->_check();
570
571     # Even if no error has occurred, I think record() might
572     # legitimately return undef if we're running in asynchronous mode
573     # and the record just hasn't been retrieved yet.  This goes double
574     # for record_immediate().
575     return undef if !defined $_rec;
576
577     # For some reason, I have to use the explicit "->" syntax in order
578     # to invoke the ZOOM::Record constructor here, even though I don't
579     # have to do the same for _new ZOOM::ResultSet above.  Weird.
580     return ZOOM::Record->_new($this, $which, $_rec);
581 }
582
583 sub record_immediate {
584     my $this = shift();
585     my($which) = @_;
586
587     my $_rec = Net::Z3950::ZOOM::resultset_record_immediate($this->_rs(),
588                                                             $which);
589     $this->{conn}->_check();
590     # The record might legitimately not be there yet
591     return undef if !defined $_rec;
592
593     return ZOOM::Record->_new($this, $which, $_rec);
594 }
595
596 sub cache_reset {
597     my $this = shift();
598
599     Net::Z3950::ZOOM::resultset_cache_reset($this->_rs());
600 }
601
602 sub records {
603     my $this = shift();
604     my($start, $count, $return_records) = @_;
605
606     my $raw = Net::Z3950::ZOOM::resultset_records($this->_rs(), $start, $count,
607                                                   $return_records);
608     # By design, $raw may be undefined (if $return_records is true)
609     return undef if !defined $raw;
610
611     # We need to package up the returned records in ZOOM::Record objects
612     my @res = ();
613     for my $i (0 .. @$raw-1) {
614         my $_rec = $raw->[$i];
615         if (!defined $_rec) {
616             push @res, undef;
617         } else {
618             push @res, ZOOM::Record->_new($this, $start+$i, $_rec);
619         }
620     }
621
622     return \@res;
623 }
624
625 sub sort {
626     my $this = shift();
627     my($sort_type, $sort_spec) = @_;
628
629     return Net::Z3950::ZOOM::resultset_sort1($this->_rs(),
630                                              $sort_type, $sort_spec);
631 }
632
633 sub destroy {
634     my $this = shift();
635
636     Net::Z3950::ZOOM::resultset_destroy($this->_rs());
637     $this->{_rs} = undef;
638 }
639
640
641 # ----------------------------------------------------------------------------
642
643 package ZOOM::Record;
644
645 sub new {
646     my $class = shift();
647     die "You can't create $class objects directly";
648 }
649
650 # PRIVATE to ZOOM::ResultSet::record(),
651 # ZOOM::ResultSet::record_immediate(), ZOOM::ResultSet::records() and
652 # ZOOM::Record::clone()
653 #
654 sub _new {
655     my $class = shift();
656     my($rs, $which, $_rec) = @_;
657
658     return bless {
659         rs => $rs,
660         which => $which,
661         _rec => $_rec,
662     }, $class;
663 }
664
665 # PRIVATE to this class
666 sub _rec {
667     my $this = shift();
668
669     my $_rec = $this->{_rec};
670     die "{_rec} undefined: has this Record been destroy()ed?"
671         if !defined $_rec;
672
673     return $_rec;
674 }
675
676 sub render {
677     my $this = shift();
678
679     my $len = 0;
680     my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "render", $len);
681     # I don't think we need '$len' at all.  ### Probably the Perl-to-C
682     # glue code should use the value of `len' as well as the opaque
683     # data-pointer returned, to ensure that the SV contains all of the
684     # returned data and does not stop at the first NUL character in
685     # binary data.  Carefully check the ZOOM_record_get() documentation.
686     return $string;
687 }
688
689 sub raw {
690     my $this = shift();
691
692     my $len = 0;
693     my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "raw", $len);
694     # See comment about $len in render()
695     return $string;
696 }
697
698 sub clone {
699     my $this = shift();
700
701     my $raw = Net::Z3950::ZOOM::record_clone($this->_rec())
702         or ZOOM::_oops(ZOOM::Error::CLONE);
703
704     # Arg 1 (rs) is undefined as the new record doesn't belong to an RS
705     return _new ZOOM::Record(undef, undef, $raw);
706 }
707
708 sub destroy {
709     my $this = shift();
710
711     Net::Z3950::ZOOM::record_destroy($this->_rec());
712     $this->{_rec} = undef;
713 }
714
715
716 # ----------------------------------------------------------------------------
717
718 package ZOOM::ScanSet;
719
720 sub new {
721     my $class = shift();
722     die "You can't create $class objects directly";
723 }
724
725 # PRIVATE to ZOOM::Connection::scan(),
726 sub _new {
727     my $class = shift();
728     my($conn, $startterm, $_ss) = @_;
729
730     return bless {
731         conn => $conn,
732         startterm => $startterm,# This is not currently used, which is
733                                 # just as well since it could be
734                                 # either a string (when the SS is
735                                 # created with scan()) or a
736                                 # ZOOM::Query object (when it's
737                                 # created with scan1())
738         _ss => $_ss,
739     }, $class;
740 }
741
742 # PRIVATE to this class
743 sub _ss {
744     my $this = shift();
745
746     my $_ss = $this->{_ss};
747     die "{_ss} undefined: has this ScanSet been destroy()ed?"
748         if !defined $_ss;
749
750     return $_ss;
751 }
752
753 sub option {
754     my $this = shift();
755     my($key, $value) = @_;
756
757     my $oldval = Net::Z3950::ZOOM::scanset_option_get($this->_ss(), $key);
758     Net::Z3950::ZOOM::scanset_option_set($this->_ss(), $key, $value)
759         if defined $value;
760
761     return $oldval;
762 }
763
764 sub size {
765     my $this = shift();
766
767     return Net::Z3950::ZOOM::scanset_size($this->_ss());
768 }
769
770 sub term {
771     my $this = shift();
772     my($which) = @_;
773
774     my($occ, $len) = (0, 0);
775     my $term = Net::Z3950::ZOOM::scanset_term($this->_ss(), $which,
776                                               $occ, $len)
777         or ZOOM::_oops(ZOOM::Error::SCANTERM);
778
779     die "length of term '$term' differs from returned len=$len"
780         if length($term) != $len;
781
782     return ($term, $occ);
783 }
784
785 sub display_term {
786     my $this = shift();
787     my($which) = @_;
788
789     my($occ, $len) = (0, 0);
790     my $term = Net::Z3950::ZOOM::scanset_display_term($this->_ss(), $which,
791                                                       $occ, $len)
792         or ZOOM::_oops(ZOOM::Error::SCANTERM);
793
794     die "length of display term '$term' differs from returned len=$len"
795         if length($term) != $len;
796
797     return ($term, $occ);
798 }
799
800 sub destroy {
801     my $this = shift();
802
803     Net::Z3950::ZOOM::scanset_destroy($this->_ss());
804     $this->{_ss} = undef;
805 }
806
807
808 # ----------------------------------------------------------------------------
809
810 package ZOOM::Package;
811
812 sub new {
813     my $class = shift();
814     die "You can't create $class objects directly";
815 }
816
817 # PRIVATE to ZOOM::Connection::package(),
818 sub _new {
819     my $class = shift();
820     my($conn, $options, $_p) = @_;
821
822     return bless {
823         conn => $conn,
824         options => $options,
825         _p => $_p,
826     }, $class;
827 }
828
829 # PRIVATE to this class
830 sub _p {
831     my $this = shift();
832
833     my $_p = $this->{_p};
834     die "{_p} undefined: has this Package been destroy()ed?"
835         if !defined $_p;
836
837     return $_p;
838 }
839
840 sub option {
841     my $this = shift();
842     my($key, $value) = @_;
843
844     my $oldval = Net::Z3950::ZOOM::package_option_get($this->_p(), $key);
845     Net::Z3950::ZOOM::package_option_set($this->_p(), $key, $value)
846         if defined $value;
847
848     return $oldval;
849 }
850
851 sub send {
852     my $this = shift();
853     my($type) = @_;
854
855     Net::Z3950::ZOOM::package_send($this->_p(), $type);
856     $this->{conn}->_check();
857 }
858
859 sub destroy {
860     my $this = shift();
861
862     Net::Z3950::ZOOM::package_destroy($this->_p());
863     $this->{_p} = undef;
864 }
865
866
867 1;