Skip to content

Instantly share code, notes, and snippets.

@xtetsuji
Created June 8, 2016 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtetsuji/8205a5451cea7c69f509e3677605e352 to your computer and use it in GitHub Desktop.
Save xtetsuji/8205a5451cea7c69f509e3677605e352 to your computer and use it in GitHub Desktop.
MH format mail file simple parser.
#!/usr/bin/perl
# mhls MH_FILENAME ...
# MH 形式のファイルを引数に与えるとファイルサイズとDateヘッダから導き出される日付を表示します
use strict;
use warnings;
use Date::Format qw(time2str);
use Date::Parse qw(str2time);
use List::Util qw(first);
use Term::ANSIColor qw(colored);
my @mh_filenames;
for my $filename (@ARGV) {
my $filename = shift;
if ( !-f $filename ) {
warn colored(qq(file "$filename" is not found.\n), "yellow");
} else {
push @mh_filenames, $filename;
}
}
for my $filename (@mh_filenames) {
my $mh = lookup_mh($filename);
printf "%s\t%d\t%s\n", $filename, $mh->{size}, $mh->{format_date};
}
sub lookup_mh {
my $filename = shift;
open my $fh, '<', $filename;
my @header;
while (<$fh>) {
chomp;
last if /^$/;
push @header, $_;
}
close $fh;
my $date = search_date(\@header);
my $size = -s $filename;
my $format_date = time2str("%Y/%m/%d %H:%M:%S", str2time($date));
return +{ date => $date, size => $size, format_date => $format_date };
}
sub search_date {
my $array = shift;
my $date = first { /^Date:/ } @$array;
$date =~ s/^Date:\s*//i;
return $date;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment