karlgoethebier has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

i,m i'm banging my head against the wall with this:

use strict; use warnings; use IO::All; # v5.18.2 my $io = io $0; while ( my ( $index, $value ) = each @$io ) { print qq($index\t$value); print qq($index\t$value\n); } __END__

Only the second print works.

I wonder why.

Thank you very much for any hint and best regards,

Karl

«The Crux of the Biscuit is the Apostrophe»

Replies are listed 'Best First'.
Re: Why doesn't this print when i omit the newline?
by ikegami (Patriarch) on Dec 16, 2014 at 14:42 UTC
    Because $value ends with a Carriage Return, the second line overwrites the first.

      D'oh! That hurts!

      Best regards and thank you, Karl

      «The Crux of the Biscuit is the Apostrophe»

        See Data::Dumper's Useqq option, or Data::Dump:

        $ perl -MData::Dumper \ -le '$Data::Dumper::Useqq=1; print Dumper("foo\r\n")' $VAR1 = "foo\r\n"; $ perl -MData::Dump=pp -le 'print pp("foo\r\n")' "foo\r\n"
      Reading the thread, I was also going to suggest this possibility, if ikegami had not done it before, because I had the same problem a couple of months ago and it took me at least 30 minutes to figure out that the line that appeared not to be printed was in fact overwritten by the next line (data produced on Windows and used on a Unix box, so that chomp removed the \n but not the \r).

        “Oooohhhh...!! Wick-ed!”

Re: Why doesn't this print when i omit the newline?
by QM (Parson) on Dec 16, 2014 at 13:15 UTC
    STDOUT is usually buffered by default. It's all queueing up until it sees a newline. Try setting $| = 1; before the loop, and add a sleep 5; between the prints. Then you should see something interesting.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      ..."Then you should see something interesting"

      Here we go:

      Thanks and best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

        Please don't manually add line numbers to your code. Then if I want to paste it, I have to remove them again. PerlMonks has an option in your user settings to enable line numbers so that you see them next to any code segment.

        You didn't mention what output you are getting here, but on my system I immediately get the first line printed, and then five seconds later I get the first line plus a newline. Then immediately the second line, and five seconds later the second line plus a newline, and so on.

        Ubuntu Linux 14.04 with Perl 5.20.1.

        Ah, I get it... windows, and ikegami nabbed it below. :)


        Dave

Re: Why doesn't this print when i omit the newline?
by duelafn (Parson) on Dec 16, 2014 at 13:30 UTC

    To expand a bit on QM's node, there's an old article from The Perl Journal, "Suffering from Buffering?", that would be a good read.

    Good Day,
        Dean

      Thank you for the hint and best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

Re: Why doesn't this print when i omit the newline?
by toolic (Bishop) on Dec 16, 2014 at 13:16 UTC
    It prints everything for me on 1 line when I comment out the 2nd print. I'm on perl v5.12.2, IO::All 0.46.

      Thank you toolic for feedback.

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

Re: Why doesn't this print when i omit the newline?
by Anonymous Monk on Dec 16, 2014 at 13:22 UTC

    For clarification: You mean it doesn't print anything at all?? What I am seeing is this:

    0 use strict;0 use strict; 1 use warnings;1 use warnings; ...

    And inspecting $index and $value seems to show that they are plain scalars and not objects with overloaded stringification. Are you printing to STDOUT or a file? Are you using select at all, or perhaps some other part of your program is?

      The first print should print everything on one line and the second all the lines (with newline), as obsevered by toolic at Re: Why doesn't this print when i omit the newline?.

      I print to STDOUT and don't use select.

      I don't do anything else in my code - it's as shown in the example.

      Thank you and best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

        Sorry, can't reproduce on Linux / v5.20 / IO::All v0.85. So I'll just throw out a couple more debugging guesses...

        If you replace my $io = io $0; with my $io = [qw/foo bar/]; (and perhaps drop the use IO::All;) I hope it works?

        Have you tried opening something other than $0?