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

Hello monks! I'm trying to write a simple (just for learning purposes) e-mail client using the Net::POP3 module.So far i've written this code (only 8 lines :P)
#!/usr/bin/perl -w use strict; use Net::POP3; my $pop = Net::POP3->new('localhost', Timeout => 30); my $user = 'root'; my $pass = 'something'; $pop->login ($user,$pass); $pop->top();

but the result is an error
Can't call method "login" on an undefined value at ./pop3.pl line 7.

Where is my mistake!? (and what is a method!???)
Antonis!

Replies are listed 'Best First'.
Re: use (of) Net::POP3
by Tomte (Priest) on Jun 21, 2003 at 11:27 UTC

    $pop is undefined, the new call failed.

    a method call is a subroutine call on an object (!?), new is a sub (the constructor) in the Net::POP3 module, and so is login.

    Try to replace

    my $pop = Net::POP3->new('localhost', Timeout => 30);
    with
    my $pop = Net::POP3->new('localhost', Timeout => 30) or die("Can't ins +tantiate Net::POP3 object");

    Are you sure a pop3 server is listening on your localhost? I tried the snippet and it works if the host is listening, and fails if it doesn't.

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

      Thank you Tomte that line solved my problem, and from now on pop3 is listening!
Re: use (of) Net::POP3
by vek (Prior) on Jun 21, 2003 at 15:58 UTC

    You should try to get into the habit of checking the return value of all the method calls and either handle failures or die accordingly.

    my $pop = Net::POP3->new('localhost', Timeout => 30) || die "Couldn't connect to localhost\n"; $pop->login($user, $pass) || die "Couldn't login as $user with $pass\n"; # etc...
    This will save you some grief in the long run.

    -- vek --
Re: use (of) Net::POP3
by ScooterQ (Pilgrim) on Jun 21, 2003 at 12:17 UTC
    Please please please please PLEASE consider running this script as someone other than root. I know you're just testing, but if this test ever changes to go over the network rather than to localhost you will be exposing your root password via clear text over the network.
    In addition, working as root on a regular basis is a bad idea. Log in as a normal user and su only when necessary. Very bad things can happen when you're root.
Re: use (of) Net::POP3
by hydo (Monk) on Jun 21, 2003 at 11:35 UTC
    Heheh.. I wonder if I still have a copy of the command line POP client I wrote so long ago when I was learning. Ahh memories. Anyway...

    Are you sure that you have a POP3 server running on localhost? If nothing else, and to make completely sure, telnet to 'localhost 110' and make sure you get a response.

Re: use (of) Net::POP3
by atnonis (Monk) on Jun 21, 2003 at 17:54 UTC
    Yes vek i agree with you. So i've check if
    $pop->login ($user,$pass);
    it returns true or false and it returns false. The error i get is
    Could not login as root with something Use of uninitialized value in numeric eq (==) at /usr/lib/perl5/5.8.0/ +Net/POP3.pm line 301. Use of uninitialized value in numeric eq (==) at /usr/lib/perl5/5.8.0/ +Net/POP3.pm line 302.

    is this my fault (again) or is it some kind of bug (i don't think so but..)!?
    Atnonis!

      Looking at the linenumbers, there is a version mismatch between your module and mine :-)

      Nevertheless, the lines in question are almost certainly:

      sub _USER { shift->command('USER',$_[0])->response() == CMD_OK } sub _PASS { shift->command('PASS',$_[0])->response() == CMD_OK }
      shift->command() returns the object itself, calling response() to fetch the , well, response.

      Looking at Net::Cmd::response() shows, that it will return undef if the response from the server can't be parsed.
      I would manualy play through the commands via telnet server 110 to see what's going on, and have a deeper look at POP3.pm and Cmd.pm than my quick glance now.

      regards,
      tomte


      Hlade's Law:

      If you have a difficult task, give it to a lazy person --
      they will find an easier way to do it.

        You can also turn on debugging in Net::POP3 by adding it after the server in new:

        my $pop = Net::POP3->new($mailserver, Debug => 1);

        That should get it to show you whats going on behind the scenes as the script runs.