David Winters > Persistent-Base-0.52 > Persistent::Memory

Download:
Persistent-Base-0.52.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Source  

NAME ^

Persistent::Memory - A Persistent Class implemented using Memory (RAM)

SYNOPSIS ^

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

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

    ### allocate a persistent object ###
    my $person = new Persistent::Memory();

    ### define attributes of the object ###
    $person->add_attribute('firstname', 'ID', 'VarChar', undef, 10);
    $person->add_attribute('lastname',  'ID', 'VarChar', undef, 20);
    $person->add_attribute('telnum', 'Persistent',
                           'VarChar', undef, 15);
    $person->add_attribute('bday', 'Persistent', 'DateTime', undef);
    $person->add_attribute('age', 'Transient', 'Number', undef, 2);

    ### query the datastore for some objects ###
    $person->restore_where(qq{
                              lastname = 'Flintstone' and
                              telnum =~ /^[(]?650/
                             });
    while ($person->restore_next()) {
      printf "name = %s, tel# = %s\n",
             $person->firstname . ' ' . $person->lastname,
             $person->telnum;
    }
  };

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

ABSTRACT ^

This is a Persistent class that uses memory (RAM) to store and retrieve objects. This can be useful when you want to create a cache of objects (i.e. a server-side web cache). 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 base package which is available from:

  http://www.bigsnow.org/persistent
  ftp://ftp.bigsnow.org/pub/persistent

DESCRIPTION ^

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.

METHODS ^

new -- Object Constructor

  use Persistent::Memory;

  eval {
    my $person = new Persistent::Memory($field_delimiter);
  };
  croak "Exception caught: $@" if $@;

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

Parameters:

These are the same as for the datastore method below.

datastore -- Sets/Returns the Data Store Parameters

  eval {
    ### set the data store ###
    $person->datastore($field_delimiter);

    ### get the data store ###
    $href = $person->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.

Parameters:

$field_delimiter

Delimiter used to separate the attributes of the object in the data store. This argument is optional and will be initialized to the value of the special Perl variable $; (or $SUBSCRIPT_SEPARATOR if you are using the English module) as a default.

Returns:

$href

Reference to the hash used as the data store.

insert -- Insert an Object into the Data Store

  eval {
    $person->insert();
  };
  croak "Exception caught: $@" if $@;

Inserts an object into the data store. This method throws Perl execeptions so use it with an eval block.

Parameters:

None.

Returns:

None.

See the Persistent documentation for more information.

delete -- Delete an Object from the Data Store

  eval {
    $person->delete();
  };
  croak "Exception caught: $@" if $@;

Deletes an object from the data store. This method throws Perl execeptions so use it with an eval block.

Parameters:

@id

Values of the Identity attributes of the object. This argument is optional and will default to the Identifier values of the object as the default.

Returns:

$flag

A true value if the object previously existed in the data store (it was deleted), and a false value if not (nothing to delete).

See the Persistent documentation for more information.

restore -- Restore an Object from the Data Store

  eval {
    $person->restore(@id);
  };
  croak "Exception caught: $@" if $@;

Restores an object from the data store. This method throws Perl execeptions so use it with an eval block.

Parameters:

@id

Values of the Identity attributes of the object. This method throws Perl execeptions so use it with an eval block.

Returns:

$flag

A true value if the object previously existed in the data store (it was restored), and a false value if not (nothing to restore).

See the Persistent documentation for more information.

restore_where -- Conditionally Restoring Objects

  use Persistent::Memory;

  eval {
    my $person = new Persistent::Memory('|');
    $person->restore_where(
      "lastname = 'Flintstone' and telnum =~ /^[(]?650/",
      "lastname, firstname, telnum DESC"
    );
    while ($person->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 Perl based Persistent class, the restore_where method expects the $where argument to use Perl expressions.

Parameters:

$where

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

$order_by

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

Returns:

$num_of_objs

The number of objects that match the conditions.

See the Persistent documentation for more information.

SEE ALSO ^

Persistent, Persistent::Base, Persistent::DBM, Persistent::Memory

NOTES ^

This Persistent class does not lock the data store (a hash in this case) before reading or writing it. This should not be a problem unless you are using threaded Perl or forking processes that share an object of this class.

BUGS ^

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:

  winters@bigsnow.org

And you know, include the regular stuff, OS, Perl version, snippet of code, etc.

AUTHORS ^

  David Winters <winters@bigsnow.org>

COPYRIGHT ^

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.

syntax highlighting: