Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Net::FTP troubles

by runrig (Abbot)
on Nov 09, 2001 at 02:13 UTC ( [id://124211]=note: print w/replies, xml ) Need Help??


in reply to Net::FTP troubles

foreach $key(%Servers){ should be:
foreach $key (keys %Servers){
and actually I'd scrap the first declaration of $key and make that:
foreach my $key (keys %Servers){
You'd also do well to check the status of your Net::FTP methods. new() returns undef and an error message in $@ on failure, and the rest will return undef but the error message will be returned from
$FTP->message (and don't put it in quotes since its a method not a variable).
And why are you chomping $IP?? It doesn't come from any file or user input...

Update: Though a good catch, this line:

$IP="$Server{$key}";
is almost certainly just a typo here since use strict should be complaining about it (generating a "Global symbol requires explicit package name..." error) before the runtime error that RedDog is currently getting (at least I get a compile time error with build 628, if you don't get one with that code, maybe you should upgrade). And I agree with the opinions that if you don't need a hash, don't put the IP's in a hash.

Replies are listed 'Best First'.
Re(dmmiller2k): Net::FTP troubles
by dmmiller2k (Chaplain) on Nov 09, 2001 at 19:21 UTC

    I agree about the foreach loop.

    In addition, the actual error message (apparently) refers to the variable $IP as being undefined; this is because the assignment to $IP is using a different hash than the one that is being looped through. The loop starts with:

    foreach $key(%Servers){

    which loops through all of the key/value pairs of the hash %Servers in pseudo-random order (probably not what you wanted, as runrig has said). However, your assignment to $IP uses the variable $key to retrieve values from another hash called %Server, which is undoubtably empty, in fact making $IP undefined.

    Also, as has been noted elsewhere, you most certainly do not need the double quotes around scalar values, for example, $Server{$key}: they force unnecessary interpolation, since $Server{$key} itself is already a string.

    There is no particular reason to use a hash at all, preferring an array, which will preserve your ordering of IP addresses. I would go one step further and remove the unnecessary call to chomp (since the addresses do not end in newlines):

    my @Servers = qw( 128.37.37.201 128.38.38.201 128.39.39.201 ); foreach my $IP ( @Servers ) { my $FTP = Net::FTP->new($IP); if ( $FTP->login($user,$pass) ) { $FTP->ascii(); $FTP->put($File); $FTP->quit; } else { print "Error logging in to $IP: ". $FTP->message ."\n"; } }

    dmm

    
    You can give a man a fish and feed him for a day ...
    Or, you can teach him to fish and feed him for a lifetime
    
      I suspect the %Server vs. %Servers thing was a typo. His code sample shows him using strict, so an error like this should have been caught early on.

      Good catch, though. I'd missed this one.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://124211]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-16 07:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found