how to improve this code? 
Author Message
 how to improve this code?


Quote:

>how can I improve this code?

>foreach (split /\n/, $records[$number_of_mails]{"Header"}) {
>    $element = $_;
>    foreach $e (qw(From Subject To Reply-To Cc)) {
>                    $records[$number_of_mails]{$e} = $1 if $element =~ /^$e:\s+(.+)/i;
>    }
>}

A straightforward transformation is:

{ my $rec = $records[$number_of_mails];
  for (split /\n/, $rec->{Header}) {
    foreach $hdr (qw(From Subject To Reply-To Cc)) {
      $rec->{$hdr} = $1 if /^$hdr:\s+(.+)/i;
    }
  }

Quote:
}

I think it would be preferable to do:

{ my $rec = $records[$number_of_mails];
  for (split /\n/, $rec->{"Header"}) {
    $rec->{lc $1} = $2 if /^(From|Subject|To|Reply-To|Cc):\s+(.+)/i;
  }

Quote:
}

I think the /i is probably wrong---mail message headers are now
allowed to have varying capitalization.

In particular, if /i is really desirable, then your original code
probably has a bug: If an email message has 'subject' instead of
'Subject', then you actually use 'subject' as a key; later code that
looks for $rec->{Subject} will not find anything.  But I will leave it
in since you may know something I don't.

I notice that your code does not deal correctly with continuation lines.
An amended version follows.

{ my $rec = $records[$number_of_mails];
  my ($hdr, %interesting);
  BEGIN { %interesting = map {$_ => 1} qw(from subject to reply-to cc)) }
  for (split /\n/, $rec->{Header}) {
    if (s/^\s+//) {
      $rec->{$hdr} .= $_ if defined $hdr;
    } else {
      ($hdr, my $val) = /^([^:]):\s+(.*)/;
      undef($hdr), next unless $interesting{lc $hdr};
      $rec->{$hdr} = $val;
    }
  }

Quote:
}

rd
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


Sat, 27 Sep 2003 20:55:26 GMT  
 how to improve this code?

Quote:

> I think the /i is probably wrong---mail message headers are now
> allowed to have varying capitalization.

"now"?

I've noticed that there is no universal consistency
in at least one field: Message-Id.  Truly about half
of all messages have it as Message-ID.  What a pain.

--
John Porter



Sat, 27 Sep 2003 22:44:59 GMT  
 how to improve this code?

Quote:


> > I think the /i is probably wrong---mail message headers are now
> > allowed to have varying capitalization.

> "now"?

> I've noticed that there is no universal consistency
> in at least one field: Message-Id.  Truly about half
> of all messages have it as Message-ID.  What a pain.

Well RFC822 actually states that field names are not case sensitive

  3.4.7.  CASE INDEPENDENCE

        Except as noted, alphabetic strings may be represented in  any
        combination of upper and lower case.  The only syntactic units
        which requires preservation of case information are:

                    -  text
                    -  qtext
                    -  dtext
                    -  ctext
                    -  quoted-pair
                    -  local-part, except "Postmaster"

        When matching any other syntactic unit, case is to be ignored.
        For  example, the field-names "From", "FROM", "from", and even
        "FroM" are semantically equal and should all be treated ident-
        ically.

Graham.



Sat, 27 Sep 2003 23:04:02 GMT  
 how to improve this code?

Quote:



> >how can I improve this code?

> >foreach (split /\n/, $records[$number_of_mails]{"Header"}) {
> { my $rec = $records[$number_of_mails];
>   for (split /\n/, $rec->{"Header"}) {

The 'split' is wrong. RFC 822 headers are allowed to have continuation
lines, and it's a rare email message that doesn't use this (in at least
the Received:).
To: and Cc: lines very frequently have continuations, too.

split /^\b/ does the trick (but leaves the trailing \n in the value of
the header field).

Quote:
>       $rec->{$hdr} = $val;

this is also wrong. You're allowed to have multiple "To" headers, for
instance. Your program will lose all but the last one.

Ian



Sun, 28 Sep 2003 17:08:20 GMT  
 how to improve this code?

Quote:



> > >how can I improve this code?

> > >foreach (split /\n/, $records[$number_of_mails]{"Header"}) {

> > { my $rec = $records[$number_of_mails];
> >   for (split /\n/, $rec->{"Header"}) {

> The 'split' is wrong. RFC 822 headers are allowed to have continuation
> lines,

That same message said:

Quote:
>> I notice that your code does not deal correctly with continuation lines.
>> An amended version follows.

Don't you read the articles you are replying to?

Quote:
> this is also wrong.

It depends on the application.


Sun, 28 Sep 2003 21:11:56 GMT  
 how to improve this code?

Quote:


> > I think the /i is probably wrong---mail message headers are now
> > allowed to have varying capitalization.

> Which means you SHOULD use the case B<i>nsensitve modifier, yes?

'now' was a typo.  I meant to type "message headers are NOT allowed...".

As others have pointed out, I was mistaken in this, but the typo seems
to have caused enough confusion that I should explain what I meant.



Mon, 29 Sep 2003 08:01:09 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. how to improve this code?

2. novice question: improving this code?

3. How can i improve this code ?

4. how can i improve this code ?

5. Advantage tdataset descendant release 2.7

6. Text::Wrap how to use it? Improve this code?

7. Improving file record terminator code

8. Improving code

9. Improve code with RegEx

10. help on improving code (pine2mutt-script ca 130 lines)

11. Suggestion wanted on improving code.

12. ??Delphi 2 ODBC

 

 
Powered by phpBB® Forum Software