NAME

TIe::File::Hashify - Parse a file and tie the result to a hash.

SYNOPSIS

use Tie::File::Hashify;

my %rc;
my $path = "$ENV{HOME}/.some.rc";

# Parse lines like 'foo = bar':
sub parse { $_[0] =~ /^\s*(\S+)\s*=\s*(.*?)\s*$/ };

# Format pairs as 'key = value':
sub format { "$_[0] = $_[1]" };

tie(%rc, 'Tie::File::Hashify', $path, \&parse, \&format);

print "option 'foo' = $rc{foo}\n";

# Add new option.
$rc{bar} = 'moo';

# Save file.
untie %rc;

DESCRIPTION

This module helps parsing simple text files and mapping it's contents to a plain hash. It reads a file line by line and calls a callback you provide to parse a key and a value from the file. The key/value pairs are then available through the generated hash. You can also provide another callback that formats a key/value pair to a line to be stored back to the file.

METHODS

tie(%hash, $path, \&parse, \&format)

The first argument is the hash you want to tie to the file. The second one is the path to a file. The file does not really have to exist, but using a path to a non-existent file does only make sense if you provide a format-callback to write a new file. The third argument is the callback function that is called for every line of the parsed file. It gets the currently read line as argument and should return two scalars (key and value). If the first return value (the key) is undef or empty (''), nothing is added to the hash. The fourth argument is another callback. If it's provided, it will be called for every key/value pair in the hash when the tied hash is untied. It gets the key and the value of the current pair as arguments and should return a string, which then will be saved to the file.

The parse callback and the format callback may be omitted. If you omit both, you get a plain hash that does nothing special.

TODO

  • Allow regex instead of parse callback for parsing.

  • Allow format instead of format callback for formatting.

COPYRIGHT

Copyright (C) 2008 by Jonas Kramer <jkramer@cpan.org>. Published under the terms of the Artistic License 2.0.