in reply to reading & searching file
Hi
First up, you might find it quicker to use a standard Config file format, like Config::IniFiles, XML::Config or maybe YAML. Then you'll save yourself having to write your own config reader.
You don't say anything about what you've tried already, or what you're stuck on. You don't say much about what, generally you're trying to achieve. Trusting that this isn't homework, and taking it step by step, first you need to parse your main config file. Try reading it line by line, and noting when you find a new "block" starting:
open(CONF, '<conf-main.txt') or die "Can't open file: $!"; my ( %config, $setting ); while ( my $line = <CONF> ) { chomp $line; # skip blank lines next if $line =~ /^\s*$/; # find named sections if ( $line =~ /^\[(\w+)\]/ ) { $setting = $1; next; } # save values if ( exists $config{$setting} ) { # squash into an array if already set $config{$setting} = ref($config{$setting}) ? [ @{ $config{$setting} }, $line ] : [ $config{$setting}, $line ]; } else { $config{$setting} = $line; } }
Data::Dumper is very useful for checking that you've got what you expected in a variable after something like this:
use Data::Dumper; print Data::Dumper::Dumper \%config;
Then you need to check each of the script files to see if it's got the 'update' action. Again, you want to specify what you're looking for, and then reading each file line-by-line to see if it contains it.
my $type_match = qr/#DO\s+$config{'type'}/i; foreach my $script_file ( @{ $config{'scripts'} } ) { open(SCRIPT_FILE, $script_file) or die "Can't open script file '$_': $!"; while ( my $line = <SCRIPT_FILE> ) { if ( $line =~ $type_match ) { print "File '$script_file' is the right type"; break; } } }
Hope this helps
ViceRaid
|
|---|