#!/usr/bin/perl -w BEGIN { open(STDERR, ">>foo.err") or die "invisible error"; warn localtime() . ": $0 started " . $/; } use strict; use Foo; start(); # start the program while (1) { process(); # the main program logic goto_sleep(); # goto sleep } ### and then, in Foo.pm ####### sub process { # create an IMAP connection eval { $imap = Mail::IMAPClient->new( Server => $IMAP, User => $UNAME, Password => $PWD, Uid => 0, Debug => $DEBUGGING, ) or carp "Cannot connect to $IMAP as $UNAME: $@"; }; open LOG, ">>foo.log"; print LOG ($@) ? "failed with $@\n" : "connected successfully\n"; close LOG; }