Thanks everyone. I came up with a nifty solution. It occurred to me that Perl doesn't need endline characters. Those semicolons do the trick. Here I am trying to put in those "silly rectangles (ASCII character number 10) when I didn't even need them. Actually I need one, after the first line #!/usr/bin/perl as that has no semicolon. There I just cut and paste a 'silly rectangle' from a working script. (I knew I could have cut and pasted all along, but it's far too tedious to do for every line, and if I should mess up just one instance...)
Fred --- On Tue, 3/27/12, Rob Dixon <rob.di...@gmx.com> wrote: From: Rob Dixon <rob.di...@gmx.com> Subject: Re: ASCII/binary endline character trouble To: beginners@perl.org Cc: "hOURS" <h_o...@yahoo.com> Date: Tuesday, March 27, 2012, 3:51 AM On 25/03/2012 23:45, hOURS wrote: > > I write CGI scripts for my website in PERL. When I used to upload > them with my FTP program I made sure to do so in ASCII mode rather > than binary. My host made me switch to sFTP however. I use > FIlezilla, and can't for the life of me find out how to choose the > mode on that. Unfortunately, binary is the default mode. I don't > like programming when the file displays ASCII endline characters. > I'd rather see clear, distinct lines rather than one long line broken > up with those vertical rectangles. When I download a script from my > site for editing it looks terrible. So I wrote a little program to > fix that: > > open (NEW, '>niceviewscript.pl') or die "Couldn't open file for writing"; > open (FILE, 'script.pl') or die "Couldn't open file for reading"; > while (<FILE>) { > $line = $_; > chomp($line); > print NEW "$line\n"; > } > close (FILE); > close (NEW); > > It works great. How do I get it to go in reverse though so it will > work when I upload it? I figured out that the ASCII number for those > vertical rectangles that represent line breaks was 10. So I tried: > > $sillyrectangle = chr(10); > open (NEW, '>workingscript.pl') or die "Couldn't open file for writing"; > open (FILE, 'niceviewscript.pl') or die "Couldn't open file"; > while (<FILE>) { > $line = $_; > chomp($line); > $withnewend = $line . $sillyrectangle; > print NEW $withnewend; > } > close (FILE); > close (NEW); > > Why doesn't this work? The newly created file looks just like the > old one. Now, if anyone can tell me how to upload in ASCII mode > (assuming that's even possible) that will solve my problem even > better of course. But now I'm curious as to why my program for > switching back doesn't work. The problem is that the Unix standard is to terminate lines with a linefeed (LF) character. This is the chr(10) that you are seeing. Meanwhile Windows terminates lines with the /two/ characters carriage-return linefeed (CRLF) which would be chr(13).chr(10). The terminators in a file must be changed when it is moved from one type of system to another, and FTP's ASCII mode did this automatically for you. A quick Google tells me that SFTP supports only binary mode, but an ASCII mode like FTP's is planned for the future. So you have to do the conversion separately if you are using SFTP. Perl's PerlIO layers (<http://perldoc.perl.org/PerlIO.html>) will allow you to specify the format of the file when you open it so you can write a Perl program to do the conversion. Historically the programs to do this have been ux2dos and dos2ux. Below is ux2dos.pl which you can use as ux2dos.pl unix.txt > dos.txt and the corresponding dos2ux.pl should be obvious. I hope this helps. Please come back if you have any questions. Rob use strict; use warnings; open my $fh, '<:unix', $ARGV[0] or die $!; binmode STDOUT, ':crlf'; print while <$fh>;