86c75af6ae504e080e8f1c7c29998c91fc352ac9
[ZOOM-Perl-moved-to-github.git] / samples / net-z3950 / zoomtst1.pl
1 # $Id: zoomtst1.pl,v 1.4 2006-11-02 17:48:25 mike Exp $
2 #
3 # See ../README for a description of this program.
4 # perl -I../../blib/lib -I../../blib/arch zoomtst1.pl <target> <query>
5
6 use strict;
7 use warnings;
8 use Net::Z3950;
9
10 if (@ARGV != 2) {
11     print STDERR "Usage: $0 target query\n";
12     print STDERR "      eg. $0 z3950.indexdata.dk/gils computer\n";
13     exit 1;
14 }
15
16 my($host, $query) = @ARGV;
17
18 # Database name defaults to "Default" in Net::Z3950 and must be overridden
19 $host =~ s/\/(.*)//;
20 my $db = $1;
21 my $conn = new Net::Z3950::Connection($host, 0, databaseName => $db)
22     or die "can't connect to '$host': $!";
23
24 # Default format is GRS-1 in Net::Z3950
25 $conn->option(preferredRecordSyntax => "usmarc");
26
27 # Default format is "B" in Net::Z3950
28 $conn->option(elementSetName => "F");
29
30 my $rs = $conn->search(-prefix => $query)
31     or die "can't search for '$query': ", $conn->errmsg();
32 my $n = $rs->size();
33 print "Query '$query' found $n records\n";
34
35 # Note that the record-index is 1-based here, 0-based in ZOOM-C
36 for my $i (1..$n) {
37     my $rec = $rs->record($i)
38         or die "can't fetch record $i: ", $rs->errmsg();
39     print "=== Record $i of $n ===\n";
40
41     # Rendering format for MARC records is different
42     print $rec->render(), "\n";
43 }
44
45 $rs->delete();
46 $conn->close();