fionbarr has asked for the wisdom of the Perl Monks concerning the following question:

hi I have to apply some rules to an incoming email:
if sender = 'abc' or internet domain = 'xyz' or message contains "DEF" + then system = 'LMN'
I have never used this parser..is it appropriate for this situation or overkill? If anyone who thinks it appropriate could suggest what the grammar might look like, it would be greatly appreciated Thanks

Replies are listed 'Best First'.
Re: Parse::RecDescent question
by ELISHEVA (Prior) on Nov 14, 2010 at 15:28 UTC

    The answer to your question very much depends on what sort of information you need to extract from the email. If you are merely extracting and testing standard fields in an email, there are som 130+ CPAN modules at your service. Surely at least one of them will have something you can use. (see http://search.cpan.org/search?m=dist&q=Email&s=1)

    If you are trying to parse the body of the email as well, then the answer would depend on how the body of the mail is formatted. If it is a standard format (.ini, CSV, XML, HTML, YAML, etc), there is most likely a Perl module specifically for that format and you would be reinventing the wheel. If it is non-standard, the choice of parser depends on the grammar - it might be over kill, it might be just right, or it might just not fit the problem. Not every grammar can be parsed efficiently with Parse::RecDescent.

    As for suggesting a grammar? The first step is describing the grammar in plain English and writing out some examples for yourself. Then you can work on converting the rules into the format needed by Parse::RecDescent or any other parsing tool you choose. The time to ask for help is after you've made a first pass on your own. Show us what you tried and we'll be glad to help clean up syntax problems or questions about how to represent some particularly troublesome formatting rule.

    There are some great tips on how to be more exact in raising a question here: How (Not) To Ask A Question Please take the time to read it: We'd like to help you, but we can't read your mind or intuit your text formats and business rules off of a white board in your office.

    Best, beth

      ... 130+ CPAN modules ... at least one of them will have something you can use...

      ... even in the worst possible case. See Yes, even you can use CPAN.

Re: Parse::RecDescent question
by JavaFan (Canon) on Nov 14, 2010 at 15:21 UTC
    Overkill. Wouldn't something like:
    $system = "LMN" if $sender eq 'abc' or $domain eq 'xyz' or $message =~ + /DEF/;
    do? If your real question is "how to get the headers out of an email", use one of the gazillion e?mail related CPAN modules,
Re: Parse::RecDescent question
by ikegami (Patriarch) on Nov 14, 2010 at 19:38 UTC

    It's not only overkill, it's not particularly appropriate. Parsing is to assign meaning to text, but that's not your goal at all. Using PRD to execute mail rules is a bit funky.

    Now, if you wanted to implement your own rule language, PRD could be used to parse the rules into something you can execute. But that's not a trivial job. Is there a reason for not just using Perl?