----- 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: Mon, 21 Feb 2005 12:39:25 -0800

> 

Finally, a bite - but I'm done a'ready.

> 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
> http://search.cpan.org/author/NWIGER/Config-Fast-1.04/Fast.pm
> http://search.cpan.org/author/AHICOX/Config-Framework-2.5/Framework.pod
> http://search.cpan.org/author/BTROTT/Config-FreeForm-0.01/FreeForm.pm
> http://search.cpan.org/author/OESTERHOL/Config-Frontend-0.18/lib/Config/Frontend.pm
> http://search.cpan.org/author/PTANDLER/Bundle-PBib-2.08/lib/Config/General.pm
> http://search.cpan.org/author/WADG/Config-IniFiles-2.38/IniFiles.pm
> http://search.cpan.org/author/JENDA/Config-IniHash-2.7/IniHash.pm
> http://search.cpan.org/author/DRTECH/Config-Loader-0.01/lib/Config/Loader.pm
> http://search.cpan.org/author/RUSTYP/Config-Magic-0.76/lib/Config/Magic.pm
> http://search.cpan.org/author/SAPER/Config-Natural-0.99/lib/Config/Natural.pm
> http://search.cpan.org/author/ROTH/Config-Objective-0.9.1/lib/Config/Objective.pm
> http://search.cpan.org/author/DANBERR/Config-Record-1.0.5/lib/Config/Record.pod
> http://search.cpan.org/author/SHERZODR/Config-Simple-4.58/Simple.pm
> http://search.cpan.org/author/ADAMK/Config-Tiny-2.00/lib/Config/Tiny.pm
> http://search.cpan.org/author/ATRICKETT/Config-Trivial-0.40/lib/Config/Trivial.pm
> http://search.cpan.org/author/ROODE/Config-Vars-0.01/Vars.pm
> 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.
> 
> > 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.
> 
> > { # From scalar delimited by commas - like what is in the cfg 
> > file used below.
> >   my %Hash = () ;
> >   print "\nUsing variable\n" ;
> >   my $HashInit = '( FileName,^.ssh$,IsDir,1,Desc,Secure Shell directory )' ;
> >   ( $HashInit = $HashInit ) =~ s/^\s*\(\s*(.*)\s*\)\s*$/$1/ ;
> 
> Why are you assigning the contents of $HashInit to $HashInit?  It would be
> simpler to write that as:
> 
> $HashInit =~ s/^\s*\(\s*(.*)\s*\)\s*$/$1/ ;
> 
Good catch, an oversight when making this from first concepts where I had 
something more like:
( $Var = $DifferentVar ) =~ s/^\s*\(\s*(.*)\s*\)\s*$/$1/ ;
> 
> >   my @HashInit = split (/,/,$HashInit ) ;
> >   %Hash = @HashInit ;
> 
> You could skip the array and assign the output from split() directly to %Hash.
>
True, I used it to bring out parallelism between the last three methods.
> 
> >   foreach ( keys %Hash ) {
> >     print "Key = $_\n" ;
> >     print "\$Hash{$_}=" . $Hash{$_} . "\n" ;
> >   } ;
> > }
> >
> > { # From file, delimited by commas, can't have whitespace,
> >   # didn't get "=>" to work.
> >   my %Hash = () ;
> >   print "\nUsing config file\n" ;
> >   my $HashInit = undef ;
> >   open ( CfgFile, "CfgFile.txt" ) ;
> 
> You should *always* verify that the file opened correctly before trying to
> read from the filehandle.
> 
Yes, but for simplicity, I left that sort of thing off with the other 700+ 
lines and tried to beginner-ize by making a small working example.  Perhaps on 
this forum I should more strictify my examples - seems a good policy.  
Especially when I started off so good by using "-w" and "use strict".
> 
> >   chomp ( my @Lines=<CfgFile> ) ;
> >   close CfgFile ;
> >   foreach ( @Lines ) {
> >     # Discard comment or blank lines#.
> >     next if ( /^\s*\#/ ||  /^\s*$/ ) ;
> >     # Concatenate all the contents for this hash together.
> >     $HashInit = $HashInit . $_ ;
> >     # Look for the end of the hash definition-a parenthesis at the end
> >     # of the line.
> >     if ( /\)\s*$/ ) {
> >             # Throw away the parens and any padding whitespace - they
> >             # are just for looks and to find the end of the hash def.
> >             ( $HashInit = $HashInit ) =~ s/^\s*\(\s*(.*)\s*\)\s*$/$1/ ;
> >             # Stuff comma-delimited list in hash.
> >             %Hash = split (/,/,$HashInit ) ;
> >             $HashInit = undef ;
> >     } ;
> >   } ;
> >   foreach ( keys %Hash ) {
> >     print "Key = $_\n" ;
> >     print "\$Hash{$_}=" . $Hash{$_} . "\n" ;
> >   } ;
> > }
> >
> > { # This one doesn't work, of course.
> 
> [snip code]
> 
> > Contents of CfgFile.txt would be like:
> > ( Name,^\.ssh$,
> > IsDir,1,
> > Desc,Secure Shell directory )
> >
> > Contents of a config file I would like to use would be like these examples:
> > # This is the sort of thing I want to use, has commas in 
> > expressions, whitespace, etc.
> > ( Name => '^\.ssh$',
> >     IsDir => 1,
> >     Desc => "Secure Shell directory" )
> >
> > (
> >     SubDir => '^\.ssh$',
> >     Desc => "Secure Shell stuff"
> > )
> >
> > ( Name => '^\.log$',
> >     SubDir => '^log$',
> >     IsDir => 0,
> >     MDays => > 31,
> >     Desc => "Log files in log directories not modified in the last 31 days"
> > )
> >
> > ( Name => '^:0,[0-9]+\.dat$',
> >     SubDir => '^data$',
> >     IsDir => 0,
> >     ADays => > 10,
> >     Desc => "Data files in data directories not accessed in the last 10 
> > days" )
> >
> > Any ideas appreciated.
> 
> You could use the Data::Dumper module to get files like that.
> 
> perldoc Data::Dumper
> 
> 
> 
> John
> -- use Perl;
> program
> fulfillment
> 
> -- To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>

-- 
_______________________________________________
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>


Reply via email to