print prints things in strange order.

Discussion in 'Perl Misc' started by Robbie Hatley, Oct 7, 2005.

  1. 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
    Robbie Hatley, Oct 7, 2005
    #1
    1. Advertising

  2. "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/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Oct 7, 2005
    #2
    1. Advertising

  3. 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
    John W. Krahn, Oct 7, 2005
    #3
  4. 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
    Robbie Hatley, Oct 7, 2005
    #4
  5. Robbie Hatley

    Dr.Ruud Guest

    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."
    Dr.Ruud, Oct 7, 2005
    #5
  6. 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
    Tad McClellan, Oct 7, 2005
    #6
  7. "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
    Robbie Hatley, Oct 9, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=

    vs2005 publish website doing bad things, bad things

    =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=, Oct 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    587
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Oct 25, 2006
  2. keto
    Replies:
    0
    Views:
    898
  3. Replies:
    2
    Views:
    298
  4. karthikbalaguru
    Replies:
    18
    Views:
    369
  5. PW
    Replies:
    3
    Views:
    130
Loading...

Share This Page