0

I've written a script that needs to store a small string in-between runs. Which CPAN module could I use to make the process as simple as possible? Ideally I'd want something like:

use That::Module;
my $static_data = read_config( 'script-name' ); # read from e.g. ~/.script-name.data
$static_data++;
write_config( 'script-name', $static_data ); # write to e.g. ~/.script-name.data

I don't need any parsing of the file, just storage. There's a lot of different OSes and places to store these files in out there, which is why I don't want to do that part myself.

Andreas
  • 7,470
  • 10
  • 51
  • 73

4 Answers4

1

Just use Storable for portable persistence of Perl data structures and File::HomeDir for portable "general config place" finding:

use File::HomeDir;
use FindBin qw($Script);
use Storable qw(nstore);

# Generate absolute path like:
# /home/stas/.local/share/script.pl.data
my $file = File::Spec->catfile(File::HomeDir->my_data(), "$Script.data");

# Network order for better endianess compatibility
nstore \%table, $file;
$hashref = retrieve($file);
creaktive
  • 5,193
  • 2
  • 18
  • 32
  • There's a lot of different OSes and places to store these files in out there, which is why I don't want to do that part myself. – Andreas Dec 28 '12 at 13:44
  • Sorry, I haven't noticed that you also need a portable way for determining the storage place. Edited my answer to suit better your need! – creaktive Dec 28 '12 at 14:08
0

If it's just a single string (eg, 'abcd1234'), just use a normal file and write to it with open.

If you're looking for something a bit more advanced, take a look at Config::Simple or JSON::XS. Conifg::Simple has its own function to write out to a file, and JSON can just use a plain open.

titanofold
  • 2,852
  • 1
  • 15
  • 21
  • There's a lot of different OSes and places to store these files in out there, which is why I don't want to do that part myself. – Andreas Dec 28 '12 at 13:16
0

May be this can help you - http://www.stonehenge.com/merlyn/UnixReview/col53.html. But I think you cannot avoid using work with files and directories.

Kostia Shiian
  • 1,024
  • 7
  • 12
0

The easiest way that I know how to do this (rather than rolling by hand) is to use DBM::Deep.

Every time I post about this module I get hate posts responding that its too slow, so please don't do that.

Joel Berger
  • 20,180
  • 5
  • 49
  • 104