Discussion:
ASCII/binary endline character trouble
(too old to reply)
hOURS
2012-03-25 22:45:11 UTC
Permalink
Hello all,
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.

Thank you!
Fred
timothy adigun
2012-03-26 01:35:12 UTC
Permalink
Hi Fred,
Please, check my comments and possible solution within your mail.
Post by hOURS
Hello all,
I write CGI scripts for my website in PERL. When I used to upload them
with my FTP
The programming language is Perl not PERL.
Post by hOURS
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
open (NEW, '>niceviewscript.pl') or die "Couldn't open file for writing";
I think is better to use open() three arguments like:
open(my $fh,">",'niceviewscript.pl') or die "Couldn't open file for
writing: $!";
Post by hOURS
open (FILE, 'script.pl') or die "Couldn't open file for reading";
## same thing here too
Post by hOURS
while (<FILE>) {
$line = $_;
chomp($line);
print NEW "$line\n";
}
close (FILE); ## close($fh) or die "can't 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
$sillyrectangle = chr(10);
open (NEW, '>workingscript.pl') or die "Couldn't open file for writing";
## same as above
Post by hOURS
open (FILE, 'niceviewscript.pl') or die "Couldn't open file";
## same thing here
Post by hOURS
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
Possible Solution:
Launch your FileZilla Program, click on the menu bar "Transfer" menu,
then check your sub-menu bar "Transfer type", you should be able to change,
your mode. I preferred to leave mine as "Auto"!
Post by hOURS
will solve my problem even better of course. But now I'm curious as to
why my program for switching back doesn't work.
Thank you!
Fred
--
Tim
hOURS
2012-03-26 22:50:23 UTC
Permalink
Thanks.  I do see the "Transfer Type" option under the "Transfer" menu, but it's grayed out and I can't use it.

