http://www.perlmonks.org?node_id=546622


in reply to More efficient way to get unseen IMAP messages

where does $tmessages come from? You can just use Mail::IMAPClient's unseen method:
my @unread = $imap->unseen or warn "Could not find unseen msgs: $@\n";
Also from the docs:
foreach my $f ($imap->folders) { print "The $f folder has ", $imap->unseen_count($f)||0, " unseen messages.\n"; }
I don't know if these are more efficient runtime-wise, but certainly make the code cleaner..