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

Hey gang,

I've got my site on a virtual dedicated server. My provider's "support" isn't that hot unless I pay $150/hr labor, so that's out. My basic problem is that the web server is experiencing bottlenecks. They say it's memory (I'm only guaranteed 256MB). So, before I just throw money at the problem and go fully dedicated (which I will likely have to do) I would like to make it as lean as possible

I have a script that uses Net::POP3 that logs into the pop server, downloads the list, checks the uidl's against a list of previously read uidls (stored in a DB) and if it's not in there, it parses it. Basic idea is:
my $pop = Net::POP3->new( $server, Debug => 0 ) || die $!; if ( $pop->login( $u_name, $p_word ) > 0 ) { my $msgnums = $pop->list() || die $!; foreach my $msgnum ( sort keys %$msgnums ) { my $uidl = $pop->uidl( $msgnum ); ## Check if message was previously read ## If not, read, parse, and move on } }
As I have a few hundred messages on the server at any time, this seems to be a pretty massive waste of resources (it runs several times an hour). Is there any way w/ Net::POP3 to keep it open, and just have it listen like a socket so I can just let the script run like a daemon?

I see the last() function, but that doesn't seem to guarantee I'll get every message.

Replies are listed 'Best First'.
Re: pop3 issue
by mr_mischief (Monsignor) on Jan 31, 2008 at 04:22 UTC
    popstat() returns the number of undeleted elements. That should be related to the highest numbered message if you haven't deleted anything this session.

    uidl( msgnum ) returns the UIDL of the given message.

    Since POP3 servers tend to number messages from oldest to newest, if your last message's UIDL has been seen, all previous ones should have been seen. If it hasn't, you could decrement the message number and get the UIDL of each in turn until one you've seen. Once you've seen one, you've seen all the previous ones.

    As a side note, you may want to consider downloading and storing the messages themselves in the database, if that would work for your application. Unlike IMAP, POP3 is not really designed nor intended to be used as long-term storage of email messages. VPS servers tend to have a huge amount of disk space and just a bit of memory. Using the database for your storage allows you to query the DB and grab just the information you need into memory (apart from overheard of course).

      Hi. That is probably a better long term goal, although I'd need to come up w/ a way to get rid of the crap messages instead of storing them in the DB. I still read the messages in my mail client though, so I'd have to come up with some way to store them in the DB only after I've read them (on my own). Thanks for the tips.