Thanx for the pointer, almut. That dup & perlio scrap is interesting.

But there must be more to it than that, as I don't see any special treatment for STDOUT in the perlio.c scrap (neither for the numeric FD's 0 to 2):

I was playing with the scrap below in the meantime.

I dupped SAVOUT on STDERR instead / simplifying system to printing / using autoflush / opening STDOUT myself to /dev/tty first: no change.

This however is interesting:

Changing the name of the handle STDOUT <=> ANYTHINGeLSE manages to act as a toggle for the problem. Furthermore, w/o close, the tell on the STDOUT file pointer at begin prints 19 in the example below (might be due to the handle earlier being a tty, and something didn't quite catch the change to a plain file w/o explicit close?). Any other handle name prints 5 regardless of close or no close.

So it looks like we have some hard-coded STDOUT-related magic somewhere in the guts of PERLIO or even lower, with probably STDIN/ERR offering similar peculiarities.

use IO::Handle; open(SAVOUT, '>&STDERR') or die $!; #open(SAVOUT, '>&STDOUT') or die $!; # play with these: # s/STDOUT/STDOUT/; s/STDOUT/STDOUT/ close STDOUT; open(STDOUT, '>>', "/dev/tty"); STDOUT->autoflush; print STDOUT "a STDOUT test\n"; # play with this: # close STDOUT; open(STDOUT, '+>', "/tmp/STDOUT.log") or die $!; STDOUT->autoflush; print STDOUT "test\n"; #system("echo hello world"); print SAVOUT "before=", tell(STDOUT), "\n"; seek(STDOUT, 0, 0) or die $!; print SAVOUT "after=", tell(STDOUT), "\n"; while (1) { my $rv = read STDOUT, $_, 8192; die "read: $!\n" if !defined($rv); last unless $_; print SAVOUT "STDOUT=", $_; } print SAVOUT "at end=", tell(STDOUT), "\n"; close STDOUT;

Given that too much in Perl, esp wrt <> and stdio is magic, it's probably a good idea to say strictly outside any possibly dusty corner whose smell is faintly related to something magic. Which in this case might just be the idea of reusing a special handle, and worse, reading from it.

How to classify this behaviour: What doc/code do we still miss? Or is this indeed, say, an easy-to-fix oversight in the documentation? Or is it a somewhat larger actual bug?

Still wondering (& vowing to step even more cautiously anywhere near STDIO magic),
My thanx to almut & ikegami for the work below!
less confused now (& busy scribbling away two new-to-me debugging tips along with a link to their demonstration here)
Peter


In reply to Re^4: reading from a file after a seek isn't working for me by jakobi
in thread reading from a file after a seek isn't working for me by samwyse

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.