|
In a message dated 10/24/2005 1:53:59 P.M. Eastern Standard Time,
[EMAIL PROTECTED] writes:
> Hi,
> if we want to send any emails using qmail, we use /var/qmail/bin/qmail-inject. Now, when I receive > any emails to one mailbox, just saying "test" for the username. In those emails contain some data > in the body. The contents in the body is like this > > usename: test > password: 1234 > name: 1234 > order: purchase > flight number: SQ1234 > depart: chicago, IL > destination: singapore > price: USD 1400 > > Is it possible I parse those emails and process it? eko --
i am not sure exactly what you want, and my guess is that there is probably
some Parse::What::Ever module around that would work just as well or
better.
however, if you want to use something written from scratch, try this
code. it is written to be flexible and also to be fairly
bullet-proof, perhaps more so than you want or need. it has had
limited testing.
hth -- bill walters ======= code ==========
use warnings;
use strict; # define record extraction patterns my $k_element = qr/[a-zA-Z\d]/; # alphanumeric element of key
field
my $v_element = qr/[,a-zA-Z\d]/; # alnum or comma element of value field my $key_re = qr/$k_element+ (?: \s+ $k_element+)*/x; #
key field pattern
my $value_re = qr/$v_element+ (?: \s+ $v_element+)*/x; # value field pattern # key, value regexes allow embedded whitespace, but no leading or trailing
ws.
my $separator = qr/\s*:\s*/; # field separator pattern
my $undefined_value = 'UNDEFINED'; # could just be undef # define acceptable record key fields, with undefined values by
default.
# note that 'flight number' has embedded space. my %Records = # will hold extracted records map { $_ => $undefined_value } 'flight number', qw(usename password name order depart destination price); # get file name, open file defined (my $filename = shift) or die "no file name given";
open FH, '<', $filename or die "opening $filename: $!";
# process records in file
while(defined (my $line = <FH>)) {
# validate data: does each line match acceptable record
pattern?
# if line is valid record, capture key ($1) and value ($2) fields. $line =~ /^\s* ($key_re) $separator ($value_re) \s*$/x or die "bad record: \n ``$line'' "; my ($key, $value) = ($1, $2); # key and value
fields
# validate data: is this key field
acceptable?
exists $Records{$key} or die "unknown key ``$key'' "; # validate data: was this key already assigned a
value?
$Records{$key} eq $undefined_value or die "already assigned key ``$key'' "; $Records{$key} = $value;
}
$! and die "reading $filename: $!"; # a read error in loop?
close FH or die "closing $filename: $!"; # file not closed if read
error
# data validation: was any key NOT assigned? $Records{$_} ne $undefined_value or die "unassigned key ``$_''
"
for keys %Records; print "<$_> <$Records{$_}> \n" for sort keys %Records; # FOR DEBUG # e.g., " foo baz : bar 1234 " record
prints "<foo baz> <bar 1234>"
# %Records hash now holds successfully extracted records ======== end code =========
|
_______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
