Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > print prints things in strange order.

Reply
Thread Tools

print prints things in strange order.

 
 
Robbie Hatley
Guest
Posts: n/a
 
      10-07-2005
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


 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-07-2005
"Robbie Hatley" <> wrote in news:Qps1f.13033
$:

> 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
--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      10-07-2005
Robbie Hatley wrote:
> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Robbie Hatley
Guest
Posts: n/a
 
      10-07-2005
Ok, thanks to "John W. Krahn" and "A. Sinan Unur" for the
fast replies to my post.

I'd written:

> > #!/bin/perl
> > open my $F, $0 or die $!;
> > while (<$F>)
> > {
> > chomp;
> > print ("one two three " . "$_" . " DEF \n");
> > }
> > close $F;
> > ...
> > djgpp Perl outputs:
> > 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;
> > What's going on there???...


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 wrote:

> 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


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-07-2005
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.

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-07-2005
Robbie Hatley <> wrote:

> 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


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Robbie Hatley
Guest
Posts: n/a
 
      10-09-2005
"Tad McClellan" wrote:
> Robbie Hatley wrote:
>
> > I think I'll alter chomp in the Perl source and recompile.

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
vs2005 publish website doing bad things, bad things =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?= ASP .Net 1 10-25-2006 06:18 PM
Durability of inkjet prints, photo prints Bill Tuthill Digital Photography 45 05-02-2004 03:04 AM
Just recieved my 15 free digital prints from Kellards- Amazing Quality Prints! Bruce Digital Photography 5 12-29-2003 02:49 AM
Why the prints from film feel more 3D then prints from digital. Victor81 Digital Photography 35 12-12-2003 07:49 AM
Epson 1280 prints NOT Waterproof, 2200 prints ARE, Right? Dr. Slick Digital Photography 12 11-29-2003 10:19 AM



Advertisments