7b86946658e662e24afd6d46fe6fddbf64f0cfae
[ZOOM-Perl-moved-to-github.git] / samples / zoom / zoomscan.pl
1 #!/usr/bin/perl -w
2
3 # $Id: zoomscan.pl,v 1.5 2008-05-14 13:27:32 mike Exp $
4 #
5 # This is the scanning counterpart to zoomscan.pl's searching
6 # perl -I../../blib/lib -I../../blib/arch zoomscan.pl <target> <scanQuery>
7 #
8 # For example (using Z39.50 and SRW, Type-1 and CQL):
9 # perl zoomscan.pl tcp:localhost:8018/IR-Explain---1 '@attr 1=dc.title the'
10 # perl zoomscan.pl http://localhost:8018/IR-Explain---1 '@attr 1=dc.title the'
11 # perl zoomscan.pl -q http://localhost:8018/IR-Explain---1 'dc.title=the'
12
13 use strict;
14 use warnings;
15 use Getopt::Std;
16 use ZOOM;
17
18 my %opts;
19 if (!getopts('q', \%opts) || @ARGV != 2) {
20     print STDERR "Usage: $0 [options] target scanQuery
21         -q      Query is CQL [default: PQF]
22         eg. $0 z3950.indexdata.dk/gils computer\n";
23     exit 1;
24 }
25
26 my($host, $scanQuery) = @ARGV;
27
28 eval {
29     my $conn = new ZOOM::Connection($host, 0);
30     $conn->option(preferredRecordSyntax => "usmarc");
31     ### Could use ZOOM::Query::CQL below, but that only works in SRU/W.
32     my $q = $opts{q} ? new ZOOM::Query::CQL($scanQuery) :
33                        new ZOOM::Query::PQF($scanQuery);
34     my $ss = $conn->scan($q);
35     my $n = $ss->size();
36     for my $i (0..$n-1) {
37         my($term, $occ) = $ss->term($i);
38         print $i+1, ": $term";
39         print " ($occ)" if defined $occ;
40         print "\n";
41     }
42     
43     $ss->destroy();
44     $conn->destroy();
45 }; if ($@) {
46     die "Non-ZOOM error: $@" if !$@->isa("ZOOM::Exception");
47     print STDERR "ZOOM error $@\n";
48     exit 1;
49 }