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.


In reply to My first Perl application! mailsumm.pl by gizmoguy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.