The ``Hello World'' Zoo

24th August 2002

Here, we show different versions of the canonical ZOOM ``Hello World'' program in various languages supported by the implemented bindings:

1. The C Binding
2. The C++ Binding
3. The Perl Binding
4. The Python Binding
5. The Java Binding
6. The Visual Basic Binding
7. The Scheme Binding
8. The Tcl Binding
9. The Ruby Binding
10. The Ada Binding
11. The Shell Commands Binding

All these programs do the same thing: they are tiny Z39.50 clients that fetch and display the MARC record for Farlow & Brett Surman's The Complete Dinosaur from the Library of Congress.

1. The C Binding

#include <stdio.h>
#include <yaz/zoom.h>

int main(int argc, char **argv)
{
    ZOOM_connection z = ZOOM_connection_new ("z3950.loc.gov:7090/Voyager", 0);
    ZOOM_resultset r;
    const char *rec;

    ZOOM_connection_option_set(z, "preferredRecordSyntax", "USMARC");
    r = ZOOM_connection_search_pqf(z, "@attr 1=7 0253333490");
    rec =  ZOOM_record_get (ZOOM_resultset_record (r, 0), "render", 0);
    if (rec)
        printf ("%s\n", rec);
    return 0;
} 
[download] [read more]

2. The C++ Binding

#include <iostream>
#include <yazpp/zoom.h>

using namespace ZOOM;

int main (int argc, char **argv)
{
  connection conn ("z3950.loc.gov", 7090);
  conn.option ("databaseName", "Voyager");
  resultSet rs (conn, prefixQuery ("@attr 1=7 0253333490"));
  const record rec(rs, 0);
  std::cout << rec.render () << std::endl;
}
[download] [read more]

3. The Perl Binding

use ZOOM;
$conn = new ZOOM::Connection('z3950.loc.gov', 7090,
			     databaseName => "Voyager",
			     preferredRecordSyntax => "USMARC");
$rs = $conn->search_pqf('@attr 1=7 0253333490');
$rec = $rs->record(0);
print $rec->render();
[download] [read more]

4. The Python Binding

from PyZ3950 import zoom

conn = zoom.Connection ('z3950.loc.gov', 7090)
conn.databaseName = 'Voyager'
conn.preferredRecordSyntax = 'USMARC'

query = zoom.Query ('CCL', 'isbn=0253333490')

res = conn.search (query)
print res [0]

conn.close ()
[download] [read more]

5. The Java Binding

import uk.org.miketaylor.zoom.*;

class Canonical {
    public static void main (String[] args) throws Exception {
	Connection conn = new Connection("z3950.loc.gov", 7090);
	conn.option("databaseName", "Voyager");
	conn.option("preferredRecordSyntax", "USMARC");
	Query q = new PrefixQuery("@attr 1=7 0253333490");
	ResultSet rs = conn.search(q);
	System.out.println(rs.getRecord(0).render());
    }
}
[download] [read more]

6. The Visual Basic Binding

Set zoom = WScript.CreateObject("VBZOOMC.ZoomFactory")
Set zconn = zoom.CreateZoomConnection("z3950.loc.gov", 7090)
zconn.SetOption "databaseName","Voyager"
zconn.SetOption "preferredRecordSyntax", "USmarc"
Set zquery = zoom.CreateZoomQuery("@attr 1=7 0253333490")
Set zrs = zconn.Search(zquery)
WScript.Echo zrs.GetRecord(0).RenderRecord
[download] [read more]

7. The Scheme Binding

(module hello_world
        (include "bzoom.sch")
        (include "bzoom.scm")
        (main main))

(define main (lambda (args)
               (let ((conn
                      (zoom:connection_new "z3950.loc.gov:7090/Voyager" 0)))
                 (zoom:connection_option_set 
                  conn "preferredRecordSyntax" "USMARC")
                 (let* ((r (zoom:connection_search_pqf 
                            conn "@attr 1=7 0253333490"))
                        (rec (zoom:record_get 
                              (zoom:resultset_record r 0)
                              "render")))
                   (if (pair? rec)
                       (display (car rec)))
                   0))) )
[download] [read more]

8. The Tcl Binding

source zoom-1.2.tcl

object Connection conn "z3950.loc.gov" 7090
conn.setOption "database" "Voyager"
conn.setOption "preferredRecordSyntax" "MARC"

object Query query1 0 "7 0253333490"
set rs [conn.search [query1]]
set rec [$rs.getRecord 0]
puts [$rec.getRaw]
[download] [read more]

9. The Ruby Binding

require 'zoom'

ZOOM::Connection.open('z3950.loc.gov', 7090) do |conn|
    conn.database_name = 'Voyager'
    conn.preferred_record_syntax = 'USMARC'
    rset = conn.search('@attr 1=7 0253333490')
    p rset[0]
end
[download] [read more]

10. The Ada Binding

with Zoom.Connections; use Zoom.Connections;
with Zoom.Records;     use Zoom.Records;
with Zoom.Result_Sets; use Zoom.Result_Sets;
with Zoom.Queries;     use Zoom.Queries;

procedure Canonical is
   Conn : Connection   := New_Connection ("z3950.loc.gov", 7090);
   Q    : Prefix_Query := Create ("@attr 1=7 0253333490");
   Rs   : Result_Set;
begin
   Set_Option (Conn, "databaseName", "Voyager");
   Rs := Search (Conn, Q);
   Render (Get_Record (Rs, 0));
end Canonical;
[download] [read more]

11. The Shell Commands Binding

connect z3950.loc.gov:7090/Voyager
set preferredRecordSyntax USMARC
search @attr 1=7 0253333490
show 0 1
[download] [read more]

Feedback to <mike@indexdata.com> is welcome!