[Mimedefang] Testing for the existence of a header

David F. Skoll dfs at roaringpenguin.com
Tue Sep 30 22:49:44 EDT 2008

Joseph Brennan wrote:

[code that slurps headers into a hash]

> if (/^(\S+): (.*)/)

I'm not sure a single space is required after the header name.
I would write this as:

  if (/^(\S+):\s*(.*)/)

The greediness of "*" means that \s* will eat up all whitespace
before any (possible) non-whitespace value.

> Taking your example, then the contents of a header "X-ABC:" would be
> stored in $Header{'x-abc'}.  You could test both existence:

>     if ($Header{'x-abc'})

I would write:  if (exists($Header{'x-abc'}))

It is possible, though maybe improbable, to have something like:

   X-ABC: 0

and Perl treats that as false.

> and contents:

>     if ($Header{'x-abc'} =~ /some string/)

If you are using "use strict; use warnings;" this might elicit a
warning.  I'd write it as:

      if (defined($Header{'x-abc'}) && ($Header{'x-abc'} =~ /some string/))

(Advanced Perl hackers may choose to avoid auto-vivification with an
extra exists() test first...)



More information about the MIMEDefang mailing list