Not sure how else to word this.  Basically, I have a util which reads a config 
file 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" ;
  } ;
}

{ # 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/ ;
  my @HashInit = split (/,/,$HashInit ) ;
  %Hash = @HashInit ;
  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" ) ;
  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.
  # From a more intuitively laid out file - like the book method above.  But 
even using the
  # parens when setting the hash doesn't get it.  It sees the contents of 
$HashInit as a
  # scalar I guess.  Even though it should (in my newbie way of thinking) look 
like
  # the book method at the top.
  my %Hash = () ;
  print "\nUsing wished config file\n" ;
  my $HashInit = undef ;
  open ( CfgFile, "CfgFile.wish.txt" ) ;
  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 the thingie which looks like a list in the hash.
                %Hash = ( $HashInit ) ;
                $HashInit = undef ;
        } ;
  } ;
  foreach ( keys %Hash ) {
        print "Key = $_\n" ;
        print "\$Hash{$_}=" . $Hash{$_} . "\n" ;
  } ;
}

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