procmail
[Top] [All Lists]

Re: Defining a common set of actions ?

1997-05-23 12:41:00
Alan Stebbens asked,

| I've always wondered why some folks seem to prefer using the negated
| tests in front of a null block, followed by an "else" block, rather than
| what seems to be more obvious form of weighted scores:

Personal taste, mostly; in fact, "{ }" as a no-op was more an accident of the
code than an intentional feature (if I understood Stephen correctly when I
told him I'd discovered the negate-null-else method).  Scoring can work just
as well, but Alan's sample has a definite drawback:

|     :0
|     * 2^0 some test
|     * 2^0 some other test
|     * -1^0
|     {
|       # if any test succeeds
|     }

[Of course, if the result of a successful OR is a simple action line, the
 braces are unnecessary, but that's not what I'm getting at.]

The problem with that code is this: if the first test passes, procmail scores
2 and then tries the second test, even though there's no need.  Note that the
negate-null-else syntax doesn't have that problem:

  :0
  * ! some test
  * ! some other test
  { } 
  :0E
  real_action_here

If "some test" passes, then "! some test" fails, and procmail heads straight
for the else recipe without bothering with the other test.

The way to get that functionality with weights is to give a supremum weight
to every condition (or a supremum to each but the last one and some positive
weight to the last one).  For example, if there are three conditions to OR,

  :0
  * 9876543210^0 some test
  * 9876543210^0 some other test
  * 1^0 last opportunity
  action

[2174783647 is a difficult number to remember: in SmartList's rcfiles, Stephen
 uses 9876543210, which is easy to remember and to type and does the job just
 as well as the exact value.]

That way, if "some test" passes, procmail skips the other tests and goes
straight to the action.  If "some test" fails but "some other test" passes,
we're good and procmail goes straight to the action without trying the third
condition.  (Also, as Philip Guenther has pointed out, Alan didn't need to
subtract 1, because a zero score is a failure.)

It would also work to weight the last condition at 9876543210^0; that may
in fact make it easier for you to add or remove conditions over time.

<Prev in Thread] Current Thread [Next in Thread>