Ea has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying out Damian Conway's Persistence.pm module (page 409 Object Oriented Perl). I just need the course-grained persistence to store a hash of objects. It stores the data structure fine, but it doesn't load the data the next time the script runs. Does anyone have any experience with this module?
use vars qw( %Courses ); use Persistence "Assignments.dat"; ... END { persistent '%Courses'; }
cheers, (Going back to run some tests I should have done last week)

Update: Ack! solved problem. Solution in the reply. I just needed some time away from the problem, although writing a test case would have focused my thinking.

perl -e 'print qq(Just another Perl Hacker\n)'

Replies are listed 'Best First'.
test case for Persistance.pm
by Ea (Chaplain) on Mar 30, 2005 at 12:35 UTC
    A simple test case repeats what I see in my code:
    #!/usr/local/bin/perl -w # # name: persistest # # does stuff # works fine use vars qw( %hash ); use strict; use Persistence 'test.dat'; my ($k, $v, $n); foreach (($k, $v) = each %hash) { $n++; print "$n: $k => $v\n"; } %hash = ( blue => 'berries', red => 'roses', black => 'coffee', yellow => 'bananas', ); END { persistent '%hash'; }
    produces
    $ ./persistest 1: => 2: =>
    Warnings removed to improve readability. Why it iterates twice when it can't find the values, I can't figure.

    perl -e 'print qq(Just another Perl Hacker\n)' # where's the irony switch?

    janitored by ybiC: Append "for Persistance.pm" to title for better site search results

      Need to use it with a call for the persistant variable to invoke the stored values. So test case works fine with
      #!/usr/local/bin/perl -w # # name: persistest # # does stuff # works fine use vars qw( %hash ); use strict; use Persistence 'test.dat';
      persistent '%hash';
      my ($k, $v, $n);
      while
      (($k, $v) = each %hash) { $n++; print "$n: $k => $v\n"; } %hash = ( blue => 'berries', red => 'roses', black => 'coffee', yellow => 'bananas', ); END { persistent '%hash'; }
      The loop funniness was due to using foreach instead of while over the calls to each %hash.

      Nothing to see here. Move along.

      perl -e 'print qq(Just another Perl Hacker\n)' # where's the irony switch?
        Just a quick observation: generally speaking when answering your own question, the convention is to post an Update: on the original node, so that readers don't need to read the entire thread to find out the problem is solved.

        Update: Just checked your homenode and found out you've been here on the monastery a lot longer than me! Still, I had to read the whole thread before finding out it was solved, so I guess it was worth the reminder.

        Update: OP now posted update on original post.

        g0n, backpropagated monk