what's wrong with <DATA>

Discussion in 'Perl Misc' started by cyl, Apr 3, 2006.

  1. cyl

    cyl Guest

    my script is as below

    $i=1;
    while(<DATA>){
    $x=`dir $_`;
    print $i++.". $_";
    }
    __DATA__
    123.txt
    456.txt
    789.txt

    and the output is
    1. 123.txt
    2.
    3. 456.txt
    4.
    5. 789.txt

    Can anyone shed a light on why the extra blank appeared? Thanks.
    cyl, Apr 3, 2006
    #1
    1. Advertising

  2. cyl wrote:
    > my script is as below
    >
    > $i=1;
    > while(<DATA>){
    > $x=`dir $_`;
    > print $i++.". $_";
    > }
    > __DATA__
    > 123.txt
    > 456.txt
    > 789.txt
    >
    > and the output is
    > 1. 123.txt
    > 2.
    > 3. 456.txt
    > 4.
    > 5. 789.txt
    >
    > Can anyone shed a light on why the extra blank appeared? Thanks.


    Doesn't do it here. Maybe you
    --
    Josef Mo"llers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Apr 3, 2006
    #2
    1. Advertising

  3. cyl wrote:
    > my script is as below
    >
    > $i=1;
    > while(<DATA>){
    > $x=`dir $_`;
    > print $i++.". $_";
    > }
    > __DATA__
    > 123.txt
    > 456.txt
    > 789.txt
    >
    > and the output is
    > 1. 123.txt
    > 2.
    > 3. 456.txt
    > 4.
    > 5. 789.txt
    >
    > Can anyone shed a light on why the extra blank appeared? Thanks.


    Can't reproduce.
    Maybe you have CR+LF in your file?


    --
    Josef Mo"llers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Apr 3, 2006
    #3
  4. cyl

    Anno Siegel Guest

    cyl <> wrote in comp.lang.perl.misc:
    > my script is as below
    >
    > $i=1;
    > while(<DATA>){
    > $x=`dir $_`;
    > print $i++.". $_";
    > }
    > __DATA__
    > 123.txt
    > 456.txt
    > 789.txt


    You're running without "strict". Switch it on and declare your variables.
    What is the variable $x for? You're not using it.

    > and the output is
    > 1. 123.txt
    > 2.
    > 3. 456.txt
    > 4.
    > 5. 789.txt
    >
    > Can anyone shed a light on why the extra blank appeared? Thanks.


    I don't see that output, it prints three lines as expected. What kind
    of line feeds does your source file have and what are the settings of
    $/ and $\?

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Apr 3, 2006
    #4
  5. cyl

    Guest

    cyl <> wrote:
    > my script is as below


    > $i=1;
    > while(<DATA>){
    > $x=`dir $_`;
    > print $i++.". $_";
    > }
    > __DATA__
    > 123.txt
    > 456.txt
    > 789.txt


    > and the output is
    > 1. 123.txt
    > 2.
    > 3. 456.txt
    > 4.
    > 5. 789.txt


    > Can anyone shed a light on why the extra blank appeared? Thanks.


    It doesn't on my system. Maybe you have some non-printing characters
    embedded after the 'txt' portion of each datum.

    Axel
    , Apr 3, 2006
    #5
  6. cyl

    Paul Lalli Guest

    cyl wrote:
    > my script is as below
    >
    > $i=1;
    > while(<DATA>){
    > $x=`dir $_`;
    > print $i++.". $_";
    > }
    > __DATA__
    > 123.txt
    > 456.txt
    > 789.txt
    >
    > and the output is
    > 1. 123.txt
    > 2.
    > 3. 456.txt
    > 4.
    > 5. 789.txt
    >
    > Can anyone shed a light on why the extra blank appeared? Thanks.


    I have no answers for you, but I will state that unlike the rest of the
    responders, I did duplicate your issue. Under Windows 2000
    Professional and ActiveState's Perl 5.8.4, the following code (modified
    from the above to agree with the Posting Guidelines),

    #!/usr/bin/perl
    use strict;
    use warnings;

    my $i = 1;
    while (<DATA>) {
    chomp;
    print "$i: '$_'\n";
    $i++;
    my $x = qx"dir $_";
    }

    __DATA__
    123.txt
    456.txt
    789.txt

    gave the same output as above. (Except that mine also printed three
    "file not founds" because I didn't have those three files).

    Very interesting to note is that after I duplicated your problem and
    couldn't explain why, I rebooted my computer for an unrelated reason.
    I just went back to the same program, without modifying anything, and
    now the code works as I expect it to. I get three lines of output,
    plus the File Not Founds.

    I have no idea what was the original cause of the problem, but the fact
    that a reboot solved it leads me to blame Windows in general. I'm
    sorry I can't offer more assistance than that.

    Paul Lalli
    Paul Lalli, Apr 3, 2006
    #6
  7. cyl

    cyl Guest

    My script is written in Windows so it has CRLF in the end of a line. I
    tested it again in both Windows and Linux and only got this problem in
    Windows. If I convert the CRLF to LF, the problem disappears. Another
    interesting thing to me is that if I comment the system call, the
    problem won't happen either even with CRLF in the end. Like this

    $i=1;
    while(<DATA>){
    #$x=`dir $_`;
    print $i++.". $_";
    }
    __DATA__
    123.txt
    456.txt
    789.txt

    output is
    1. 123.txt
    2. 456.txt
    3. 789.txt

    so there might be something during the system call that affect the file
    handle DATA?
    cyl, Apr 4, 2006
    #7
  8. cyl

    Brad Murray Guest

    cyl <> wrote:
    c> My script is written in Windows so it has CRLF in the end of a line. I
    c> tested it again in both Windows and Linux and only got this problem in
    c> Windows. If I convert the CRLF to LF, the problem disappears. Another
    c> interesting thing to me is that if I comment the system call, the
    c> problem won't happen either even with CRLF in the end. Like this
    c>
    c> $i=1;
    c> while(<DATA>){

    chomp;

    c> #$x=`dir $_`;
    c> print $i++.". $_";
    c> }
    c> __DATA__
    c> 123.txt
    c> 456.txt
    c> 789.txt

    Why not chomp your data if you don't want the newlines? No matter how
    you slice it, the newlines are probably not intended to be part of
    your filenames, so why try to make things work with them intact?

    --
    Brad Murray * "This won't help relations. Marines taking over a Baghdad
    VSCA Founder * hotel. [yelling]" -- CNN closed captioning error
    Brad Murray, Apr 4, 2006
    #8
  9. cyl wrote:
    > My script is written in Windows so it has CRLF in the end of a line. I
    > tested it again in both Windows and Linux and only got this problem in
    > Windows. If I convert the CRLF to LF, the problem disappears. Another
    > interesting thing to me is that if I comment the system call, the
    > problem won't happen either even with CRLF in the end. Like this
    >
    > $i=1;
    > while(<DATA>){
    > #$x=`dir $_`;
    > print $i++.". $_";
    > }
    > __DATA__
    > 123.txt
    > 456.txt
    > 789.txt
    >
    > output is
    > 1. 123.txt
    > 2. 456.txt
    > 3. 789.txt
    >
    > so there might be something during the system call that affect the file
    > handle DATA?


    Not being a conoisseur of the software that comes from the northwest of
    the US, I venture to put it the other way round: Maybe the DATA
    containing CRLFs affects the working of the backticks operation?

    Like: `dir 123.txt<CR><LF>` causes two operations to happen:
    - execute "dir 123.txt"
    - echo CRLF

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Apr 4, 2006
    #9
  10. cyl

    Ch Lamprecht Guest

    Josef Moellers wrote:
    > cyl wrote:


    >> $i=1;
    >> while(<DATA>){
    >> #$x=`dir $_`;
    >> print $i++.". $_";
    >> }
    >> __DATA__
    >> 123.txt
    >> 456.txt
    >> 789.txt
    >>


    > Not being a conoisseur of the software that comes from the northwest of
    > the US, I venture to put it the other way round: Maybe the DATA
    > containing CRLFs affects the working of the backticks operation?
    >
    > Like: `dir 123.txt<CR><LF>` causes two operations to happen:
    > - execute "dir 123.txt"
    > - echo CRLF
    >

    The backticks work as expected. The question is: why do the backticks
    change the behaviour of <DATA>...

    use warnings;
    use strict;
    while(<DATA>){
    ` `;
    print "$. $_";
    }
    __DATA__
    123.txt
    456.txt
    789.txt

    On WinXP, -in case the file has CRLF EOLs- this prints:

    1 123.txt
    2
    3 456.txt
    4
    5 789.txt

    In case the file has LF EOLs or with backticks commented out it prints:
    1 123.txt
    2 456.txt
    3 789.txt

    Christoph
    --

    perl -e "print scalar reverse q//"
    Ch Lamprecht, Apr 4, 2006
    #10
  11. cyl

    Dr.Ruud Guest

    Ch Lamprecht schreef:


    > [backticks change the behaviour of <DATA>]
    > On WinXP, -in case the file has CRLF EOLs- this prints:
    >
    > 1 123.txt
    > 2
    > 3 456.txt
    > 4
    > 5 789.txt


    Looks like an implicit (lexical/local?) 'binmode' is performed, if
    backticks.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Apr 4, 2006
    #11
  12. cyl wrote:
    > My script is written in Windows so it has CRLF in the end of a line. I
    > tested it again in both Windows and Linux and only got this problem in
    > Windows. If I convert the CRLF to LF, the problem disappears. Another
    > interesting thing to me is that if I comment the system call, the
    > problem won't happen either even with CRLF in the end. Like this
    >
    > $i=1;
    > while(<DATA>){
    > #$x=`dir $_`;
    > print $i++.". $_";
    > }
    > __DATA__
    > 123.txt
    > 456.txt
    > 789.txt
    >
    > output is
    > 1. 123.txt
    > 2. 456.txt
    > 3. 789.txt
    >
    > so there might be something during the system call that affect the file
    > handle DATA?
    >


    What happens if you leave out the "print", or redirect it into another
    file (print STDERR ..., then redirect stderr into another file)?

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Apr 4, 2006
    #12
  13. cyl

    Dr.Ruud Guest

    Ch Lamprecht schreef:

    > ` `;


    Backticks in a void context, could be another special case as well.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Apr 4, 2006
    #13
  14. cyl

    Ch Lamprecht Guest

    Dr.Ruud wrote:
    > Ch Lamprecht schreef:
    >
    >
    >
    >>[backticks change the behaviour of <DATA>]
    >>On WinXP, -in case the file has CRLF EOLs- this prints:
    >>
    >>1 123.txt
    >>2
    >>3 456.txt
    >>4
    >>5 789.txt

    >
    >
    > Looks like an implicit (lexical/local?) 'binmode' is performed, if
    > backticks.
    >

    It does not seem to be a matter of scope- this one still shows the same
    output:

    use warnings;
    use strict;
    while(<DATA>){
    b_t();
    print "$. $_";
    }
    sub b_t{
    ` `;
    #system ('echo test');
    }
    __DATA__
    123.txt
    456.txt
    789.txt

    You can as well replace the backticks with a system call - that won't
    change anything ether.

    Christoph
    --

    perl -e "print scalar reverse q//"
    Ch Lamprecht, Apr 4, 2006
    #14
    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. Irishmaninusa

    Dropdown displaying data in the wrong order

    Irishmaninusa, Aug 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    444
    Irishmaninusa
    Aug 27, 2004
  2. David Hearn
    Replies:
    0
    Views:
    353
    David Hearn
    Apr 18, 2005
  3. Liza
    Replies:
    2
    Views:
    546
  4. Aaron
    Replies:
    0
    Views:
    387
    Aaron
    Jan 2, 2006
  5. Matthew
    Replies:
    7
    Views:
    665
    Priscilla Walmsley
    Jan 7, 2005
Loading...

Share This Page