Eitan Adler wrote:
I wrote a program to fetch email from a IMAP account, look to see if I sent
it, if yes execute any commands in the email and email the results back to
via a SMTP server.
1) This being my first perl program I'd like to know if I'm using the proper
perl idioms and language features. Is there anything I could do to improve
my code?
Sure.
use constant false => 0;
use constant true => 1;
Constants are usually written in all uppercase to distinguish them from
keywords, functions, operators and subroutines. How did you choose the
arbitrary values 0 and 1 for false and true instead of using other
values? Why did you name them false and true instead of zero and one?
sub doMessage {
print "Doing a message...\n";
my %command = ();
$command{"command"} = undef;
$command{"reply"} = false;
Creating and initializing a hash is usually done like:
my %command = (
command => undef,
reply => false,
);
But why do you need to define those keys at all when autovivification
can usually take care it for you?
my @lines = split('\n',$_[0]);
my $m_id = $_[1];
foreach(@lines)
{
my @coml = split(":",$_,2);
print "Command: ". $coml[0];
print "\nText:" .$coml[1];
print "\n";
given(uc $coml[0])
{
when("COMMAND")
{
$command{"command"} = $coml[1];
print PUSHCOLOR BLUE . $coml[1] . POPCOLOR
."\n";
}
when ("REPLY")
{
$command{"reply"} = true;
}
You don't have a default {} block so you are saying that $coml[0] will
only ever contain 'command' or 'reply'? The only thing you do with
$command{ reply } is set it to 0 at the beginning and set it to 1 here
so why do you even have this variable?
}
}
my $res;
if (defined($command{"command"}))
{
$res = doCommand($command{"command"});
}
sendResults($res, $m_id);
print "\n";
}
Perhaps you only need something like this:
sub doMessage {
print "Doing a message...\n";
my ( $msg, $m_id ) = @_;
if ( $msg =~ /(?s:.*)^command:(.+)/im ) {
print PUSHCOLOR BLUE, $1, POPCOLOR, "\n";
sendResults( doCommand( $1 ), $m_id );
}
print "\n";
}
John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/