File Coverage

blib/lib/Text/Header.pm
Criterion Covered Total %
statement 9 31 29.0
branch 0 2 0.0
condition 0 3 0.0
subroutine 3 5 60.0
pod 0 2 0.0
total 12 43 27.9


line stmt bran cond sub pod time code
1              
2             # $Id: Header.pm,v 1.3 2000/10/02 17:43:20 nwiger Exp $
3             ####################################################################
4             #
5             # Copyright (c) 2000 Nathan Wiger
6             #
7             # This simple module provides two functions, header and unheader,
8             # which do lightweight, general-purpose RFC 822 header parsing.
9             #
10             # This module is intended mainly as a proof-of-concept for the Perl
11             # 6 proposal located at: http://dev.perl.org/rfc/3__.html
12             #
13             ####################################################################
14             #
15             # This program is free software; you can redistribute it and/or
16             # modify it under the terms of the GNU General Public License
17             # as published by the Free Software Foundation; either version 2
18             # of the License, or (at your option) any later version.
19             #
20             # This program is distributed in the hope that it will be useful,
21             # but WITHOUT ANY WARRANTY; without even the implied warranty of
22             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23             # GNU General Public License for more details.
24             #
25             # You should have received a copy of the GNU General Public License
26             # along with this program; if not, write to the Free Software
27             # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28             # 02111-1307, USA.
29             #
30             ####################################################################
31              
32             package Text::Header;
33             require 5.004;
34              
35 1     1   676 use strict;
  1         1  
  1         31  
36 1     1   5 use vars qw(@EXPORT @ISA $VERSION);
  1         1  
  1         158  
37             $VERSION = do { my @r=(q$Revision: 1.3 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r };
38              
39 1     1   6 use Exporter;
  1         4  
  1         485  
40             @ISA = qw(Exporter);
41             @EXPORT = qw(header unheader);
42              
43             sub header {
44 0     0 0   my @ret;
45 0           my @args = @_;
46              
47             # go through each tag pair, reformatting the tag
48             # and pushing it onto an array
49 0   0       while (my $tag = shift @args and my $val = shift @args) {
50 0           chomp($tag = ucfirst lc $tag);
51 0           $tag =~ s/[-_](\w)/-\u$1/g;
52 0 0         if ( ref $val ) {
53 0           $val = join ', ', @$val;
54             }
55 0           chomp $val;
56 0           push @ret, "$tag: $val\n";
57             }
58 0           return @ret;
59             }
60              
61             sub unheader {
62 0     0 0   my @ret;
63 0           chomp(my @lines = @_);
64 0           my $i = 0;
65 0           while (my $line = $lines[$i]) {
66              
67             # join multiple indented lines per RFC 822
68 0           $line .= $lines[$i] while ($lines[++$i] =~ /^\s+/);
69              
70             # split the two and change the tag to lowercase
71 0           my($tag, $val) = $line =~ m/([-\w]+)\s*:\s*(.*)/s;
72 0           $tag = lc $tag;
73 0           $tag =~ s/-/_/g;
74            
75             # some cleanup
76 0           $val =~ s/\n\s*/ /g;
77 0           $val =~ s/\s*,\s+/, /g;
78 0           push @ret, $tag, $val;
79             }
80 0           return @ret;
81             }
82              
83             1;
84              
85             __END__