I've just started learning Perl a few days ago, and this is my first program. mailsumm.pl prints out a summary of a Maildir mailbox. It takes 2 arguments: the first is the path to the Maildir, and the second is a comma-separated list of subdirectories under the Maildir you wish to look at. Here's the code:
#! /usr/bin/perl # mailsumm.pl use strict; use warnings; sub readmail; sub checkdir; sub checkmsg; my $maildir = shift; my @subdirs = split ",", shift; unless ($maildir =~ '/$') { $maildir = $maildir . '/' } for (@subdirs) { unless ($_ =~ '/$') { $_ = $_ . '/'; } $_ = $maildir . $_ } readmail @subdirs; sub checkdir { (-e $_[0] and -d $_[0]) or return 1; return 0; } sub readmsg { open FH, $_[0] or die ($! . "\nFile: " . $_[0]); my $curline; my %curmsg = (Subject => '', Sender => '', Date => ''); while ($curline = <FH>) { if ($curline =~ /^Subject: /) { $curmsg{Subject} = $'; #' (fixes syntax highlighting in emacs) } elsif ($curline =~ /^From: /) { $curmsg{Sender} = $'; #' } elsif ($curline =~ /^Date: /) { $curmsg{Date} = $'; #' } else { next; } } close FH; return %curmsg; } sub printhash { for (0...$#_) { if ($_ % 2) { print $_[$_] . "\n"; } else { print $_[$_] . ": "; } } } sub readmail { for (@_) { unless (checkdir $_) { print "\nMail under $_\n" . '-'x(11 + length $_) . "\n\n"; opendir DH, $_ or die ($! . "\nFolder: " . $_); while (my $curfile = readdir DH) { unless (($curfile eq '.') or ($curfile eq '..')) { my %curmsg = readmsg ($_ . $curfile); chomp $_ for %curmsg; printhash %curmsg; print "\n"; } } closedir DH; } } }
and some sample output:
gizmoguy@geek:~$ ./mailsumm.pl Maildir/ tmp/ Mail under Maildir/tmp/ ----------------------- Subject: new user mail Sender: vroom@perlmonks.org Date: Tue, 17 Aug 2010 21:49:36 -0400
I'd be very appreciative if anyone could give me some comments on coding style, etc., as I'm just starting out. Hope y'all like it. :) Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: My first Perl application! mailsumm.pl
by Utilitarian (Vicar) on Aug 19, 2010 at 09:05 UTC | |
by hexcoder (Curate) on Aug 28, 2010 at 22:02 UTC | |
|
Re: My first Perl application! mailsumm.pl
by jwkrahn (Abbot) on Aug 19, 2010 at 17:44 UTC |