----- Original Message ----- From: "John W. Krahn" <[EMAIL PROTECTED]> To: "Perl Beginners" <beginners@perl.org> Subject: Re: Setting a Hash Using the Contents of a File Date: Tue, 22 Feb 2005 14:04:35 -0800
> > Jon Mathews wrote: > > From: "John W. Krahn" <[EMAIL PROTECTED]> > >> Jon Mathews wrote: > >> > >>> Not sure how else to word this. Basically, I have a util which reads a > >>> config file > >> > >> That is one "wheel" that has been invented many times before: > >> > >> http://search.cpan.org/author/MWS/CONFIG-V0.99.11/Hash.pm > >> http://search.cpan.org/author/AVAJADI/Config-Abstract-0.12/Abstract.pm > >> http://search.cpan.org/author/KANE/Config-Auto-0.11/lib/Config/Auto.pm > >> http://search.cpan.org/author/JONBJ/Config-Easy-0.2/lib/Config/Easy.pm > >> > >> [snip] > >> > >> etc. > >> etc... > > > > Thanks - some of these are close to drop-in solutions which I will > > consider in the future where applicable (like code that I actually > > run). However, they add complexity to the config file which will be > > created/maintained by lots of rather low-level users. I would rather > > they not have to know names of variables, keys, formats, etc. any more > > than absolutely necessary. They seem to like the last working method > > okay - and it is working out better than I expected. > > > > Since I actually need to read multiple hashes from the config file, I > > saw no example which was an amalgam of having the minimal amount of the > > structure in the config file and the rest in the code - to keep the > > config file simple. > > The main problem is that "low-level users" are not Perl programmers but your > config file format relies on a Perl friendly syntax instead of a more user > friendly syntax. What happens if your users put commas or parentheses in the > data? > True, but not so Perl friendly and knowledgeable as to specify everything about the data structure as in most of the examples. This format is somewhat rigid - embedded commas, trailing parens, even extraneous whitespace are not permitted in the data. The other (old) config file format is yet more rigid but can handle everything I have encountered to date, so we are covered. This stanza sort-of format is much easier to work with - and these users aren't that lame - they will read the documentation which tells what is legal - or they know I will give them a boo-boo. So this is sort of a compromise, I guess, but one which will work in the vast majority of our cases. I didn't have to spend much time making it yet more user friendly and we are already rolling. The thing to do is spend some more time to do it right. All I got is square-toits for now, with sharp edges. But if the only additional mod to my desired format is a very simple index (looks like that in some of the examples you sent), maybe I will get to it before too long. > > >>> to set metadata which is kept finally in a HOH. I want to > >>> support another form of the config file, though, which may be less > >>> flexible, but is easier to read. Here is some example code which shows > >>> my progression of thinking in 4 sections - the last of which is the one > >>> that shows what I want to do: > >>> > >>> #!/usr/bin/perl -w > >>> use strict ; > >>> { # Out of the book method. > >>> my %Hash = () ; > >>> print "Using values\n" ; > >>> %Hash = ( FileName => '^.ssh$', IsDir => 1, > >>> Desc => "Secure Shell directory" ) ; > >>> foreach ( keys %Hash ) { > >>> print "Key = $_\n" ; > >>> print "\$Hash{$_}=" . $Hash{$_} . "\n" ; > >>> } ; > >>> } > >> > >> Just curious, which book did you get this method from? > > > > Advanced Perl Programming - O'Reilly - first edition - p. 10. > > Page 10 is the section on "References to Anonymous Storage" but > there is no anonymous storage in your example? True, the lines I was looking at are prefaced by "Contrast this to the way you'd normally created a named hash:" > > The reasons I asked is because one would normally combine the > declaration and assignment for the hash in one statement and you > are using interpolation for the $_ scalar but not for the $Hash{$_} > scalar. Yes, whenever possible I like the declaration and assignment together, that would be a good idea here. But I am in the habit of putting declarations for a scope together at the top for readability. So, if assignment has to happen later, . . . I will put realistic code in my next post. That way, no excuses. > > { # Out of the book method. > print "Using values\n" ; > my %Hash = ( FileName => '^.ssh$', IsDir => 1, > Desc => "Secure Shell directory" ) ; > foreach ( keys %Hash ) { > print "Key = $_\n" ; > print "\$Hash{$_}=$Hash{$_}\n" ; > } > } > > > > John > -- use Perl; > program > fulfillment > Thanks for your help. -- _______________________________________________ Find what you are looking for with the Lycos Yellow Pages http://r.lycos.com/r/yp_emailfooter/http://yellowpages.lycos.com/default.asp?SRC=lycos10 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>