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


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?


{ # 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/ ;


  my @HashInit = split (/,/,$HashInit ) ;
  %Hash = @HashInit ;

You could skip the array and assign the output from split() directly to %Hash.


  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.


  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>




Reply via email to