Hello, I (Perl newbie) tried to write a script that checks the number of messages on my POP3 accounts. This is my code:
#!/usr/bin/perl use strict; use warnings; use diagnostics; use Net::POP3; use Term::ReadKey; # Read the mail accounts from a file called addresses # which resides in the script's directory open ADDRESSES, "./addresses"; # read all the lines my @addys = <ADDRESSES>; # close address file close ADDRESSES; # do the following for every address foreach my $address (@addys) { # chomp newlines chomp $address; # split address into username and hostname (my $user, my $host) = split /\s/, $address; # let the user enter his password print "Please enter the password for $user\@$host\:"; ReadMode( "noecho" ); my $pass = ReadLine(); ReadMode( "normal" ); print "\n"; # connect to host my $pop = Net::POP3->new( $host ) or do { print "Could not initialize connection.\n"; next; }; print "Connected to $host.\n"; # send USER $pop->user( $user ) or do { print "Could not send user name.\n"; next; }; print "Sent username $user.\n"; # send pass, receive number of messages my @a = $pop->pass( $pass ) or do { print "Could not send password.\n"; next; }; # print number of msg or complain about failed login if (defined $a[0]) { $pop->quit(); print "Messages: $a[0]\n"; } else { print "Login failed.\n"; } }
The script accesses a file called "addresses" where login and POP3 server are stored in the following way: <login> <pop3 server>\n <login> <pop3 server>\n etc. Generally the script does its work as long as login to the server works too. If login fails I get an error message: Use of uninitialized value in numeric eq (==) at /usr/share/perl/5.8.2/Net/POP3.pm line 302, <STDIN> line 1 (#1) The line itself is:
sub _QUIT { shift->command('QUIT')->response() == CMD_OK }
Seems like the problem is that the server after getting the wrong password closes the connection and this way $pop gets undefined? Can anyone please show me a workaround? Thank you

20031224 Edit by BazB: Changed title from 'Net::POP3'


In reply to How to check number of messages using Net::POP3? by BjoernD

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.