develooper Front page | perl.perl5.porters | Postings from September 2004

Re: [perl #31455] Dup DATA filehandle, lose initial data

Thread Previous | Thread Next
Nick Ing-Simmons
September 16, 2004 02:17
Re: [perl #31455] Dup DATA filehandle, lose initial data
Message ID:
Jim Carter <> writes:
>On Mon, 6 Sep 2004, Nick Ing-Simmons via RT wrote:
>> Can you try this with 
>> PERLIO=perlio perl ...
>> and 
>> PERLIO=stdio perl ...
>Yes, it fails equally with perlio and stdio.  

Good - principle of least surprise seems to be working for our perlio 
clone of stdio behaviour ;-)

>Do I _need_ to do a dup?  My original motivation was to use standard
>features to encompass this unusual case: normally the table is a plain file
>named on the command line, but for ultra paranoia I concatenate the script
>and the table, and then specify '<&DATA'.  As a workaround, what I'm doing
>now is to test for the string '<&DATA', and in that case return \*DATA
>rather than opening the so-called filename.
>I kind of figured that when a stream was duped, the remainder of the
>original filehandle's buffer would be pre-stuffed into the new stream
>object, so the duplication would be exact.  

It does not work like that (as you have discovered). Pre-stuffing 
buffer is no use if (as is common) dup is for benefit of a sub-process.
Instead it tries to achieve same effect by doing seek().

>But of course that takes work,

We could probably pre-stuff buffers most places (at least with :perlio),
but I am unconvinced it would be a big win.

>and the cross-platform issues are probably a disaster.  If a partially used
>stream object were copied either by memcpy 

Wouldn't work - two streams would try and use same buffer memory.

>or by its copy constructor, 

Most IO libraries are not C++ but C - so no such thing as a "copy constructor".

As I say for :perlio we could make ->Dup() call pre-stuff the buffer.
Perhaps we could do that if seek fails, but that won't help 
if dup'ed handle is handed off to a sub-process or accessed 
with unbuffered IO.
So I am not sure it is enough "better" to be worth the 
complexity speed hit etc.

>wonder if the result would be useable on most platforms?
>Your point is well taken that both instances share the same file pointer, 
>so if you do buffered I/O from both instances interleaved, the data would 
>be totally trashed.  
>James F. Carter          Voice 310 825 2897    FAX 310 206 6673
>UCLA-Mathnet;  6115 MSA; 405 Hilgard Ave.; Los Angeles, CA, USA  90095-1555
>Email: (q.v. for PGP key)

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About