If you just need to save the last state you could use Config::IniFiles to store the state. This gives you a recognizable file format, a portable module and a flexible interface to your data. I have used this method on both Linux and Win2k boxes, without changing my code.

Here is an example that should give you an idea of how this works. I prefer using the tied method because IMHO it is easier to work with an HoH than a bunch of method calls.

use Config::IniFiles; # my $cfg = new Config::IniFiles( -file => "configfile.ini" ); # or my %ini tie %ini, 'Config::IniFiles', ( -file => "configfile.ini" ); # do stuff with %ini # create a new section and add values $ini{new_section} = {}; $ini{new_section}{new_param1} = $val; $ini{new_section}{new_param2} = $val2; # add a multiline value $ini{new_section}{new_param2} = [$value1, $value2, ...]; # save with the TIED hash # overwrite the current ini file tied( %ini )->RewriteConfig(); # or you can write to a different file #tied( %ini )->WriteConfig ($NewFilename); # save with object ref # overwrite the current ini file #$cfg->RewriteConfig(); # or you can write to a different file #$cfg->WriteConfig ($NewFilename);
Even though it is tied to a hash you will still have to call RewriteConfig() or WriteConfig($file) in order to save it.

This is portable and human readable! :)

--
hiseldl


In reply to Re: Maintaining state in modules by hiseldl
in thread Maintaining state in modules by cLive ;-)

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.