#!/usr/bin/perl 
######################################################################
# persistent.pl
######################################################################
# Perl Power! - Michael Schilli 1998
######################################################################

######################################################################
  package PersTest;          # sample class that inherits
                             # persistent properties
######################################################################
use Persistent;

@ISA = qw ( Persistent );    # inherits from "Persistent.pm"

sub new {                    # constructor
    my $type = shift;
    my $self = {};
    bless $self, $type;
}

sub initdata {               # initialize data
    my $self = shift;

    $self->{'the_hash'}   = \%the_hash;
    $self->{'the_array'}  = \@the_array;
    $self->{'the_scalar'} = 
      "This scalar contains a very " .
      "long value with some special chars: " .
      " @,\$,\\,\",'.";
    $the_hash{'hash_key'} = 'hash_value';
    $the_array[1]         = 'array_value';

                             # create new object as part
                             # of the persistent object
    my $objref = PersTest->new();
    $objref->{'myobjvar'} = 'myobjvarval';
    $self->{'the_object'} = $objref;
}

######################################################################
  package main;              # main program
######################################################################

$obj1 = PersTest->new();     # create,
$obj1->initdata();           # initialize, and
$obj1->store("myobj.sav") || # store
    print "Cannot save\n";   # persistent object


$obj2 = PersTest->new();     # create new persistent 
                             # object ... and
$obj2->load("myobj.sav") ||  # initialize it with the
    print "Cannot load\n";   # stored data of obj1

                             # ouput object data
print 
  "\$obj2->{'the_object'}->{'myobjvar'} = ",
  "$obj2->{'the_object'}->{'myobjvar'}\n";
print 
  "\$obj2->{'the_array'}->[1]           = ",
  "$obj2->{'the_array'}->[1]\n";
print 
  "\$obj2->{'the_hash'}->{'hash_key'}   = ",
  "$obj2->{'the_hash'}->{'hash_key'}\n";
print 
  "\$obj2->{'the_scalar'}               = ",
  "$obj2->{'the_scalar'}\n";

