lib/Message/2822.pm
changeset 6 caaa00b9165b
child 7 5f6309f60053
equal deleted inserted replaced
5:2e7d31c0cdfe 6:caaa00b9165b
       
     1 package Message::2822;
       
     2 
       
     3 use 5.010;
       
     4 use strict;
       
     5 use warnings;
       
     6 use IO::File;
       
     7 use if $ENV{DEBUG} => "Smart::Comments";
       
     8 
       
     9 my %DATA;
       
    10 
       
    11 sub new {
       
    12     my $class = ref $_[0] ? ref shift : shift;
       
    13     my $this = bless \(my $x) => $class;
       
    14     my %arg = @_;
       
    15 
       
    16     $DATA{$this}{file} = $arg{file};
       
    17     $DATA{$this}{fh} = IO::File->new($DATA{$this}{file})
       
    18 	or die "Can't open $DATA{$this}{file}: $!\n";
       
    19 
       
    20     local $/ = "";
       
    21     my $_ = $DATA{$this}{fh}->getline();
       
    22 
       
    23     foreach (split /\n(?=\S)/, $_) {
       
    24 	my (undef, $k, $v) = split /(^(?:\S+:|(?i:from )))/, $_;
       
    25 	push @{$DATA{$this}{header_fields}}, $k;
       
    26 	$DATA{$this}{header}{$k} = $v;
       
    27     }
       
    28 
       
    29     return $this;
       
    30 }
       
    31 
       
    32 sub header_fields {
       
    33     my $this = shift;
       
    34     return @{$DATA{$this}{header_fields}};
       
    35 }
       
    36 
       
    37 sub header_content {
       
    38     my $this = shift;
       
    39     my $field = shift;
       
    40 
       
    41     my $_ = $DATA{$this}{header}{$field};
       
    42 
       
    43     if (not defined) {
       
    44 	($field) = grep /^$field$/i => $this->header_fields();
       
    45 	$_ = $DATA{$this}{header}{$field}
       
    46     }
       
    47 
       
    48     /^\s*(.*?)\s*$/m;
       
    49     return $1;
       
    50 }
       
    51 
       
    52 sub DESTROY {
       
    53     my $this = shift;
       
    54     delete $DATA{$this};
       
    55 }
       
    56 
       
    57 
       
    58 1;