Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: comparing files after FTP

by demerphq (Chancellor)
on Oct 15, 2003 at 23:41 UTC ( [id://299593]=note: print w/replies, xml ) Need Help??


in reply to comparing files after FTP

Id just like to point out that this is some scary code. Duplication like you have in that first section just screams out "refactor me!" You should convert as much of that into a subroutine, and then call it twice with the appropriate values. A perfect example of why you should do this are the following two lines

$s_ftp->binary() or die $s_ftp->message; $d_ftp->binary() or die $s_ftp->message;

while you were cutting an pasting you forgot to change the second error condition from $s_ftp to $d_ftp. If I were writing it the code would probably look something like this:

use strict; use warnings; use Carp; sub die_msg { my $ftp=shift; Carp::confess join "",@_,":\n",$ftp->message; } sub do_connect { my ($user,$pass,$host,$dir)=@_; my $ftp=Net::FTP->new($host) or die "Failed to connect to host '$host':$@"; $ftp->login($user,$pass) or die_msg $ftp,"Failed login"; $ftp->binary() or die_msg $ftp,"Failed to switch to binary mode"; $ftp->cwd($dir) or die "Cant cwd '$dir'"; return $ftp; } my $source_ftp=do_connect('user1','mypassword','some-server.com','/hom +e/test/source'); my $dest_ftp =do_connect('user2','mypassword','some-server.com','/hom +e/test/dest'); my $source_file = "test.txt"; my $dest_file ||= $source_file; $source_ftp->pasv_xfer($source_file, $dest_ftp, $dest_file) or warn "Failed xfer:\n" . $source_ftp->ok ? $dest_ftp->message : $source_ftp->message;

(Yes I know this doesnt address your real question. But this is also good advice. :-)

---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi


Replies are listed 'Best First'.
Re: Re: comparing files after FTP
by nsyed (Novice) on Oct 16, 2003 at 01:42 UTC
    Thanks a lot for the advice "demerphq" . As I mentioned I am still a newbie. I know I could have writen a better code. But I just wanted to keep it simple. Thank you all. I will try your recomendations and post the complete solution. Thanks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-20 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found