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