Chon-Ji has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to make simple ftp module using perl, here'smy code
$ftpobj = Net::FTP -> new ($URL) or die "unable to connect\n"; print "Connection successful\n"; print "retrieving file from medix\n"; $ftpobj -> login($user, $pass); $ftpobj -> cwd ($src_dir); @files=$ftpobj->dir; foreach(@files) { print "$_\n"; } $ftpobj -> get ("INPUT", "$dest_dir/OUTPUT"); $ftpobj -> quit; print "file size ",-s "$dest_dir/OUTPUT"," bytes\n";
This code is able to transfer file but the problem is the fileszie of the file transaferred is alaways less than the source file. For instance, my soucrce file is supposed to be 11046 bytes but the transffered file is only 10257.

Replies are listed 'Best First'.
Re: Perl ftp
by BrowserUk (Patriarch) on Jul 03, 2006 at 02:24 UTC

    This is probably because you are transfering binary files but are not calling the binary method before the transfers and some line-ending conversion is being done.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      what is the binary method? how doicall it? thanks

        From the Net::FTP docs:

        binary

        Transfer file in binary mode. No transformation will be done.

        Hint: If both server and client machines use the same line ending for text files, then it will be faster to transfer all files in binary mode.

        Just call it (once) before transferring any files. Something like:

        $ftpobj->binary; $ftpobj->get( $inputfile, $outputfile ) or die $!;

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Perl ftp
by dorko (Prior) on Jul 03, 2006 at 02:33 UTC
    Is it possible your file is ASCII and you are FTPing between operating systems with differing line endings? If so, FTP will convert between different kinds of line endings on the fly. From the Net::FTP docs:
    The protocol also defines several standard translations which the file can undergo during transfer. These are ASCII, EBCDIC, binary, and byte. ASCII is the default type, and indicates that the sender of files will translate the ends of lines to a standard representation which the receiver will then translate back into their local representation.
    This could cause you to lose or gain a couple of bytes, depending on the circumstances.

    Cheers,

    Brent

    -- Yeah, I'm a Delt.
      thanks it worked!