[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Parsing rfc822 headers



Christian Borup wrote:
> I just came up with this sub that parses a rfc822 header and stuffs it
> into a array of hashes (yes - it works).
> 
> sub headers {
>   return [ map { my $q= {}; @{$q}{'key','value'}= split(/:\s+/, $_, 2);
> $q } split(/\n(?!\s)/, $_[0]) ];
> }

I don't get it. Why return a list of hashes? Don't you want
just a hash with the header names as keys and the (possible
multiple) values as list refs?

sub headers {
  my $h = {};
  foreach (split(/\n(?!\s)/, $_[0]) {
    m/^ ( [^:\s]+ ) : \s* (.*) /mx 
      or die "malformed header: `$_'";
    # if you want, you can adjust the possibly multi-line values here
    # my ($v = $2) =~ s/\n\s+/ /g;
    push @{$h{$1}}, $2;
  }
  return $h;
}

Roland
--
perl -e '@_=@ARGV;$_="@_[-3,-2,-1]";s{([@{[$_[-1]]}])}#($0=$1.
${[qw[>$%, ()%{&", &"=, |}+&" *]]}[$@++])=~y/*=->$%&"(-){-}+,/
lusternohack /,$0#eg; print; '        -- Roland Giersig, JAPH.

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org