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>;



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to