#!/usr/bin/perl

use strict;
use warnings;

###########################################
#
# subjects2.pl
#
# Prints out the Subject lines from a
# mail inbox in "mbox" format.
#
# Takes file name as command-line argument.
# Prints subject lines in alphabetical order.
#
###########################################

my $filename = shift @ARGV;

if( !$filename )
{
    print STDERR "Usage: $0 mailbox\n";
    exit( 1 ); 
}

open IN, $filename or die "Can't open $filename";

my @list_of_subjects;
while( <IN> )
{
    if( /^Subject:/i )
    {
        my $line = $_;
        chomp $line;
        $line =~ s/^Subject:\s*//i;
        push @list_of_subjects, $line;
    }
}

for my $s (sort @list_of_subjects)
{
    print $s . "\n";
    #print $s . '\n';  Bad.
    #print $s , "\n";
    #print "$s\n";
}

close IN;
