I have the following in my script
#!/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; }

My logic is thusly -- if something is wrong enough for the script to not compile, it will write the reason in foo.err and die rightaway. However, if compilation is successful, then it will try to connect to the IMAP host. If that fails, it will write to foo.log and go to sleep for a specified time, and then wake up and try again.

Except, my logic is wrong. If the connection to the IMAP host fails, the error gets written to foo.err and the script dies. This indicates that the script is checking for the IMAP host connection right at compile time.

What am I doing wrong, and how can I achieve what I want?

What I want is -- after the script starts up successfully, I want it to make the connection to the IMAP host. If successful, do the rest of its stuff, else write a note in the log and go to sleep only to try again later.

Update: Stranger still... if the connection fails, I get an error in foo.err, and yet, I also get "connected successfully" in foo.log. This is most bewildering.


In reply to Understanding compiletime vs. runtime by punkish

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.