package myimap; our $our_cache_imap_object; sub helpdesk { if (ENTRY_POINT_FOR_CGI_ACTION...) { my $imap; if ( ! exists $our_cache_imap_object ) { # open the imap connection using IMAP::Talk $imap = Mail::IMAPTalk->new( Server => $server, Port => $imap_port, Username => $login, Password => $password, Separator => "\\", RootFolder => '', CaseInsensitive => 1, Uid => 1) || die "Connection failed. Reason: $@"; $our_cache_imap_object = $imap; } elsif ($our_cache_imap_object->is_open()) { $imap = $our_cache_imap_object; } DEAL WITH THE IMAP OBJECT .... ETC. } } 1;