I also tried modifying the first line of my code.  Instead of using the ASCII value and the chr function, I cut and pasted the vertical rectangle.  So the line became:
$sillyrectangle = "";
(with said rectangle between the quotemarks - it won't show up in this e-mail)
Needless to say, that didn't work either.

Fred

--- On Sun, 3/25/12, timothy adigun <***@gmail.com> wrote:

From: timothy adigun <***@gmail.com>
Subject: Re: ASCII/binary endline character trouble
To: "hOURS" <***@yahoo.com>
Cc: "Perl Beginners" <***@perl.org>, perl-***@yahoogroups.com
Date: Sunday, March 25, 2012, 9:35 PM

Hi Fred,
Please, check my comments and possible solution within your mail.

On Sun, Mar 25, 2012 at 11:45 PM, hOURS <***@yahoo.com> wrote:

Hello all,

I write CGI scripts for my website in PERL.  When I used to upload them with my FTP 
  The programming language is Perl not PERL.

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";
I think is better to use open() three arguments like:

open(my $fh,">",'niceviewscript.pl') or die "Couldn't open file for writing: $!";


open (FILE, 'script.pl') or die "Couldn't open file for reading";
## same thing here too


while (<FILE>) {

  $line = $_;

  chomp($line);

  print NEW "$line\n";

}

close (FILE); ## close($fh) or die "can't 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";
## same as above


open (FILE, 'niceviewscript.pl') or die "Couldn't open file";
## same thing here


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  Possible Solution:
  Launch your FileZilla Program, click on the menu bar "Transfer" menu, then check your sub-menu bar "Transfer type", you should be able to change, your mode. I preferred to leave mine as "Auto"! 

will solve my problem even better of course.  But now I'm curious as to why my program for switching back doesn't work.




Thank you!

Fred
 
--
Tim
timothy adigun
2012-03-27 06:50:21 UTC
Permalink
Hi Fred,
Thanks. I do see the "Transfer Type" option under the "Transfer" menu,
but it's grayed out and I can't use it.
I don't know the version of FileZilla you have, but I believe you could
download a new one for use. I use version 3.5.1, I think the recent one is
3.5.3. Please google it.
I also tried modifying the first line of my code. Instead of using the
ASCII value and the chr function, I cut and pasted the vertical rectangle.
$sillyrectangle = "";
(with said rectangle between the quotemarks - it won't show up in this e-mail)
Needless to say, that didn't work either.
Please, allow me to think aloud, since we couldn't get how this "vertical
rectangle" character looks like (or your input file) maybe you could use
both "ord" and "chr" functions in Perl to *pick* each word or character (as
the case maybe) in your while loop used for reading the original file you
downloaded from your site. This could give you the numeric value, you can
use for your "$sillyrectangle" using chr function. However, you must note
that ord function will only return the numeric value of first *character*
of your EXP, in case you try using word on the function.

lastly, I don't know if it works perfectly for you. You could also look
into unpack and pack functions, using "B*" Template for conversion; like:
while(<>){
print unpack "B*",$_; ## output in binary files and pack reverses
}

I hope any of this suggestions helps. But really if you ask me I will
say, download and do a fresh installation of FileZilla.
Fred
Subject: Re: ASCII/binary endline character trouble
Date: Sunday, March 25, 2012, 9:35 PM
Hi Fred,
Please, check my comments and possible solution within your mail.
Hello all,
I write CGI scripts for my website in PERL. When I used to upload them
with my FTP
The programming language is Perl not PERL.
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
open (NEW, '>niceviewscript.pl') or die "Couldn't open file for writing";
open(my $fh,">",'niceviewscript.pl') or die "Couldn't open file for writing: $!";
open (FILE, 'script.pl') or die "Couldn't open file for reading";
## same thing here too
while (<FILE>) {
$line = $_;
chomp($line);
print NEW "$line\n";
}
close (FILE); ## close($fh) or die "can't 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
$sillyrectangle = chr(10);
open (NEW, '>workingscript.pl') or die "Couldn't open file for writing";
## same as above
open (FILE, 'niceviewscript.pl') or die "Couldn't open file";
## same thing here
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
Launch your FileZilla Program, click on the menu bar "Transfer" menu,
then check your sub-menu bar "Transfer type", you should be able to change,
your mode. I preferred to leave mine as "Auto"!
will solve my problem even better of course. But now I'm curious as to
why my program for switching back doesn't work.
Thank you!
Fred
--
Tim
--
Tim
Rob Dixon
2012-03-27 07:51:18 UTC
Permalink
Post by hOURS
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
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
$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>;
hOURS
2012-04-03 15:00:38 UTC
Permalink
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 <***@gmx.com> wrote:

From: Rob Dixon <***@gmx.com>
Subject: Re: ASCII/binary endline character trouble
To: ***@perl.org
Cc: "hOURS" <***@yahoo.com>
Date: Tuesday, March 27, 2012, 3:51 AM
Post by hOURS
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
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
$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>;

Jenda Krynicky
2012-03-27 22:22:24 UTC
Permalink
Date sent: Sun, 25 Mar 2012 15:45:11 -0700 (PDT)
I write CGI scripts for my website in PERL.=A0 When I used to upload
them with my FTP program I made sure to do so in ASCII mode rather
than binary.=A0 My host made me switch to sFTP however.=A0 I use
FIlezilla, and can't for the life of me find out how to choose the
mode on that.=A0 Unfortunately, binary is the default mode.=A0 I don't
like programming when the file displays ASCII endline characters.=A0 I'd
rather see clear, distinct lines rather than one long line broken up
with those vertical rectangles.=A0 When I download a script from my site
for editing it looks terrible.
Any even remotely decent editor handles unix line edings just fine.
What are you using to edit/view the files??? Notepad?

If you want something small and generic try
http://www.scintilla.org/SciTE.html
if you want something bigger and with more features try
http://padre.perlide.org/

Jenda
=3D=3D=3D=3D=3D ***@Krynicky.cz =3D=3D=3D http://Jenda.Krynicky.cz =3D=3D=
=3D=3D=3D
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
Loading...