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

I'm writing an email script so I can check my mail through perl. Once in a while I recieve an error that returns the login method of Net::POP3 as undefined. This exits the script. I've been trying to catch the error using an eval, but when I use it, I can't login. The code is below. Thanx for your help, I hate to be such a bother to the PerlMonks (with my frequent posts and all).
sub login() { print "logging in...\n"; $pop3 = Net::POP3->new($host); LOGIN: if ($numb = eval'$pop3->login("$username", "$password"'){ system("cls"); print "logged in...\n"; $status = 1; $numb =~ s/^0E0$/0/; print "you have $numb messages...\n"; $variable = $numb; } else { print $numb; print "couldn't login, try again?[y/n] "; $answer = <STDIN>; if ($answer =~ /y/i) { login(); } } goto PROMPT; }
- p u n k k i d "Reality is merely an illusion, albeit a very persistent one." -Albert Einstein

Replies are listed 'Best First'.
Re: Login Error
by tye (Sage) on Aug 01, 2000 at 00:20 UTC

    You are missing a closing paren. When you use eval you should always check and report $@.

Re: Login Error
by slurp (Initiate) on Aug 01, 2000 at 04:28 UTC
    Hi, You don't need to use eval in this case. First of all you may want check whether new() returns something you can use and then you safely can call login. This small script will hopefully show you one possible direction:
    use Net::POP3; my $pop = Net::POP3->new("pluto.runbox.com"); if ($pop) { my $msg = $pop->login("test21","test"); if ($msg) { $msg eq '0E0' and $msg = 0; print $msg." messages\n"; } else { print "Couldn't read...\n" } $pop->quit(); } else { print "Couldn't connect...\n" }
    -- slurp
      Just FYI, "0E0" ("0 but true") evaluates to true in a boolean context but will still equal zero if you do a numeric test. So you don't have to do a string comparison against '0E0'. A simple if ($msg) { ... if ($msg == 0) { ... } } would work as you'd expect (for cases where you're testing), and forcing it to be numeric could easily be done just by adding zero ($msg += 0;) or printing it out via s?printf.