Perhaps you could be using a hash instead of a set of global variables, to hold all the data. Hashes require far less code to store and retrieve, than individual scalars, which you'd have to enumerate — heck, Storable can do save and load in one single line of code, each. On the other hand, the saved file wouldn't quite be "legible".

OTOH, hash items are almost as easy to handle than individual variables are. For example, if your hash is %v, then $v{'a'} (or even $v{a}) does the same as your $a would have done.

In that perspective, virtually any Config::* module might do. Or YAML. Even a simple CSV or tab delimited textfile would work.

The next code saves and loads a hash to and from a tab delimited text file — provided the data contains neither a tab or a newline:

sub save { my($file, $data) = @_; local *FH; open FH, ">$file" or die "Can't open file $file: $!"; local($\, $,) = ("\n", "\t"); foreach my $key (sort keys %$data){ print FH $key, $data->{$key}; } } sub load { my($file) = @_; local *FH; my %hash; open FH, "<$file" or die "Can't open file $file: $!"; local $/ = "\n"; while(<FH>) { chomp; my($key, $value) = split /\t/; $hash{$key} = $value; } return wantarray ? %hash : \%hash; }
Example use:
my %one = ( a => 'alpha', z => 'zeta', b => 'beta'); save("data.txt", \%one); my $two = load("data.txt"); # scalar -> hash ref my %three = load("data.txt"); # list -> hash # Dump: shows everything we got: use Data::Dumper; print Dumper \%one, $two, \%three; # Use individual items, as in a normal program: printf "a stands for %s\n", $one{a}; printf "b stands for %s\n", $two->{b}; printf "z stands for %s\n", $three{z};
Check the contents of the file "data.txt" to see how it's saved.

In reply to Re: Working with a legable config file by bart
in thread Working with a legable config file by phoenix9

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.