Persistent::Sybase - A Persistent Class implemented using a Sybase database


  use Persistent::Sybase;
  use English;  # import readable variable names like $EVAL_ERROR

  eval {  ### in case an exception is thrown ###

    ### allocate a persistent object ###
    my $emp = new Persistent::Sybase($data_source, $username, $password, $table);

    ### define attributes of the object ###
    $emp->add_attribute('empno',    'ID',         'Number',   undef, 4);
    $emp->add_attribute('ename',    'Persistent', 'VarChar',  undef, 10);
    $emp->add_attribute('job',      'Persistent', 'VarChar',  undef, 9);
    $emp->add_attribute('mgr',      'Persistent', 'Number',   undef, 4);
    $emp->add_attribute('hiredate', 'Persistent', 'DateTime', undef);
    $emp->add_attribute('sal',      'Persistent', 'Number',   undef, 7, 2);
    $emp->add_attribute('comm',     'Persistent', 'Number',   undef, 7, 2);
    $emp->add_attribute('deptno',   'Persistent', 'Number',   undef, 2);

    ### query the datastore for some objects ###
                              sal > 1000 and
                              job = 'CLERK' and
                              ename LIKE 'M%'
                          }, "sal, ename");
    while ($emp->restore_next()) {
      printf "ename = %s, emp# = %s, sal = %s, hiredate = %s\n",
             $emp->ename, $emp->empno, $emp->sal, $emp->hiredate;

  if ($EVAL_ERROR) {  ### catch those exceptions! ###
    print "An error occurred: $EVAL_ERROR\n";


This is a Persistent class that uses a Sybase database table to store and retrieve objects. This class can be instantiated directly or subclassed. The methods described below are unique to this class, and all other methods that are provided by this class are documented in the Persistent documentation. The Persistent documentation has a very thorough introduction to using the Persistent framework of classes.

This class is part of the Persistent Sybase package which is available from:


Before we get started describing the methods in detail, it should be noted that all error handling in this class is done with exceptions. So you should wrap an eval block around all of your code. Please see the Persistent documentation for more information on exception handling in Perl.


new -- Object Constructor

  use Persistent::Sybase;

  eval {
    $obj = new Persistent::Sybase($data_source, $username, $password, $table);
    ### or ###
    $obj = new Persistent::Sybase($dbh, $table);
  croak "Exception caught: $@" if $@;

Allocates an object. This method throws Perl execeptions so use it with an eval block.


These are the same as for the datastore method below.

datastore -- Sets/Returns the Data Store Parameters

  eval {
    ### set the data store ###
    $obj->datastore($data_source, $username, $password, $table);
    ### or ###
    $obj->datastore($dbh, $table);

    ### get the data store ###
    $dbh = $obj->datastore();
  croak "Exception caught: $@" if $@;

Returns (and optionally sets) the data store of the object. This method throws Perl execeptions so use it with an eval block.



DBI data source name for the database. Please refer to the DBD::Sybase documentation for valid data source names.


Connect to the database with this given username


Password for the username


Table in the database that contains the objects. This table should exist prior to instantiating a Persistent class that will use it.

$dbh (also a return value)

DBI handle to the database

restore_where -- Conditionally Restoring Objects

  use Persistent::Sybase;

  eval {
    my $emp = new Persistent::Sybase($data_source, $username, $password, $table);
      "job = 'CLERK' and sal > 1000",
      "sal DESC, ename"
    while ($emp->restore_next()) {
      print "Restored: ";  print_person($person);
  croak "Exception caught: $@" if $@;

Restores objects from the data store that meet the specified conditions. The objects are returned one at a time by using the restore_next method and in a sorted order if specified. This method throws Perl execeptions so use it with an eval block.

Since this is a SQL based Persistent class, the restore_where method expects a valid SQL WHERE clause for the first argument, $where, and a valid SQL ORDER BY clause for the optional second argument, $order_by.



Conditional expression for the requested objects. The format of this expression is a SQL WHERE clause without the WHERE keyword. This argument is optional.


Sort expression for the requested objects. The format of this expression is a SQL ORDER BY clause with the ORDER BY keywords. This argument is optional.



The number of objects that match the conditions.

See the Persistent documentation for more information.


For a description of the other methods that this subclass provides, please refer to the Persistent::DBI documentation which is the parent class. Or refer to the Persistent documentation for a very thorough introduction and reference to the Persistent Framework of Classes.


Persistent, Persistent::DBI, Persistent::DataType::DateTime


This software is definitely a work in progress. So if you find any bugs please email them to me with a subject of 'Persistent Bug' at:

And you know, include the regular stuff: OS, Perl, Sybase versions, snippet of code, etc.


  David Winters <>


Copyright (c) 1998-2000 David Winters. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.