Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Working with a legable config file

by phoenix9 (Novice)
on Apr 03, 2004 at 02:12 UTC ( #342212=perlquestion: print w/replies, xml ) Need Help??
phoenix9 has asked for the wisdom of the Perl Monks concerning the following question:

I have a friend who is writing a text based game, but as yet it had no save capabilities. I told him I'd right a save and load sub-routine for him because while im still new to it i know how to use filehandelers alot better than him.

Due to the simplicity of the game all that I need it to due is store variables in a text file from one running of the game until the next. The problem is I wanted the text file to be understandable instead of just a list of numbers so say the file (named test1.usr in a subfolder of the current directory named configs) is like this:

$a = 1 $b = 2

I am trying to figure out a way so that were I to run code like this:

print "enter username:"; chomp($uname=<STDIN>); $USERFILE='configs/'.$uname.'.usr'; open(USERFILE); # Whatever the solution to my problem is print $a + $b;
It would show up as:
enter usename:test1 3
the only thing I can figure out to do is make the .usr file:
1 2
with the perl:
chomp($a = <USERFILE>); chomp($b = <USERFILE>);
the only thing I can do with the current config (.usr) file is print it to STDOUT or another file. How can I keep the current formatting (and therfore legability) but still have the disired outcome.


P.S. this is a test version of the code. In the final it will be keeping track of actual information and will not just print 3. However, I bilieve the solution to this problem will work in the final version.

Replies are listed 'Best First'.
Re: Working with a legable config file
by bbfu (Curate) on Apr 03, 2004 at 02:21 UTC
Re: Working with a legable config file
by Zaxo (Archbishop) on Apr 03, 2004 at 02:26 UTC

    Take a look at do FILENAME.

    For extra redability, have the config file return a hash reference, like this:

    { foo => 1, bar => "frolicsome", }
    Data::Dumper will help with that.

    After Compline,

Re: Working with a legable config file
by bart (Canon) on Apr 03, 2004 at 02:46 UTC
    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:
Re: Working with a legable config file
by Vautrin (Hermit) on Apr 03, 2004 at 04:13 UTC

    Whenever I need my program to read and save settings to a file, I like to use XML::Simple. Basically, you can dump a hashref / array ref or any combination thereof into a file. Then, you can read it back into an array / hash as well. Plus, with all the XML modules / packages out there (and for languages other then Perl) it becomes very easy to interface with your config files.

    For instance, by using the XML out function, you could change:

    my $data = { things => [ { foo => 'bar' }, { foo => 'baz' }, { foo => 'bam' }, ] };

    Into XML like:

    <things> <foo>bar</foo> <foo>baz</foo> <foo>bam</foo> </things>

    And then after calling the function to read XML you can access it right from the hash ref! And, again, I can't stop stressing to you how portable XML is. If, in the future, you want to do something with that data, expand the project, or create a new program to work with the data, it's very simple to do. Of course, using a Perl module for another type of config file will do this for you, but it won't be portable across platforms. (I wish everything were coded in perl, but let's be realistic. Sometimes you need to work with Java/C/ASP/Whatever)

    Want to support the EFF and FSF by buying cool stuff? Click here.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://342212]
Approved by Zaxo
[erix]: shiney! (a trilobite, 400 myo)

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2017-11-21 20:55 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (312 votes). Check out past polls.