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

I am using the following code to access an FTP server but keep getting an error
#!/usr/bin/perl -w use Net::FTP; my $hostname='xxx.xx.x.x'; my $user='xxxxxx'; my $password='xxxx'; my $ftp=Net::FTP -> new($hostname) or die ("Connect failed"); $ftp->quit;
the error i get is:
Name "main::username" used only once: possible typo at filedate.pl lin +e 15.
Does anybody recognise this error and how to fix it?

thanks all

Replies are listed 'Best First'.
Re: Net::FTP problem
by talexb (Chancellor) on Feb 26, 2002 at 16:27 UTC
    use Net::FTP; my $hostname=xxxx'; my $user='xxxx'; # <<----------- USER my $password='xxxx'; my $ftp=Net::FTP -> new($hostname) or die ("Connect failed"); $ftp->login($username,$password); # <<---------- USERNAME $ftp->quit;
    User, Username, pick one. :) And always use -w as a compile option, and always use strict.

    Yes, yes, it's two more things to type in, but if you apply great craftmanship to your code, you will be rewarded with something that works the way you think it should much more often than not.

    --t. alex

    "There was supposed to be an earth-shattering kaboom!" --Marvin the Martian

Re: Net::FTP problem
by Dog and Pony (Priest) on Feb 26, 2002 at 16:22 UTC
    First of all, you might want to try and use diagnostics (and strict as always).

    Second, which of those lines are line 15? This is the kind of error you get when you declare a variable and then uses another, usually because of a typo. I see that you declare $user - do you by any chance try to login with $username but it should be $user? In you example, it should probably warn on line 6, that main::user is only used once - so this isn't the exact code that produces the error, is it?

    UPDATE: And there it was. The line I was missing. :)


    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
Re: Net::FTP problem
by Zaxo (Archbishop) on Feb 26, 2002 at 16:28 UTC

    The warning is doing exactly what it's supposed to do, catching a misspelling. The variable is called $user, earlier. use strict;, btw.

    After Compline,
    Zaxo

Re: Net::FTP problem
by costas (Scribe) on Feb 26, 2002 at 16:17 UTC
    sorry i forgot to include a line in the code above

    use Net::FTP; my $hostname=xxxx'; my $user='xxxx'; my $password='xxxx'; my $ftp=Net::FTP -> new($hostname) or die ("Connect failed"); $ftp->login($username,$password); $ftp->quit;
      This is an absolutely classic example of the need for use strict. It would have flagged your problem immediately and precisely. Don't code without it.