CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Perl reading config file question

 



wjbell
stranger

Sep 19, 2003, 3:31 PM

Post #1 of 8 (2207 views)
Perl reading config file question Can't Post

I have a perl mail filter that uses a config file. The config file uses line-by-line markers to identify the items. Example:

a: foo@bar.com
a: foo1@bar.com

s: make money
s: get rich

The script gets the whole config file into an array and proccesses it line by line. Anything with an 'a:' gets treated like an address that is always accepted. Anything with a 's:' gets treated as a subject of a message that will get deleted.

What I want to do is change the config file to read like this:

[accept]
foo@bar.com
foo1@bar.com

[subject]
make money
get rich

So that anything under the [marker] gets treated accordingly.

Can anyone help me out with this?

TIA


KevinR
Veteran


Sep 20, 2003, 2:51 AM

Post #2 of 8 (2201 views)
Re: [wjbell] Perl reading config file question [In reply to] Can't Post

gets rid of everything after [subject]


Code
  #!/perl/bin/perl.exe  

use strict;
use TIE::FILE;

my $filename = 'path/to/your/file';
my @NEWDATA;

tie my @DATA, 'Tie::File', $filename || die "can't open $filename: $!\n";
foreach (@DATA) {
next if (/^$/);#ignore blank lines
push @NEWDATA, $_ if (/\[accept\]/ .. /\[subject\]/);
last if (/\[subject\]/);#jump out of loop
}
pop @NEWDATA;
@DATA = @NEWDATA;
untie(@DATA);



so this:

[accept]
foo@bar.com
foo1@bar.com

[subject]
make money
get rich

becomes this:
[accept]
foo@bar.com
foo1@bar.com
-------------------------------------------------


(This post was edited by KevinR on Sep 20, 2003, 3:10 AM)


davorg
Thaumaturge / Moderator

Sep 20, 2003, 5:01 AM

Post #3 of 8 (2196 views)
Re: [wjbell] Perl reading config file question [In reply to] Can't Post

To be much help we'd need to know what data structure gets build out of the data file in order for it to be used by the rest of the program.

This solution is just a guess that builds two arrays called @accept and @subject.


Code
my ($section, @accept, @subject); 

# hash mapping valid sections to the correct array
my %sections = (accept => \@accept, subject => \@subject);

while (<CONFIG>) { # assume config file already open
chomp;
next unless /\S/; # ignore blank lines

if (^\[(\w+)]) {
$section = $1;
die "Unknown section $section\n" unless $sections{$section};
next;
}

push @{$sections{$section}}, $_;
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


wjbell
stranger

Sep 20, 2003, 7:11 AM

Post #4 of 8 (2193 views)
Re: [davorg] Perl reading config file question [In reply to] Can't Post

After the @accept and @subject arrays have the data in them, then those arrays are seperately opened and compared to the From address or Subject of the actual incomming mail message.

But I don't quite understand your code. Will this take a file like this:

[accept]
foo@foo.com
bar@bar.com

[subject]
make money
security update

and build an @accept array that contains the two email address below it and a @subject array that contains the two sentances below it?

I also want to be able to add more than two entries per array (obviously :)) and add more [marker] fields later.

Thanks again for your help.

In Reply To
To be much help we'd need to know what data structure gets build out of the data file in order for it to be used by the rest of the program.

This solution is just a guess that builds two arrays called @accept and @subject.


Code
my ($section, @accept, @subject); 

# hash mapping valid sections to the correct array
my %sections = (accept => \@accept, subject => \@subject);

while (<CONFIG>) { # assume config file already open
chomp;
next unless /\S/; # ignore blank lines

if (^\[(\w+)]) {
$section = $1;
die "Unknown section $section\n" unless $sections{$section};
next;
}

push @{$sections{$section}}, $_;
}




davorg
Thaumaturge / Moderator

Sep 20, 2003, 9:57 AM

Post #5 of 8 (2191 views)
Re: [wjbell] Perl reading config file question [In reply to] Can't Post


In Reply To
After the @accept and @subject arrays have the data in them, then those arrays are seperately opened and compared to the From address or Subject of the actual incomming mail message.


OK. That's pretty much what I expected.


In Reply To
But I don't quite understand your code. Will this take a file like this:

[accept]
foo@foo.com
bar@bar.com

[subject]
make money
security update

and build an @accept array that contains the two email address below it and a @subject array that contains the two sentances below it?


Did you try it? What happened? Isn't that what I said it was going to do :)

Actually, there's a typo in it. The line that says

Code
if (^\[(\w+)]) {

should be

Code
if (/^\[(\w+)]/) {



In Reply To
I also want to be able to add more than two entries per array (obviously :))


I guessed that. The code will handle that just fine.


In Reply To
and add more [marker] fields later.


You'll need to add another array to the "my" line that defines all of the arrays. You'll also need to add an entry to the %sections hash. I aimed to make it as easy to extend as possible.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


wjbell
stranger

Sep 20, 2003, 1:39 PM

Post #6 of 8 (2190 views)
Re: [davorg] Perl reading config file question [In reply to] Can't Post

Ok, I'll try that.

But shouldn't this:

if (/^\[(\w+)]/) {

Be this?

if (/^\[(\w+)\]/) {

:)


KevinR
Veteran


Sep 20, 2003, 3:51 PM

Post #7 of 8 (2186 views)
Re: [wjbell] Perl reading config file question [In reply to] Can't Post

did you try it? Wink

if (/^\[(\w+)]/)


I think it should be ok if only the first square bracket is escaped but escaping the second one will not hurt anything.
-------------------------------------------------


davorg
Thaumaturge / Moderator

Sep 21, 2003, 1:20 AM

Post #8 of 8 (2181 views)
Re: [wjbell] Perl reading config file question [In reply to] Can't Post


In Reply To
Ok, I'll try that.

But shouldn't this:

if (/^\[(\w+)]/) {

Be this?

if (/^\[(\w+)\]/) {

:)


No. A ] character is only a metacharacter _within_ a character class. By escaping the [ character we've ensured that it isn't in a character class.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives