Example invocation.
[ZOOM-Perl-moved-to-github.git] / samples / zoom / zoomtst3.pl
1 # $Id: zoomtst3.pl,v 1.3 2006-04-07 12:03:27 mike Exp $
2 #
3 # See ../README for a description of this program.
4 # perl -I../../blib/lib -I../../blib/arch zoomtst3.pl <t1> [...] <tN> <query>
5 # for example:
6 # perl -I../../blib/lib -I../../blib/arch zoomtst3.pl z3950.loc.gov:7090/Voyager bagel.indexdata.com:210/gils mineral
7
8 use strict;
9 use warnings;
10 use ZOOM;
11
12 if (@ARGV < 2) {
13     print STDERR "Usage: $0 target1 target2 ... targetN query\n";
14     print STDERR "      eg. $0 bagel.indexdata.dk/gils localhost:9999 fish\n";
15     exit 1;
16 }
17
18 my $n = @ARGV-1;
19 my(@z, @r);                     # connections, result sets
20 my $o = new ZOOM::Options();
21 $o->option(async => 1);
22
23 # Get first 10 records of result set (using piggyback)
24 $o->option(count => 10);
25
26 # Preferred record syntax
27 $o->option(preferredRecordSyntax => "usmarc");
28 $o->option(elementSetName => "F");
29
30 # Connect to all targets: options are the same for all of them
31 for (my $i = 0; $i < $n; $i++) {
32     $z[$i] = create ZOOM::Connection($o);
33     $z[$i]->connect($ARGV[$i]);
34 }
35
36 # Search all
37 for (my $i = 0; $i < $n; $i++) {
38     $r[$i] = $z[$i]->search_pqf($ARGV[-1]);
39 }
40
41 # Network I/O.  Pass number of connections and array of connections
42 while ((my $i = ZOOM::event(\@z)) != 0) {
43     my $ev = $z[$i-1]->last_event();
44     print("connection ", $i-1, ": event $ev (", ZOOM::event_str($ev), ")\n");
45     ### It would be nice to display results as they come in.
46 }
47
48 # No more to be done.  Inspect results
49 for (my $i = 0; $i < $n; $i++) {
50     my $tname = $ARGV[$i];
51     my($error, $errmsg, $addinfo, $diagset) = $z[$i]->error_x();
52     if ($error) {
53         print STDERR "$tname error: $errmsg ($error) $addinfo\n";
54         next;
55     }
56
57     # OK, no major errors.  Look at the result count
58     my $size = $r[$i]->size();
59     print "$tname: $size hits\n";
60
61     # Go through all records at target
62     $size = 10 if $size > 10;
63     for (my $pos = 0; $pos < $size; $pos++) {
64         print "$tname: fetching ", $pos+1, " of $size\n";
65         my $tmp = $r[$i]->record($pos);
66         if (!defined $tmp) {
67             print "$tname: can't get record ", $pos+1, "\n";
68             next;
69         }
70         my $rec = $tmp->render();
71         if (!defined $rec) {
72             print "$tname: can't render record ", $pos+1, "\n";
73             next;
74         }
75         print $pos+1, "\n", $rec, "\n";
76     }
77 }
78
79 # Housekeeping
80 for (my $i = 0; $i < $n; $i++) {
81     $r[$i]->destroy();
82     $z[$i]->destroy();
83 }
84
85 $o->destroy();