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

Dear Monks, I am facing a problem where FTP'ing a file ending with newline characters is creating an issue,please look at the file below am FTPing and notice the "\n" characters,these "\n"characters are messing up my procedure.how should I remove the "\n" characters while FTP'ng.Please advise

open(FILE, ">micro\\config") || die "Wrong Filename"; print FILE ("micro=/mnt/micro/config/\n"); print FILE ("config=/mnt/system/config/\n"); print FILE ("dump=/mnt/dump/\n"); config file gets created as follows micro=/mnt/micro/config/ config=/mnt/system/config/ dump=/mnt/dump/ FTP'ing using the below code:- $ftp = Net::FTP->new("10.69.11.65", Debug => 0) or die "Cannot connect to the target: $@"; $ftp->login("user","user") or die "Cannot login ", $ftp->message; $ftp->binary(); $ftp->cwd("/mnt/micro/config") or die "Cannot change working directory ", $ftp->message; $ftp->put("micro/config", "config") or die "put failed ", $ftp->message; $ftp->quit();

Replies are listed 'Best First'.
Re: New line problem
by davido (Cardinal) on Apr 25, 2011 at 19:40 UTC

    You seem to be FTPing non-binary data, yet you're setting your FTP handler to 'binary()' mode. Then you're sending the file somewhere that presumably uses different styles of line endings from your system. Linux, Windows, and Mac have different line endings, which is handled by FTP automatically if you use text mode, but not if you use binary mode.


    Dave

Re: New line problem
by mikeraz (Friar) on Apr 25, 2011 at 19:52 UTC

    What Dave said with two clarifications.

    • text mode is more commonly referred to as ASCII mode, eg, in the Net::FTP documentation
    • ASCII mode is the default, so if you remove the line  $ftp->binary(); from your code you'll solve your described problem


    Be Appropriate && Follow Your Curiosity
Re: New line problem
by bart (Canon) on Apr 26, 2011 at 11:38 UTC
    You can go at it from the other end. At least, when you're working on Windows, as I assume.

    If you do use binmode on the file handle, when you're generating the file, then you're creating a Unix style text file. (If you don't, you're making a Windows style textfile.) In this case it is safe to FTP the file to a Linux machine, in binary mode.

    That is all under the assumption that the character set is compatible between the two platforms: either UTF-8 or ISO-Latin-1 (or Windows CP1252, if you don't use any weird characters, like "smart quotes".)