Hi,
Well done, useful script that works ;)
...However ;)
-
$' is not advised, instead consider the form
if ($curline =~ /^Subject: (.*)$/){
$curmsg{Subject} = $1;
Note: That this also removes the need for the chomp statement in readmail, you should really do all relevant processing in one place.
- I prefer to use lexically scoped variables than $_ in sub-routines and loops, it means that I can read the program naturally when I come back to it in 6 months.eg:
sub checkdir {
my $directory = shift;
my $is_directory=(( -e $directory ) && ( -d $directory))?0:1;
$return $is_directory;
}
OR
sub printhash {
my $hash = shift;
for my $key (keys %{$hash}) {
print "$key : $hash->{$key}\n";
}
}
- In the checkdir example above I also modified it so that there was only one exit point. This makes life easier when it comes to debugging.
- In the printhash example above I illustrated the use of the keys keyword which returns the keys of a hash as a list
- Also in the printhash routine you will see that I changed the parameter from a hash to a hash reference, as a general rule this makes life easier since you can pass multiple hashes, arrays and scalars without one swallowing the others.
- Unlikely in this context, but imagine a file called >0A1234567.msg, you should use the three param form of open like so open (my $file_handle , '<', "$filename");
- When you parse through the mail file you should probably terminate the loop when you finish reading the headers ie
last if $curline=~/^$/;
- and finally I prefer the .= operator rather than$var = $var . "/".
Believe me when I say my first Perl program had all these issues and more, this is a very good first program.
Updated as per hexcoder's advice below
print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."