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

I'm trying to call the Net::POP3 login method from a subroutine. For some reason it returns an undef on me. I know that's an error, but I can't figure out where to go from there. I thought it was going out of scope, so I tried the reset function, but this didn't work. I also tried doing it manually, as you'll see in the code below. It still won't work. What am I doing wrong?
my $pop3 = Net::POP3->new('$host'); ############################### PROMPT: while ($var == 2) { print '#! '; $current_input = <STDIN>; if($current_input =~ /^login/) { &login; } } ############################### sub login() { print "logging in...\n"; $pop3 = Net::POP3->new('$host'); LOGIN: if ($numb = $pop3->login("$username", "$password")) { #line + 25 system("cls"); print "logged in...\n"; print "you have $numb messages...\n"; } else { print $numb; print "couldn't login, try again?[y/n] "; my $answer = <STDIN>; if ($answer =~ /y/i) { goto LOGIN; } } goto PROMPT; }
The exact error I get when running the script is  Can't call method "login" on an undefined value at mail.pl line 25, <STDIN> line 1. What's happening? P.S. I'm running Windows 98 IndigoPerl. Thanx for your help,

- p u n k k i d
"Reality is merely an illusion, albeit a very persistant one"
-Albert Einstein

Replies are listed 'Best First'.
RE: Undef on POP3 method
by Russ (Deacon) on Jul 31, 2000 at 04:47 UTC
    It would appear that this line:
    $pop3 = Net::POP3->new('$host');
    is failing. I would suspect the '$host' part. You are passing to new() a string containing $host, not the value of $host. Since $host is not specifically defined in your login() subroutine, it must be a global to have any value there.

    I have not used POP3, but I suspect that $host is not what it expects. Try removing the single-quotes (which do not interpolate the value of any variables in them), or (yuck!) replacing them with double-quotes and see if it works correctly.

    Russ
    Brainbench 'Most Valuable Professional' for Perl

      Ahhah! it worked. Thanx a bundle. I guess I was thinkin' to hard. Well, either that or I don't know what I'm doing. ;)

      - p u n k k i d
      "Reality is merely an illusion, albeit a very persistent one."
      -Albert Einstein

        Quick and dirty explanation of interpolation for ya:

        my $this = 'hello'; my $thisvar = 'goodbye'; # prints 'hello' of course print $this; # single quotes mean no interpolation occurs, prints '$this' print '$this'; # double quotes invoke interpolation, prints 'hello' print "$this"; # perl does 'greedy' interpolation (don't know if that's the # right term) so this prints 'goodbye' print "$thisvar"; # this makes it explicit that we only want $this, so this # line prints 'hellovar' print "${this}var";
        I'm sure the faq has a more detailed explanation that covers list interpolation as well...

        -Mark

        You're certainly welcome. We all fall victim to this kind of error. If you are old (currently interpreted by Russ as > 31), you could just call this a "mature moment." :-)

        Of course, by my metric, I will never get to say that about any of my sillinesses...

        Russ
        Brainbench 'Most Valuable Professional' for Perl