Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Perlmonks greetings,

I am still in my infancy at writing perl. The following script is by no means ingeneous, but serves a simple purpose of mine.

The script is not fully tested. I am very interested in enhancing the part where I present the user with the options and wait for the command. I have used a hash to store the options and a loop to resent the options to the user, but I am not totally convinced that this is the best way to go about it.

Thank you.
use strict; use Net::POP3; my ($popmail, $numofmessages, $command, @params); my (%options); %options = ( 'C' => ['Check', \&pop_check, ' the messages in the current accoun +t'], 'D' => ['Delete', \&pop_delete, ' a message or messages from your +account'], 'L' => ['Download', \&pop_download, ' a message or messages from y +our account'], 'Q' => ['Quit', \&pop_quit, '...'] ); ## ############################################################ ## first run, logon and check the messages in the mailbox $numofmessages = pop_login(); pop_check(); do { print_menu(); chomp($command = <STDIN>); ## the input format should be along the lines of ## D,1,2,3,4,5,6,7 ($command, @params) = split /,/,$command; $command = uc($command); if ($options{$command}) { $options{$command}[1]->(@params) } else { print 80 x "="; print "\nCommand unrecognized\n"; print 80 x "="; } } until ($command eq 'Q'); ## ############################################################ ## print the menu options that are stored in the %options hash sub print_menu { my($option); foreach $option (keys %options) { print "$option : $options{$option}[0] - $options{$option}[2]\n +"; } } ## ############################################################ ## Delete the specified messages sub pop_delete { ## return codes to be used for interpretation ## in later development of the script return 0 unless (exists($_[0]) ); ## the messages will not be deleted instantly ## they will be marked for deletion and only ## removed from the server upon quitting the session foreach (@_) { $popmail->delete($_); print ". Message $_ marked for deletion.\n"; } return 1; } ## ############################################################ ## Download the specified messages sub pop_download { ## return codes to be used for interpretation ## in later development of the script return 0 unless (exists($_[0]) ); ## the messages will not be deleted instantly ## they will be marked for deletion and only ## removed from the server upon quitting the session foreach (@_) { open MESSAGE, ">message_$_.txt"; $popmail->get($_, MESSAGE) print ". Message $_ saved in message_$_.txt.\n"; } return 1; } ## ############################################################ ## login to the mail server sub pop_login { my($server, $username, $password); if (@_ != 0) { ## any arguments passed $server = shift; $username = shift; $password = shift; } ## ###################################################### ## use the defaults unless otherwise specified $server = 'mail.server.com' unless (defined); $username = 'username' unless (defined); $password = 'password' unless (defined); $popmail = Net::POP3->new($server); if ($popmail) { $numofmessages = $popmail->login($username , $password); } else { print "Server $server not available\n\n"; exit } ## the login method will either return the number of messages ## upon successful login ## a sting equating to ZERO if there are not messages ## undef if login is not successful if ( defined($numofmessages) ) { ## no messages in mailbox return $numofmessages; } else { return 0; } } ## ############################################################ ## check messages in the current server sub pop_check { if ( defined($numofmessages) ) { if ($numofmessages == 0) { ## no mail print "There are no messages in your mailbox\n\n"; exit; } else { print "$numofmessages message(s) in your mailbox\n\n"; my ($i, $content, $messagesize); for ($i=1; $i<=$numofmessages; $i++) { $content = $popmail->top($i); $messagesize = $popmail->list($i); print "\nMessage $i\n"; print "Message Size = $messagesize\n"; foreach (@{$content}) { /^(From|Subject|Date):\s+/i + && print; } } # for the message loop } # for the second if } else { print "Could not login to specified account\n"; exit; } # for the if defined } ## ############################################################ ## quit the current session sub pop_quit { $popmail->quit; }