print prints things in strange order.

R

Robbie Hatley

Howdy. I'm trying to get this (should be) simple test
program to work, but it does different things on different
perl implimentations:


#!/bin/perl
open my $F, $0 or die $!;
while (<$F>)
{
chomp;
print ("one two three " . "$_" . " DEF \n");
}
close $F;



Here's the output with djgpp's version of perl:


wd=C:\scripts
%

wd=C:\scripts
%perl dollscor
one two three #!/bin/perl DEF
one two three open my $F, $0 or die $!; DEF
one two three while (<$F>) DEF
one two three { DEF
one two three chomp; DEF
one two three print ("one two three " . $_ . " DEF \n"); DEF
one two three } DEF
one two three close $F; DEF

wd=C:\scripts
%


Pretty much what you'd expect. BUT...
But here's the output with cygwin's version of perl:


bash-3.00
/cygdrive/c/scripts
%dollscor
DEF wo three #!/bin/perl
DEF wo three open my $F, $0 or die $!;
DEF wo three while (<$F>)
DEF wo three {
DEF wo three chomp;
DEF wo three print ("one two three " . $_ . " DEF \n");
DEF wo three }
DEF wo three close $F;

bash-3.00
/cygdrive/c/scripts
%


What's going on there??? The DEF should print at the end of the
line, but is over-writing the beginning instead.

Is one of my perl implimentations broken, or am I doing something
"undefined"? Perhaps something about the way I'm using $_ ?
I can't figure it out.


For reference, here's the version info:

djgpp:
%perl -v
This is perl, v5.6.1 built for dos-djgpp
Copyright 1987-2001, Larry Wall
MS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis
djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996
djgpp v2 port (perl5004+) by Laszlo Molnar, 1997-1999

cygwin:
%perl -v
This is perl, v5.8.7 built for cygwin-thread-multi-64int
Copyright 1987-2005, Larry Wall




Puzzled,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
 
A

A. Sinan Unur

Howdy. I'm trying to get this (should be) simple test
program to work, but it does different things on different
perl implimentations:
....

#!/bin/perl
open my $F, $0 or die $!;
while (<$F>)
{
chomp;
print ("one two three " . "$_" . " DEF \n");
}
close $F;


That is what you see on the screen. Do you know what is in the file?

Save the source file Unix line endings (LF) rather than MSDOS line
endings, and then try. Alternatively,

#!/usr/bin/perl

use strict;
use warnings;

open my $F, $0 or die $!;
binmode $F;
while (<$F>) {
s/:)?\015\012)|:)?\015)|:)?\012)// ;
print ("one two three " . "$_" . " DEF \n");
}
close $F;

Sinan
 
J

John W. Krahn

Robbie said:
Howdy. I'm trying to get this (should be) simple test
program to work, but it does different things on different
perl implimentations:


#!/bin/perl
open my $F, $0 or die $!;
while (<$F>)
{
chomp;
print ("one two three " . "$_" . " DEF \n");
}
close $F;



Here's the output with djgpp's version of perl:


wd=C:\scripts
%

wd=C:\scripts
%perl dollscor
one two three #!/bin/perl DEF
one two three open my $F, $0 or die $!; DEF
one two three while (<$F>) DEF
one two three { DEF
one two three chomp; DEF
one two three print ("one two three " . $_ . " DEF \n"); DEF
one two three } DEF
one two three close $F; DEF

wd=C:\scripts
%


Pretty much what you'd expect. BUT...
But here's the output with cygwin's version of perl:


bash-3.00
/cygdrive/c/scripts
%dollscor
DEF wo three #!/bin/perl
DEF wo three open my $F, $0 or die $!;
DEF wo three while (<$F>)
DEF wo three {
DEF wo three chomp;
DEF wo three print ("one two three " . $_ . " DEF \n");
DEF wo three }
DEF wo three close $F;

bash-3.00
/cygdrive/c/scripts
%


What's going on there??? The DEF should print at the end of the
line, but is over-writing the beginning instead.

Your perl program is stored on disk in a DOS text format that has a carriage
return and line feed at the end of the line and the djgpp version's chomp()
removes these correctly however cygwin is a UNIX enviroment so it only removes
the line feed but not the carriage return.

perldoc perlport
perldoc perldos
perldoc perlcygwin



John
 
R

Robbie Hatley

Ok, thanks to "John W. Krahn" and "A. Sinan Unur" for the
fast replies to my post.

I'd written:

and John replied:
Your perl program is stored on disk in a DOS text format that
has a carriage return and line feed at the end of the line and
the djgpp version's chomp() removes these correctly however
cygwin is a UNIX enviroment so it only removes the line feed
but not the carriage return.

AH, I see it clearly now. The LF is gone but the CR remains, so
the print cursor obediantly goes to start of same line and starts
over-writing.

Perhaps I should get a unixy editor for use with cygwin... but
then my files would look like crud in notepad; and if I use
NoteTab (my favorite editor) instead, it'll change all the LFs
back to CRLFs anyway.

I think I'll alter chomp in the Perl source and recompile.

::: Reads source :::

How come this is full of Tolkien quotes? Not that I'm
complaining. Mithril and elven glass suit me fine.

A. Sinan Unur said:
s/:)?\015\012)|:)?\015)|:)?\012)//

Ok, cool chomp replacement. Basically, "replace CRLF, CR,
or LF with empty substring".

But what is this ":?" thingy? I'd read that RE as meaning
"zero-or-one colons", which doesn't seem to make sense in
this context.

::: reads www.perl.org man pages :::

Ah, I see... you meant non-capture grouping:

s/(?:\015\012)|(?:\015)|(?:\012)//

If you switch the ':' and '?', it's still a valid RE, but
it then means something very different! :)

Or just take out the groupings and chomp all CRs and LFs:

s/[\015\012]//g;

or even:

s/[\n\r]//g;


Cheers,
Robbie Hatley
 
D

Dr.Ruud

Robbie Hatley schreef:
Perhaps I should get a unixy editor for use with cygwin...

Try pico.

but then my files would look like crud in notepad

No problem with wordpad.

I think I'll alter chomp in the Perl source and recompile.

Forget it.


And I wouldn't assume that /\012/ and /\n/ are always the same,
/\n/ could just as well be a /\015/ on an $fruitmachine.
 
T

Tad McClellan

Robbie Hatley said:
I think I'll alter chomp in the Perl source and recompile.


That is absurd.

Try putting this near the top of the program that must process
DOS style files while running on a Unix style system:

local $/ = "\015\012";

You can read about the $/ variable in:

perldoc perlvar
 
R

Robbie Hatley

Tad McClellan said:
That is absurd.

I figure, what's the use of "open source" if it's not to be
tampered with? (But perhaps in this case it might not be
worth the hastle.)
Try putting this near the top of the program that must process
DOS style files while running on a Unix style system:

local $/ = "\015\012";

Yep, that works for me. Causes chomp to eat CRLF instead of just
LF.
You can read about the $/ variable in:

perldoc perlvar

Some sort of "newline character selector", I'd guess?
I'll check it out. Thanks.


Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top