Feeds:
Posts
Comments

Posts Tagged ‘storable’

One thing I like about developing the emacs environment (i.e. writing emacs lisp) is programming in the image. In contrast when I run a perl script, any data loaded into the script vanishes when the script exits.

my $image = {};
populate_particular_set_of_people($image);
populate_particular_set_of_orders($image);
# ... more populate functions ...
produce_report($image);

If the populate functions take a long time to run and there is a syntax error in one of them, I might waste a lot of time waiting for data to be loaded over and over and potentially annoy my DBA.

Hence my thinking about REPLs earlier in the year.

Instead, I can mitigate this problem and support exploratory programming by serialising my data structure to disk after each section has been loaded.

use Storable;

use constant IMAGE_FILE => '/tmp/.jareds-image-RANDOMTEXT';

sub populate_particular_set_of_people
{
    my $image = shift;

    return if (exists $image->{'store'}{'people'});

    # Get the set of people

    store $image->{'store'}, IMAGE_FILE;
}

sub populate_particular_set_of_orders
{
    my $image = shift;

    return if (exists $image->{'store'}{'orders'});

    # Get the set of orders

    store $image->{'store'}, IMAGE_FILE;
}

my $image = {};
$image = retrieve(IMAGE_FILE) if -f IMAGE_FILE;

populate_particular_set_of_people($image);
populate_particular_set_of_orders($image);
# more populate functions
produce_report($image);

Error handling has been elided.

I’m only storing part of the hashref, $image->{'store'} in this example. This is fairly typical of my standard use case for this technique – quick one off reports. Some of the data is slow to load in from the database so I persist it to disk. The rest is calculated based on the loaded data so I don’t persist it. I keep all of the data together so I can pass it to each subroutine as a single parameter.

Of course, I wouldn’t recommend this technique for a production system.

Read Full Post »

Follow

Get every new post delivered to your Inbox.